in rules/no-services.js [29:101]
create: function(context) {
let angularObjectList = ['controller', 'filter', 'directive'];
let badServices = [];
let map;
let message = 'REST API calls should be implemented in a specific service';
function isArray(item) {
return Object.prototype.toString.call(item) === '[object Array]';
}
function isObject(item) {
return Object.prototype.toString.call(item) === '[object Object]';
}
if (context.options[0] === undefined) {
badServices = [/\$http/, /\$resource/, /Restangular/, /\$q/, /\$filter/];
}
if (isArray(context.options[0])) {
badServices = context.options[0];
}
if (isArray(context.options[1])) {
angularObjectList = context.options[1];
}
if (isObject(context.options[0])) {
map = context.options[0];
let result = [];
let prop;
for (prop in map) {
if (map.hasOwnProperty(prop)) {
result.push(prop);
}
}
angularObjectList = result;
}
function isSetBedService(serviceName, angularObjectName) {
if (map) {
return map[angularObjectName].find(object => utils.convertPrefixToRegex(object).test(serviceName));
}
return badServices.find(object => utils.convertPrefixToRegex(object).test(serviceName));
}
return {
CallExpression: function(node) {
let callee = node.callee;
if (utils.isAngularComponent(node) && callee.type === 'MemberExpression' && angularObjectList.indexOf(callee.property.name) >= 0) {
if (utils.isFunctionType(node.arguments[1])) {
node.arguments[1].params.forEach(function(service) {
if (service.type === 'Identifier' && isSetBedService(service.name, callee.property.name)) {
context.report(node, message + ' (' + service.name + ' in ' + callee.property.name + ')', {});
}
});
}
if (utils.isArrayType(node.arguments[1])) {
node.arguments[1].elements.forEach(function(service) {
if (service.type === 'Literal' && isSetBedService(service.value, callee.property.name)) {
context.report(node, message + ' (' + service.value + ' in ' + callee.property.name + ')', {});
}
});
}
}
}
};
}