-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathdi.js
163 lines (142 loc) · 5.56 KB
/
di.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* require a consistent DI syntax
*
* All your DI should use the same syntax : the Array, function, or $inject syntaxes ("di": [2, "array, function, or $inject"])
*
* @version 0.1.0
* @category conventions
* @sinceAngularVersion 1.x
*/
'use strict';
var utils = require('./utils/utils');
var angularRule = require('./utils/angular-rule');
module.exports = {
meta: {
docs: {
url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/di.md'
},
schema: [{
enum: [
'function',
'array',
'$inject'
]
}, {
type: 'object',
properties: {
matchNames: {
type: 'boolean'
},
stripUnderscores: {
type: 'boolean'
}
}
}]
},
create: angularRule(function(context) {
var syntax = context.options[0] || 'function';
var extra = context.options[1] || {};
var matchNames = extra.matchNames !== false;
var stripUnderscores = extra.stripUnderscores === true;
function report(node) {
context.report(node, 'You should use the {{syntax}} syntax for DI', {
syntax: syntax
});
}
var $injectProperties = {};
function maybeNoteInjection(node) {
if (syntax === '$inject' && node.left && node.left.property &&
((utils.isLiteralType(node.left.property) && node.left.property.value === '$inject') ||
(utils.isIdentifierType(node.left.property) && node.left.property.name === '$inject'))) {
$injectProperties[node.left.object.name] = node.right;
}
}
function normalizeParameter(param) {
return param.replace(/^_(.+)_$/, function(match, p1) {
return p1;
});
}
function checkDi(callee, fn) {
if (!fn || !fn.params) {
return;
}
if (syntax === 'array') {
if (utils.isArrayType(fn.parent)) {
if (fn.parent.elements.length - 1 !== fn.params.length) {
context.report(fn, 'The signature of the method is incorrect', {});
return;
}
if (matchNames) {
var invalidArray = fn.params.filter(function(e, i) {
var name = e.name;
if (stripUnderscores) {
name = normalizeParameter(name);
}
return name !== fn.parent.elements[i].value;
});
if (invalidArray.length > 0) {
context.report(fn, 'You have an error in your DI configuration. Each items of the array should match exactly one function parameter', {});
return;
}
}
} else {
if (fn.params.length === 0) {
return;
}
report(fn);
}
}
if (syntax === 'function') {
if (utils.isArrayType(fn.parent)) {
report(fn);
}
}
if (syntax === '$inject') {
if (fn && fn.id && utils.isIdentifierType(fn.id)) {
var $injectArray = $injectProperties[fn.id.name];
if ($injectArray && utils.isArrayType($injectArray)) {
if ($injectArray.elements.length !== fn.params.length) {
context.report(fn, 'The signature of the method is incorrect', {});
return;
}
if (matchNames) {
var invalidInjectArray = fn.params.filter(function(e, i) {
var name = e.name;
if (stripUnderscores) {
name = normalizeParameter(name);
}
return name !== $injectArray.elements[i].value;
});
if (invalidInjectArray.length > 0) {
context.report(fn, 'You have an error in your DI configuration. Each items of the array should match exactly one function parameter', {});
return;
}
}
} else if (fn.params.length > 0) {
report(fn);
}
} else if (fn.params && fn.params.length !== 0) {
report(fn);
}
}
}
return {
'angular?animation': checkDi,
'angular?config': checkDi,
'angular?controller': checkDi,
'angular?directive': checkDi,
'angular?factory': checkDi,
'angular?filter': checkDi,
'angular?inject': checkDi,
'angular?run': checkDi,
'angular?service': checkDi,
'angular?provider': function(callee, providerFn, $get) {
checkDi(null, providerFn);
checkDi(null, $get);
},
AssignmentExpression: function(node) {
maybeNoteInjection(node);
}
};
})
};