Skip to content

Commit 9f9e3a2

Browse files
committed
Merge pull request #157 from Gillespie59/development
0.5.0
2 parents 24ab243 + e80e521 commit 9f9e3a2

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ We provide also three samples :
100100
| 'ng_no_services': [2, ['$http', '$resource', 'Restangular']] | Some services should be used only in a specific AngularJS service (Ajax-based service for example), in order to follow the separation of concerns paradigm. The second parameter specifies the services. The third parameter can be a list of angular objects (controller, factory, etc.). Or second parameter can be an object, where keys are angular object names and value is a list of services (like {controller: ['$http'], factory: ['$q']}) |
101101
| 'ng_no_service_method': 2 | You should prefer the factory() method instead of service() [Y040](https://github.com/johnpapa/angular-styleguide#style-y040)|
102102
| 'ng_on_watch': 2 | Watch and On methods on the scope object should be assigned to a variable, in order to be deleted in a $destroy event handler |
103-
| 'ng_service_name': 2 | All your services should have a name starting with the parameter you can define in your config object. The second parameter can be a Regexp wrapped in quotes. You can not prefix your services by "$" (reserved keyword for AngularJS services) ("ng_service_name": [2, "ng"]) [Y125](https://github.com/johnpapa/angular-styleguide#style-y125) |
103+
| 'ng_rest_service': 0 | Check the service used to send request to your REST API. This rule can have one parameter, with tone he following value : $http, $resource or Restangular ('ng_rest_service': [0, '$http']).
104+
| 'ng_service_name': 2 | All your services should have a name starting with the parameter you can define in your config object. The second parameter can be a Regexp wrapped in quotes. You can not prefix your services by "$" (reserved keyword for AngularJS services) ("ng_service_name": [2, "ng"]) [Y125](https://github.com/johnpapa/angular-styleguide#style-y125) |
104105
| 'ng_timeout_service': 2 | Instead of the default setTimeout function, you should use the AngularJS wrapper service $timeout [Y181](https://github.com/johnpapa/angular-styleguide#style-y181) |
105106
| 'ng_typecheck_array': 2 | You should use the angular.isArray method instead of the default JavaScript implementation (typeof [] === "[object Array]"). |
106107
| 'ng_typecheck_date': 2 | You should use the angular.isDate method instead of the default JavaScript implementation (typeof new Date() === "[object Date]"). |

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
'ng_no_services': require('./rules/ng_no_services'),
3131
'ng_no_service_method': require('./rules/ng_no_service_method'),
3232
'ng_on_watch': require('./rules/ng_on_watch'),
33+
'ng_rest_service': require('./rules/ng_rest_service'),
3334
'ng_service_name': require('./rules/ng_service_name'),
3435
'ng_timeout_service': require('./rules/ng_timeout_service'),
3536
'ng_typecheck_array': require('./rules/ng_typecheck_array'),
@@ -70,6 +71,7 @@
7071
'ng_no_services': [2, ['$http', '$resource', 'Restangular', '$q']],
7172
'ng_no_service_method': 2,
7273
'ng_on_watch': 2,
74+
'ng_rest_service': 0,
7375
'ng_service_name': 0,
7476
'ng_timeout_service': 2,
7577
'ng_typecheck_array': 2,

rules/ng_rest_service.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module.exports = function(context) {
2+
3+
'use strict';
4+
5+
var utils = require('./utils/utils');
6+
7+
var angularObjectList = ['controller', 'filter', 'directive', 'service', 'factory', 'provider'];
8+
var services = ['$http', '$resource', 'Restangular'];
9+
var message = 'You should use the same service ({{method}}) for REST API calls';
10+
11+
12+
return {
13+
14+
'CallExpression': function(node) {
15+
16+
function checkElement(element){
17+
if(element.type === 'Identifier' && services.indexOf(element.name) >= 0 && context.options[0] !== element.name){
18+
context.report(node, message, {
19+
method: context.options[0]
20+
});
21+
} else if(element.type === 'Literal' && services.indexOf(element.value) >= 0 && context.options[0] !== element.value){
22+
context.report(node, message, {
23+
method: context.options[0]
24+
});
25+
}
26+
}
27+
28+
function checkAllElements(elements){
29+
elements.forEach(checkElement);
30+
}
31+
32+
var callee = node.callee;
33+
34+
if (utils.isAngularComponent(node) && callee.type === 'MemberExpression' && angularObjectList.indexOf(callee.property.name) >= 0) {
35+
if(utils.isFunctionType(node.arguments[1])){
36+
checkAllElements(node.arguments[1].params);
37+
}
38+
39+
if(utils.isArrayType(node.arguments[1])){
40+
checkAllElements(node.arguments[1].elements);
41+
42+
}
43+
}
44+
}
45+
};
46+
47+
};

test/ng_rest_service.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
var eslint = require('../node_modules/eslint/lib/eslint'),
6+
ESLintTester = require('eslint-tester');
7+
8+
var angularObjectList = ['controller', 'filter', 'directive', 'service', 'factory', 'provider'];
9+
var possibleValues = ['$http', '$resource', 'Restangular'];
10+
var valid = [], invalid = [];
11+
12+
13+
angularObjectList.forEach(function(object){
14+
possibleValues.forEach(function(value) {
15+
valid.push({
16+
code: 'app.' + object + '("name", function(Service1){});',
17+
args: [1, value]
18+
},{
19+
code: 'app.' + object + '("name", function(Service1){});',
20+
args: [1]
21+
}, {
22+
code: 'app.' + object + '("name", ["Service1", function(Service1){}]);',
23+
args: [1, value]
24+
}, {
25+
code: '"use strict";app.' + object + '("name", ["Service1", function(Service1){}]);',
26+
args: [1, value]
27+
});
28+
});
29+
30+
possibleValues.forEach(function(value){
31+
possibleValues.filter(function(v){ return v !== value}).forEach(function(badValue){
32+
invalid.push({
33+
code: 'app.' + object + '("name", function(' + badValue + '){});',
34+
args: [1, value],
35+
errors: [{ message: 'You should use the same service (' + value + ') for REST API calls'}]
36+
}, {
37+
code: 'app.' + object + '("name", ["' + badValue + '", function(' + badValue + '){}]);',
38+
args: [1, value],
39+
errors: [{ message: 'You should use the same service (' + value + ') for REST API calls'}]
40+
});
41+
});
42+
});
43+
44+
});
45+
46+
//------------------------------------------------------------------------------
47+
// Tests
48+
//------------------------------------------------------------------------------
49+
50+
var eslintTester = new ESLintTester(eslint);
51+
eslintTester.addRuleTest('rules/ng_rest_service', {
52+
valid: valid,
53+
invalid: invalid
54+
});

0 commit comments

Comments
 (0)