-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathfunction-type.js
More file actions
87 lines (76 loc) · 3.08 KB
/
function-type.js
File metadata and controls
87 lines (76 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* require and specify a consistent function style for components
*
* Anonymous or named functions inside AngularJS components.
* The first parameter sets which type of function is required and can be 'named' or 'anonymous'.
* The second parameter is an optional list of angular object names.
*
* @linkDescription require and specify a consistent function style for components ('named' or 'anonymous')
* @styleguideReference {johnpapa} `y024` Named vs Anonymous Functions
* @version 0.1.0
* @category conventions
* @sinceAngularVersion 1.x
*/
'use strict';
var utils = require('./utils/utils');
module.exports = {
meta: {
docs: {
url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/function-type.md'
},
schema: [{
enum: [
'named',
'anonymous'
]
}, {
type: 'array',
items: {
type: 'string'
}
}]
},
create: function(context) {
var angularObjectList = ['animation', 'config', 'constant', 'controller', 'directive', 'factory', 'filter', 'provider', 'service', 'value', 'decorator'];
var reservedNameList = ['_'];
var configType = context.options[0] || 'named';
var messageByConfigType = {
anonymous: 'Use anonymous functions instead of named function',
named: 'Use named functions instead of anonymous function'
};
var message = messageByConfigType[configType];
if (context.options[1]) {
angularObjectList = context.options[1];
}
if (context.options[2]) {
reservedNameList = context.options[2];
}
function checkType(arg) {
return utils.isCallExpression(arg) ||
(configType === 'named' && (utils.isIdentifierType(arg) || utils.isNamedInlineFunction(arg))) ||
(configType === 'anonymous' && utils.isFunctionType(arg) && !utils.isNamedInlineFunction(arg));
}
return {
CallExpression: function(node) {
var callee = node.callee;
var angularObjectName = callee.property && callee.property.name;
var firstArgument = node.arguments[1];
if (utils.isAngularComponent(node) && callee.type === 'MemberExpression' && angularObjectList.indexOf(angularObjectName) >= 0) {
if (reservedNameList.indexOf(callee.object.name) !== -1) {
return;
}
if (checkType(firstArgument)) {
return;
}
if (utils.isArrayType(firstArgument)) {
var last = firstArgument.elements[firstArgument.elements.length - 1];
if (checkType(last) || (!utils.isFunctionType(last) && !utils.isIdentifierType(last))) {
return;
}
}
context.report(node, message, {});
}
}
};
}
};