Skip to content

Commit ca871f5

Browse files
committed
new rule provider-name
1 parent 3f8eac8 commit ca871f5

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

Diff for: docs/provider-name.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!-- WARNING: Generated documentation. Edit docs and examples in the rule and examples file ('rules/provider-name.js', 'examples/provider-name.js'). -->
2+
3+
# provider-name - require and specify a prefix for all provider names
4+
5+
All your providers should have a name starting with the parameter you can define in your config object.
6+
The second parameter can be a Regexp wrapped in quotes.
7+
You can not prefix your providers by "$" (reserved keyword for AngularJS services) ("provider-name": [2, "ng"])
8+
*
9+
10+
**Styleguide Reference**
11+
12+
* [y125 by johnpapa - Naming - Factory and Service Names](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y125)
13+
14+
## Examples
15+
16+
The following patterns are **not** considered problems when configured `"prefix"`:
17+
18+
/*eslint angular/provider-name: [2,"prefix"]*/
19+
20+
// valid
21+
angular.module('myModule').provider('prefixProvider', function () {
22+
// ...
23+
});
24+
25+
The following patterns are considered problems when configured `"/^xyz/"`:
26+
27+
/*eslint angular/provider-name: [2,"/^xyz/"]*/
28+
29+
// invalid
30+
angular.module('myModule').provider('otherProvider', function () {
31+
// ...
32+
}); // error: The otherProvider provider should follow this pattern: /^xyz/
33+
34+
The following patterns are **not** considered problems when configured `"/^xyz/"`:
35+
36+
/*eslint angular/provider-name: [2,"/^xyz/"]*/
37+
38+
// valid
39+
angular.module('myModule').provider('xyzProvider', function () {
40+
// ...
41+
});
42+
43+
The following patterns are considered problems when configured `"xyz"`:
44+
45+
/*eslint angular/provider-name: [2,"xyz"]*/
46+
47+
// invalid
48+
angular.module('myModule').provider('myProvider', function () {
49+
// ...
50+
}); // error: The myProvider provider should be prefixed by xyz
51+
52+
## Version
53+
54+
This rule was introduced in eslint-plugin-angular 1.4.1
55+
56+
## Links
57+
58+
* [Rule source](../rules/provider-name.js)
59+
* [Example source](../examples/provider-name.js)

Diff for: examples/provider-name.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// example - valid: true, options: ["prefix"]
2+
angular.module('myModule').provider('prefixProvider', function () {
3+
// ...
4+
});
5+
6+
// example - valid: true, options: ["/^xyz/"]
7+
angular.module('myModule').provider('xyzProvider', function () {
8+
// ...
9+
});
10+
11+
// example - valid: false, options: ["xyz"], errorMessage: "The myProvider provider should be prefixed by xyz"
12+
angular.module('myModule').provider('myProvider', function () {
13+
// ...
14+
});
15+
16+
// example - valid: false, options: ["/^xyz/"], errorMessage: "The otherProvider provider should follow this pattern\: /^xyz/"
17+
angular.module('myModule').provider('otherProvider', function () {
18+
// ...
19+
});

Diff for: rules/provider-name.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* require and specify a prefix for all provider names
3+
*
4+
* All your providers should have a name starting with the parameter you can define in your config object.
5+
* The second parameter can be a Regexp wrapped in quotes.
6+
* You can not prefix your providers by "$" (reserved keyword for AngularJS services) ("provider-name": [2, "ng"])
7+
**
8+
* @styleguideReference {johnpapa} `y125` Naming - Factory and Service Names
9+
* @version 0.1.0
10+
* @category naming
11+
*/
12+
'use strict';
13+
14+
15+
var utils = require('./utils/utils');
16+
17+
18+
module.exports = function(context) {
19+
return {
20+
21+
CallExpression: function(node) {
22+
var prefix = context.options[0];
23+
var convertedPrefix; // convert string from JSON .eslintrc to regex
24+
var isProvider;
25+
26+
if (prefix === undefined) {
27+
return;
28+
}
29+
30+
convertedPrefix = utils.convertPrefixToRegex(prefix);
31+
isProvider = utils.isAngularProviderDeclaration(node);
32+
33+
if (isProvider) {
34+
var name = node.arguments[0].value;
35+
36+
if (name !== undefined && name.indexOf('$') === 0) {
37+
context.report(node, 'The {{provider}} provider should not start with "$". This is reserved for AngularJS services', {
38+
provider: name
39+
});
40+
} else if (name !== undefined && !convertedPrefix.test(name)) {
41+
if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
42+
context.report(node, 'The {{provider}} provider should be prefixed by {{prefix}}', {
43+
provider: name,
44+
prefix: prefix
45+
});
46+
} else {
47+
context.report(node, 'The {{provider}} provider should follow this pattern: {{prefix}}', {
48+
provider: name,
49+
prefix: prefix.toString()
50+
});
51+
}
52+
}
53+
}
54+
}
55+
};
56+
};

Diff for: test/provider-name.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
// ------------------------------------------------------------------------------
4+
// Requirements
5+
// ------------------------------------------------------------------------------
6+
7+
var rule = require('../rules/provider-name');
8+
var RuleTester = require('eslint').RuleTester;
9+
var commonFalsePositives = require('./utils/commonFalsePositives');
10+
11+
// ------------------------------------------------------------------------------
12+
// Tests
13+
// ------------------------------------------------------------------------------
14+
15+
var eslintTester = new RuleTester();
16+
var valid = [];
17+
var invalid = [];
18+
19+
20+
/**
21+
* New Behaviour
22+
*/
23+
24+
valid.push({
25+
code: 'app.provider("eslintProvider", function() {});',
26+
options: ['eslint']
27+
}, {
28+
code: 'app.provider("eslintProvider", function() {});',
29+
options: [/^eslint/]
30+
}, {
31+
code: 'app.provider("eslintProvider", function() {});',
32+
options: [undefined]
33+
}, {
34+
code: 'app.provider("eslintProvider", function() {});',
35+
options: ['/^eslint/']
36+
});
37+
38+
invalid.push({
39+
code: 'app.provider("Provider", function() {});',
40+
options: ['eslint'],
41+
errors: [{message: 'The Provider provider should be prefixed by eslint'}]
42+
}, {
43+
code: 'app.provider("esLintProvider", function() {});',
44+
options: ['eslint'],
45+
errors: [{message: 'The esLintProvider provider should be prefixed by eslint'}]
46+
}, {
47+
code: 'app.provider("Provider", function() {});',
48+
options: [/^eslint/],
49+
errors: [{message: 'The Provider provider should follow this pattern: /^eslint/'}]
50+
}, {
51+
code: 'app.provider("Provider", function() {});',
52+
options: ['/^eslint/'],
53+
errors: [{message: 'The Provider provider should follow this pattern: /^eslint/'}]
54+
}, {
55+
code: 'app.provider("$Provider", function() {});',
56+
options: [/^eslint/],
57+
errors: [{message: 'The $Provider provider should not start with "$". This is reserved for AngularJS services'}]
58+
});
59+
60+
eslintTester.run('provider-name', rule, {
61+
valid: valid.concat(commonFalsePositives),
62+
invalid: invalid
63+
});

0 commit comments

Comments
 (0)