Skip to content

Commit 2009670

Browse files
Merge pull request #482 from Gillespie59/development
2.5.0
2 parents d47073a + 3bd37ea commit 2009670

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-angular",
3-
"version": "2.4.2",
3+
"version": "2.5.0",
44
"description": "ESLint rules for AngularJS projects",
55
"main": "index.js",
66
"scripts": {

rules/di.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ module.exports = {
2727
properties: {
2828
matchNames: {
2929
type: 'boolean'
30+
},
31+
stripUnderscores: {
32+
type: 'boolean'
3033
}
3134
}
3235
}]
@@ -36,6 +39,7 @@ module.exports = {
3639

3740
var extra = context.options[1] || {};
3841
var matchNames = extra.matchNames !== false;
42+
var stripUnderscores = extra.stripUnderscores === true;
3943

4044
function report(node) {
4145
context.report(node, 'You should use the {{syntax}} syntax for DI', {
@@ -53,6 +57,12 @@ module.exports = {
5357
}
5458
}
5559

60+
function normalizeParameter(param) {
61+
return param.replace(/^_(.+)_$/, function(match, p1) {
62+
return p1;
63+
});
64+
}
65+
5666
function checkDi(callee, fn) {
5767
if (!fn) {
5868
return;
@@ -67,7 +77,13 @@ module.exports = {
6777

6878
if (matchNames) {
6979
var invalidArray = fn.params.filter(function(e, i) {
70-
return e.name !== fn.parent.elements[i].value;
80+
var name = e.name;
81+
82+
if (stripUnderscores) {
83+
name = normalizeParameter(name);
84+
}
85+
86+
return name !== fn.parent.elements[i].value;
7187
});
7288
if (invalidArray.length > 0) {
7389
context.report(fn, 'You have an error in your DI configuration. Each items of the array should match exactly one function parameter', {});
@@ -100,7 +116,13 @@ module.exports = {
100116

101117
if (matchNames) {
102118
var invalidInjectArray = fn.params.filter(function(e, i) {
103-
return e.name !== $injectArray.elements[i].value;
119+
var name = e.name;
120+
121+
if (stripUnderscores) {
122+
name = normalizeParameter(name);
123+
}
124+
125+
return name !== $injectArray.elements[i].value;
104126
});
105127
if (invalidInjectArray.length > 0) {
106128
context.report(fn, 'You have an error in your DI configuration. Each items of the array should match exactly one function parameter', {});

test/di.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ angularObjectList.forEach(function(object) {
3535
}, {
3636
code: 'angular.module("myModule").' + object + '(["$log", function($l) {}]);',
3737
options: ['array', {matchNames: false}]
38+
}, {
39+
code: 'angular.module("myModule").' + object + '(["$log", function($log) {}]);',
40+
options: ['array', {matchNames: true, stripUnderscores: false}]
41+
}, {
42+
code: 'angular.module("myModule").' + object + '(["$log", function(_$log_) {}]);',
43+
options: ['array', {matchNames: true, stripUnderscores: true}]
3844
}, {
3945
code: 'angular.module("myModule").' + object + '(myFunction);function MyFunction() {}',
4046
options: ['function']
@@ -50,6 +56,9 @@ angularObjectList.forEach(function(object) {
5056
}, {
5157
code: 'angular.module("myModule").' + object + '(myFunction);myFunction["$inject"]=["slMathPi"];function myFunction(pi) {}',
5258
options: ['$inject', {matchNames: false}]
59+
}, {
60+
code: 'angular.module("myModule").' + object + '(myFunction);myFunction["$inject"]=["slMathPi"];function myFunction(_slMathPi_) {}',
61+
options: ['$inject', {matchNames: true, stripUnderscores: true}]
5362
}, {
5463
code: 'myFunction.$inject=[];function myFunction() {} angular.module("myModule").' + object + '(myFunction);',
5564
options: ['$inject']
@@ -109,6 +118,10 @@ angularObjectList.forEach(function(object) {
109118
code: 'angular.module("myModule").' + object + '(["$urlRouteProvider", "$timeout", function($timeout, $urlRouteProvider){}])',
110119
options: ['array'],
111120
errors: [{message: 'You have an error in your DI configuration. Each items of the array should match exactly one function parameter'}]
121+
}, {
122+
code: 'angular.module("myModule").' + object + '(["$log", function(_$log_) {}]);',
123+
options: ['array', {matchNames: true, stripUnderscores: false}],
124+
errors: [{message: 'You have an error in your DI configuration. Each items of the array should match exactly one function parameter'}]
112125
});
113126
});
114127

0 commit comments

Comments
 (0)