-
Notifications
You must be signed in to change notification settings - Fork 131
/
di-order.js
68 lines (64 loc) · 2.27 KB
/
di-order.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
/**
* require DI parameters to be sorted alphabetically
*
* Injected dependencies should be sorted alphabetically.
* If the second parameter is set to false, values which start and end with an underscore those underscores are stripped.
* This means for example that `_$httpBackend_` goes before `_$http_`.
*
* @version 0.6.0
* @category conventions
* @sinceAngularVersion 1.x
*/
'use strict';
var angularRule = require('./utils/angular-rule');
var caseSensitive = 'case_sensitive';
module.exports = {
meta: {
docs: {
url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/di-order.md'
},
schema: [{
type: 'boolean'
}, {
type: 'string'
}]
},
create: angularRule(function(context) {
var stripUnderscores = context.options[0] !== false;
var caseSensitiveOpt = (context.options[1] || caseSensitive) === caseSensitive;
function checkOrder(callee, fn) {
if (!fn || !fn.params) {
return;
}
var args = fn.params.map(function(arg) {
var formattedArg = arg.name;
if (stripUnderscores) {
formattedArg = formattedArg.replace(/^_(.+)_$/, '$1');
}
return caseSensitiveOpt ? formattedArg : formattedArg.toLowerCase();
});
var sortedArgs = args.slice().sort();
sortedArgs.some(function(value, index) {
if (args.indexOf(value) !== index) {
context.report(fn, 'Injected values should be sorted alphabetically');
return true;
}
});
}
return {
'angular?animation': checkOrder,
'angular?config': checkOrder,
'angular?controller': checkOrder,
'angular?directive': checkOrder,
'angular?factory': checkOrder,
'angular?filter': checkOrder,
'angular?inject': checkOrder,
'angular?run': checkOrder,
'angular?service': checkOrder,
'angular?provider': function(callee, providerFn, $get) {
checkOrder(null, providerFn);
checkOrder(null, $get);
}
};
})
};