Skip to content
32 changes: 16 additions & 16 deletions rules/function-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@ 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)) ||
(configType === 'anonymous' && utils.isFunctionType(arg));
return (configType === 'named' && (utils.isIdentifierType(arg) || utils.isNamedInlineFunction(arg))) ||
(configType === 'anonymous' && utils.isFunctionType(arg) && !utils.isNamedInlineFunction(arg));
}

return {
Expand Down Expand Up @@ -51,7 +45,13 @@ module.exports = function(context) {
};

module.exports.schema = [{
type: 'string'
enum: [
'named',
'anonymous'
]
}, {
type: 'array'
type: 'array',
items: {
type: 'string'
}
}];
11 changes: 11 additions & 0 deletions rules/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
isToStringStatement: isToStringStatement,
isArrayType: isArrayType,
isFunctionType: isFunctionType,
isNamedInlineFunction: isNamedInlineFunction,
isIdentifierType: isIdentifierType,
isMemberExpression: isMemberExpression,
isLiteralType: isLiteralType,
Expand Down Expand Up @@ -149,6 +150,16 @@ function isFunctionType(node) {
return node !== undefined && node.type === 'FunctionExpression';
}

/**
* Check whether or not a node is an named FunctionExpression.
*
* @param {Object} node The node to check.
* @returns {boolean} Whether or not the node is an named FunctionExpression.
*/
function isNamedInlineFunction(node) {
return this.isFunctionType(node) && node.id && node.id.name && node.id.name.length > 0;
}

/**
* Check whether or not a node is an Identifier.
*
Expand Down
7 changes: 7 additions & 0 deletions test/function-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -47,6 +51,9 @@ angularObjectList.forEach(function(object) {
}, {
code: 'function func(Service1) {};app.' + object + '("name", ["Service1", func]);',
options: ['named']
}, {
code: 'angular.module("myModule").' + object + '("myService", function myService($http, $log) {});',
options: ['named']
});
});

Expand Down