From 7646ba84e64ce5433465234b87d920821234f92f Mon Sep 17 00:00:00 2001 From: Tilman Potthof Date: Tue, 13 Oct 2015 18:29:10 +0200 Subject: [PATCH 1/6] Add failing test for function-type rule with named inline function (#229) --- test/function-type.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/function-type.js b/test/function-type.js index 9942a95c..e7b884cf 100644 --- a/test/function-type.js +++ b/test/function-type.js @@ -47,6 +47,9 @@ angularObjectList.forEach(function(object) { }, { code: 'function func(Service1) {};app.' + object + '("name", ["Service1", func]);', options: ['named'] + }, { + code: 'angular.module("myModule").factory("myService", function myService($http, $log) {});', + options: ['named'] }); }); From 29375bf4b8eff4fed6b4fd19fdc335eadbab31fa Mon Sep 17 00:00:00 2001 From: Tilman Potthof Date: Tue, 13 Oct 2015 18:43:58 +0200 Subject: [PATCH 2/6] Detect named inline functions as named functions for function-type rule (#229) --- rules/function-type.js | 4 ++-- rules/utils/utils.js | 4 ++++ test/function-type.js | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/rules/function-type.js b/rules/function-type.js index 75124353..0ae05129 100644 --- a/rules/function-type.js +++ b/rules/function-type.js @@ -21,8 +21,8 @@ module.exports = function(context) { } function checkType(arg) { - return (configType === 'named' && utils.isIdentifierType(arg)) || - (configType === 'anonymous' && utils.isFunctionType(arg)); + return (configType === 'named' && (utils.isIdentifierType(arg) || utils.isNamedInlineFunction(arg))) || + (configType === 'anonymous' && utils.isFunctionType(arg) && !utils.isNamedInlineFunction(arg)); } return { diff --git a/rules/utils/utils.js b/rules/utils/utils.js index 12425125..227a0b5c 100644 --- a/rules/utils/utils.js +++ b/rules/utils/utils.js @@ -56,6 +56,10 @@ module.exports = { return node !== undefined && node.type === 'FunctionExpression'; }, + isNamedInlineFunction: function (node) { + return this.isFunctionType(node) && node.id && node.id.name && node.id.name.length > 0; + }, + isIdentifierType: function(node) { return node !== undefined && node.type === 'Identifier'; }, diff --git a/test/function-type.js b/test/function-type.js index e7b884cf..c43f111e 100644 --- a/test/function-type.js +++ b/test/function-type.js @@ -39,6 +39,10 @@ angularObjectList.forEach(function(object) { code: 'function func(Service1) {};app.' + object + '("name", ["Service1", func]);', options: ['anonymous'], errors: [{message: 'Use anonymous functions instead of named function'}] + }, { + code: 'angular.module("myModule").' + object + '("myService", function myService($http, $log) {});', + options: ['anonymous'], + errors: [{message: 'Use anonymous functions instead of named function'}] }); valid.push({ @@ -48,7 +52,7 @@ angularObjectList.forEach(function(object) { code: 'function func(Service1) {};app.' + object + '("name", ["Service1", func]);', options: ['named'] }, { - code: 'angular.module("myModule").factory("myService", function myService($http, $log) {});', + code: 'angular.module("myModule").' + object + '("myService", function myService($http, $log) {});', options: ['named'] }); }); From 2dc538b09e57f2d37d057425412380aa7c455b81 Mon Sep 17 00:00:00 2001 From: Tilman Potthof Date: Tue, 13 Oct 2015 18:50:45 +0200 Subject: [PATCH 3/6] Improve json schema for function-type (#193) --- rules/function-type.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rules/function-type.js b/rules/function-type.js index 0ae05129..c1af5111 100644 --- a/rules/function-type.js +++ b/rules/function-type.js @@ -51,7 +51,13 @@ module.exports = function(context) { }; module.exports.schema = [{ - type: 'string' + enum: [ + 'named', + 'anonymous' + ] }, { - type: 'array' + type: 'array', + items: { + type: 'string' + } }]; From 3f74d7d372fa12d7da475508b13360724cad0542 Mon Sep 17 00:00:00 2001 From: Tilman Potthof Date: Tue, 13 Oct 2015 18:52:24 +0200 Subject: [PATCH 4/6] Remove unnecessary array check and use hash for messages (#229) --- rules/function-type.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/rules/function-type.js b/rules/function-type.js index c1af5111..e97a867f 100644 --- a/rules/function-type.js +++ b/rules/function-type.js @@ -4,22 +4,16 @@ module.exports = function(context) { var utils = require('./utils/utils'); var angularObjectList = ['controller', 'filter', 'factory', 'service']; var configType = context.options[0]; - var message; - - function isArray(item) { - return Object.prototype.toString.call(item) === '[object Array]'; - } + var messageByConfigType = { + anonymous: 'Use anonymous functions instead of named function', + named: 'Use named functions instead of anonymous function' + }; + var message = messageByConfigType[configType]; - if (isArray(context.options[1])) { + if (context.options[1]) { angularObjectList = context.options[1]; } - if (configType === 'anonymous') { - message = 'Use anonymous functions instead of named function'; - } else if (configType === 'named') { - message = 'Use named functions instead of anonymous function'; - } - function checkType(arg) { return (configType === 'named' && (utils.isIdentifierType(arg) || utils.isNamedInlineFunction(arg))) || (configType === 'anonymous' && utils.isFunctionType(arg) && !utils.isNamedInlineFunction(arg)); From a5383dc40b1abfc6b23750d5038e36f4e69ff72c Mon Sep 17 00:00:00 2001 From: Tilman Potthof Date: Tue, 13 Oct 2015 18:52:39 +0200 Subject: [PATCH 5/6] Fix eslint issue --- rules/utils/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/utils/utils.js b/rules/utils/utils.js index 227a0b5c..a5d5b48b 100644 --- a/rules/utils/utils.js +++ b/rules/utils/utils.js @@ -56,7 +56,7 @@ module.exports = { return node !== undefined && node.type === 'FunctionExpression'; }, - isNamedInlineFunction: function (node) { + isNamedInlineFunction: function(node) { return this.isFunctionType(node) && node.id && node.id.name && node.id.name.length > 0; }, From b4ec24bd857f5879e4c53d54001e66a9df2c078d Mon Sep 17 00:00:00 2001 From: Tilman Potthof Date: Tue, 13 Oct 2015 19:09:47 +0200 Subject: [PATCH 6/6] Add isNamedInlineFunction to module.exports (#229) --- rules/utils/utils.js | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/utils/utils.js b/rules/utils/utils.js index 8e6df534..a9e14bab 100644 --- a/rules/utils/utils.js +++ b/rules/utils/utils.js @@ -32,6 +32,7 @@ module.exports = { isToStringStatement: isToStringStatement, isArrayType: isArrayType, isFunctionType: isFunctionType, + isNamedInlineFunction: isNamedInlineFunction, isIdentifierType: isIdentifierType, isMemberExpression: isMemberExpression, isLiteralType: isLiteralType,