From 6b360855e95a99be674c344f3289849f9f7e19f8 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 22 Aug 2015 20:29:12 +0200 Subject: [PATCH 1/3] Don't error when checking for ng_di_order on non function declarations --- rules/ng_di_order.js | 2 +- test/ng_di_order.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/rules/ng_di_order.js b/rules/ng_di_order.js index 009edaee..0c787ec2 100644 --- a/rules/ng_di_order.js +++ b/rules/ng_di_order.js @@ -37,7 +37,7 @@ module.exports = function(context) { 'CallExpression': function(node) { // An Angular component definition. - if(utils.isAngularComponent(node) && node.callee.type === 'MemberExpression' && angularNamedObjectList.indexOf(node.callee.property.name) >= 0){ + if(utils.isAngularComponent(node) && node.callee.type === 'MemberExpression' && node.arguments[1].type === 'FunctionExpression' && angularNamedObjectList.indexOf(node.callee.property.name) >= 0){ return checkOrder(node, node.arguments[1]); } // Injected values in unittests. diff --git a/test/ng_di_order.js b/test/ng_di_order.js index 8829481d..d98a39c5 100644 --- a/test/ng_di_order.js +++ b/test/ng_di_order.js @@ -15,6 +15,7 @@ eslintTester.run('ng_di_order', rule, { 'app.controller("", function($http, $q){});', 'app.directive("", function($http, $q){});', 'app.factory("", function($http, $q){});', + 'app.factory("", fsctoryName);', 'app.filter("", function($http, $q){});', 'app.provider("", function($http, $q){});', 'app.service("", function($http, $q){});', From 353c84a4d2bcdce42644b3d90a5b0d83e5663703 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 22 Aug 2015 20:34:23 +0200 Subject: [PATCH 2/3] Report the function expression for ng_di_order Reporting the function expression instead of the call expression results into eslint showing the correct line numbers. --- rules/ng_di_order.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rules/ng_di_order.js b/rules/ng_di_order.js index 0c787ec2..d27a3fc1 100644 --- a/rules/ng_di_order.js +++ b/rules/ng_di_order.js @@ -13,14 +13,14 @@ module.exports = function(context) { 'service' ]; - function checkOrder(node, fn) { + function checkOrder(fn) { var args = fn.params.map(function(arg) { return arg.name; }); var sortedArgs = args.slice().sort(); sortedArgs.some(function(value, index) { if(args.indexOf(value) !== index) { - context.report(node, 'Injected values should be sorted alphabetically'); + context.report(fn, 'Injected values should be sorted alphabetically'); return true; } }); @@ -31,18 +31,18 @@ module.exports = function(context) { 'AssignmentExpression': function(node) { // The $get function of a provider. if(node.left.type === 'MemberExpression' && node.left.property.name === '$get') { - return checkOrder(node, node.right); + return checkOrder(node.right); } }, 'CallExpression': function(node) { // An Angular component definition. if(utils.isAngularComponent(node) && node.callee.type === 'MemberExpression' && node.arguments[1].type === 'FunctionExpression' && angularNamedObjectList.indexOf(node.callee.property.name) >= 0){ - return checkOrder(node, node.arguments[1]); + return checkOrder(node.arguments[1]); } // Injected values in unittests. if(node.callee.type === 'Identifier' && node.callee.name === 'inject') { - return checkOrder(node, node.arguments[0]); + return checkOrder(node.arguments[0]); } } }; From 5587560264839dc5a7349f0ab31b8487bd30bfdd Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 22 Aug 2015 20:45:44 +0200 Subject: [PATCH 3/3] Also check di order in config and run functions --- rules/ng_di_order.js | 8 ++++++++ test/ng_di_order.js | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/rules/ng_di_order.js b/rules/ng_di_order.js index d27a3fc1..7f55012b 100644 --- a/rules/ng_di_order.js +++ b/rules/ng_di_order.js @@ -12,6 +12,10 @@ module.exports = function(context) { 'provider', 'service' ]; + var setupCalls = [ + 'config', + 'run' + ]; function checkOrder(fn) { var args = fn.params.map(function(arg) { @@ -40,6 +44,10 @@ module.exports = function(context) { if(utils.isAngularComponent(node) && node.callee.type === 'MemberExpression' && node.arguments[1].type === 'FunctionExpression' && angularNamedObjectList.indexOf(node.callee.property.name) >= 0){ return checkOrder(node.arguments[1]); } + // Config and run functions. + if(node.callee.type === 'MemberExpression' && node.arguments.length > 0 && setupCalls.indexOf(node.callee.property.name) !== -1 && node.arguments[0].type === 'FunctionExpression') { + return checkOrder(node.arguments[0]); + } // Injected values in unittests. if(node.callee.type === 'Identifier' && node.callee.name === 'inject') { return checkOrder(node.arguments[0]); diff --git a/test/ng_di_order.js b/test/ng_di_order.js index d98a39c5..fa69a53d 100644 --- a/test/ng_di_order.js +++ b/test/ng_di_order.js @@ -19,6 +19,8 @@ eslintTester.run('ng_di_order', rule, { 'app.filter("", function($http, $q){});', 'app.provider("", function($http, $q){});', 'app.service("", function($http, $q){});', + 'app.config(function($httpProvider, $routeProvider){});', + 'app.run(function($http, $q){});', 'inject(function($http, $q){});', 'it(inject(function($http, $q){}));', 'this.$get = function($http, $q){};', @@ -41,6 +43,12 @@ eslintTester.run('ng_di_order', rule, { }, { code: 'app.service("", function($q, $http){});', errors: [{message: 'Injected values should be sorted alphabetically'}] + }, { + code: 'app.config(function($routeProvider, $httpProvider){});', + errors: [{message: 'Injected values should be sorted alphabetically'}] + }, { + code: 'app.run(function($q, $http){});', + errors: [{message: 'Injected values should be sorted alphabetically'}] }, { code: 'inject(function($q, $http){});', errors: [{message: 'Injected values should be sorted alphabetically'}]