Skip to content

Commit 043df77

Browse files
committed
Merge pull request #235 from tilmanpotthof/issue-229/function-type-rule-does-not-recognize-inline-named-functions-properly
Issue 229/function type rule does not recognize inline named functions properly
2 parents 66a661b + b90be75 commit 043df77

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

rules/function-type.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@ module.exports = function(context) {
44
var utils = require('./utils/utils');
55
var angularObjectList = ['controller', 'filter', 'factory', 'service'];
66
var configType = context.options[0];
7-
var message;
8-
9-
function isArray(item) {
10-
return Object.prototype.toString.call(item) === '[object Array]';
11-
}
7+
var messageByConfigType = {
8+
anonymous: 'Use anonymous functions instead of named function',
9+
named: 'Use named functions instead of anonymous function'
10+
};
11+
var message = messageByConfigType[configType];
1212

13-
if (isArray(context.options[1])) {
13+
if (context.options[1]) {
1414
angularObjectList = context.options[1];
1515
}
1616

17-
if (configType === 'anonymous') {
18-
message = 'Use anonymous functions instead of named function';
19-
} else if (configType === 'named') {
20-
message = 'Use named functions instead of anonymous function';
21-
}
22-
2317
function checkType(arg) {
24-
return (configType === 'named' && utils.isIdentifierType(arg)) ||
25-
(configType === 'anonymous' && utils.isFunctionType(arg));
18+
return (configType === 'named' && (utils.isIdentifierType(arg) || utils.isNamedInlineFunction(arg))) ||
19+
(configType === 'anonymous' && utils.isFunctionType(arg) && !utils.isNamedInlineFunction(arg));
2620
}
2721

2822
return {
@@ -51,7 +45,13 @@ module.exports = function(context) {
5145
};
5246

5347
module.exports.schema = [{
54-
type: 'string'
48+
enum: [
49+
'named',
50+
'anonymous'
51+
]
5552
}, {
56-
type: 'array'
53+
type: 'array',
54+
items: {
55+
type: 'string'
56+
}
5757
}];

rules/utils/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
isToStringStatement: isToStringStatement,
3333
isArrayType: isArrayType,
3434
isFunctionType: isFunctionType,
35+
isNamedInlineFunction: isNamedInlineFunction,
3536
isIdentifierType: isIdentifierType,
3637
isMemberExpression: isMemberExpression,
3738
isLiteralType: isLiteralType,
@@ -149,6 +150,16 @@ function isFunctionType(node) {
149150
return node !== undefined && node.type === 'FunctionExpression';
150151
}
151152

153+
/**
154+
* Check whether or not a node is an named FunctionExpression.
155+
*
156+
* @param {Object} node The node to check.
157+
* @returns {boolean} Whether or not the node is an named FunctionExpression.
158+
*/
159+
function isNamedInlineFunction(node) {
160+
return this.isFunctionType(node) && node.id && node.id.name && node.id.name.length > 0;
161+
}
162+
152163
/**
153164
* Check whether or not a node is an Identifier.
154165
*

test/function-type.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ angularObjectList.forEach(function(object) {
3939
code: 'function func(Service1) {};app.' + object + '("name", ["Service1", func]);',
4040
options: ['anonymous'],
4141
errors: [{message: 'Use anonymous functions instead of named function'}]
42+
}, {
43+
code: 'angular.module("myModule").' + object + '("myService", function myService($http, $log) {});',
44+
options: ['anonymous'],
45+
errors: [{message: 'Use anonymous functions instead of named function'}]
4246
});
4347

4448
valid.push({
@@ -47,6 +51,9 @@ angularObjectList.forEach(function(object) {
4751
}, {
4852
code: 'function func(Service1) {};app.' + object + '("name", ["Service1", func]);',
4953
options: ['named']
54+
}, {
55+
code: 'angular.module("myModule").' + object + '("myService", function myService($http, $log) {});',
56+
options: ['named']
5057
});
5158
});
5259

0 commit comments

Comments
 (0)