Skip to content

Commit cba4fa3

Browse files
Merge pull request #394 from Gillespie59/development
1.1.0
2 parents d37e718 + 016d5ab commit cba4fa3

File tree

11 files changed

+234
-8
lines changed

11 files changed

+234
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ These rules prevent you from using deprecated angular features.
135135
136136
These rules help you to specify several naming conventions.
137137
138+
* [component-name](docs/component-name.md) - require and specify a prefix for all component names
138139
* [controller-name](docs/controller-name.md) - require and specify a prefix for all controller names ([y123](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y123), [y124](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y124))
139140
* [directive-name](docs/directive-name.md) - require and specify a prefix for all directive names ([y073](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y073), [y126](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y126))
140141
* [file-name](docs/file-name.md) - require and specify a consistent component name pattern ([y120](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y120), [y121](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y121))

docs/component-name.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!-- WARNING: Generated documentation. Edit docs and examples in the rule and examples file ('rules/component-name.js', 'examples/component-name.js'). -->
2+
3+
# component-name - require and specify a prefix for all component names
4+
5+
All your components 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 components by "ng" (reserved keyword for AngularJS components) ("component-name": [2, "ng"])
8+
9+
## Examples
10+
11+
The following patterns are **not** considered problems when configured `"prefix"`:
12+
13+
/*eslint angular/component-name: [2,"prefix"]*/
14+
15+
// valid
16+
angular.module('myModule').component('prefixTabs', function () {
17+
// ...
18+
});
19+
20+
The following patterns are considered problems when configured `"/^ui/"`:
21+
22+
/*eslint angular/component-name: [2,"/^ui/"]*/
23+
24+
// invalid
25+
angular.module('myModule').component('navigation', function () {
26+
// ...
27+
}); // error: The navigation component should follow this pattern: /^ui/
28+
29+
The following patterns are **not** considered problems when configured `"/^ui/"`:
30+
31+
/*eslint angular/component-name: [2,"/^ui/"]*/
32+
33+
// valid
34+
angular.module('myModule').component('uiNavigation', function () {
35+
// ...
36+
});
37+
38+
The following patterns are considered problems when configured `"ui"`:
39+
40+
/*eslint angular/component-name: [2,"ui"]*/
41+
42+
// invalid
43+
angular.module('myModule').component('tabs', function () {
44+
// ...
45+
}); // error: The tabs component should be prefixed by ui
46+
47+
## Version
48+
49+
This rule was introduced in eslint-plugin-angular 0.1.0
50+
51+
## Links
52+
53+
* [Rule source](../rules/component-name.js)
54+
* [Example source](../examples/component-name.js)

examples/component-name.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// example - valid: true, options: ["prefix"]
2+
angular.module('myModule').component('prefixTabs', function () {
3+
// ...
4+
});
5+
6+
// example - valid: true, options: ["/^ui/"]
7+
angular.module('myModule').component('uiNavigation', function () {
8+
// ...
9+
});
10+
11+
// example - valid: false, options: ["ui"], errorMessage: "The tabs component should be prefixed by ui"
12+
angular.module('myModule').component('tabs', function () {
13+
// ...
14+
});
15+
16+
// example - valid: false, options: ["/^ui/"], errorMessage: "The navigation component should follow this pattern\: /^ui/"
17+
angular.module('myModule').component('navigation', function () {
18+
// ...
19+
});

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": "1.0.1",
3+
"version": "1.1.0",
44
"description": "ESLint rules for AngularJS projects",
55
"main": "index.js",
66
"scripts": {

rules/component-limit.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ module.exports = angularRule(function(context) {
4242
'angular:filter': checkLimit,
4343
'angular:provider': checkLimit,
4444
'angular:run': checkLimit,
45-
'angular:service': checkLimit
45+
'angular:service': checkLimit,
46+
'angular:component': checkLimit
4647
};
4748
});
4849

rules/component-name.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* require and specify a prefix for all component names
3+
*
4+
* All your components 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 components by "ng" (reserved keyword for AngularJS components) ("component-name": [2, "ng"])
7+
*
8+
* @version 0.1.0
9+
* @category naming
10+
*/
11+
'use strict';
12+
13+
var utils = require('./utils/utils');
14+
15+
module.exports = function(context) {
16+
if (context.settings.angular === 2) {
17+
return {};
18+
}
19+
20+
return {
21+
22+
CallExpression: function(node) {
23+
var prefix = context.options[0];
24+
var convertedPrefix; // convert string from JSON .eslintrc to regex
25+
26+
if (prefix === undefined) {
27+
return;
28+
}
29+
30+
convertedPrefix = utils.convertPrefixToRegex(prefix);
31+
32+
if (utils.isAngularComponentDeclaration(node)) {
33+
var name = node.arguments[0].value;
34+
35+
if (name !== undefined && name.indexOf('ng') === 0) {
36+
context.report(node, 'The {{component}} component should not start with "ng". This is reserved for AngularJS components', {
37+
component: name
38+
});
39+
} else if (name !== undefined && !convertedPrefix.test(name)) {
40+
if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
41+
context.report(node, 'The {{component}} component should be prefixed by {{prefix}}', {
42+
component: name,
43+
prefix: prefix
44+
});
45+
} else {
46+
context.report(node, 'The {{component}} component should follow this pattern: {{prefix}}', {
47+
component: name,
48+
prefix: prefix.toString()
49+
});
50+
}
51+
}
52+
}
53+
}
54+
};
55+
};

rules/controller-name.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = function(context) {
1818
return {
1919

2020
CallExpression: function(node) {
21-
var prefix = context.options[0] || '/[A-Z].*Controller$/';
21+
var prefix = context.options[0] || '/^[A-Z][a-zA-Z0-9]*Controller$/';
2222
var convertedPrefix; // convert string from JSON .eslintrc to regex
2323

2424
convertedPrefix = utils.convertPrefixToRegex(prefix);

rules/utils/angular-rule.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var angularChainableNames = [
3030
* module.exports = angularRule(function(context) {
3131
* return {
3232
* 'angular:animation': function(configCallee, configFn) {},
33+
* 'angular:component': function(componentCallee, componentObj) {},
3334
* 'angular:config': function(configCallee, configFn) {},
3435
* 'angular:controller': function(controllerCallee, controllerFn) {},
3536
* 'angular:directive': function(directiveCallee, directiveFn) {},
@@ -95,6 +96,8 @@ function angularRule(ruleDefinition) {
9596
* ^^^^^^
9697
* .animation('', function() {})
9798
* ^^^^^^^^^ ^^^^^^^^^^
99+
* .component('', {})
100+
* ^^^^^^^^^
98101
* .config(function() {})
99102
* ^^^^^^ ^^^^^^^^^^
100103
* .constant()
@@ -243,6 +246,7 @@ function angularRule(ruleDefinition) {
243246
function assembleArguments(node) {
244247
switch (node.callExpression.callee.property.name) {
245248
case 'animation':
249+
case 'component':
246250
case 'config':
247251
case 'controller':
248252
case 'directive':

rules/utils/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = {
3939
isEmptyFunction: isEmptyFunction,
4040
isStringRegexp: isStringRegexp,
4141
isAngularComponent: isAngularComponent,
42+
isAngularComponentDeclaration: isAngularComponentDeclaration,
4243
isAngularControllerDeclaration: isAngularControllerDeclaration,
4344
isAngularFilterDeclaration: isAngularFilterDeclaration,
4445
isAngularDirectiveDeclaration: isAngularDirectiveDeclaration,
@@ -241,6 +242,18 @@ function isAngularComponent(node) {
241242
isArrayType(node.arguments[1]));
242243
}
243244

245+
/**
246+
* Check whether a CallExpression node defines an Angular component.
247+
*
248+
* @param {Object} node The CallExpression node to check.
249+
* @returns {boolean} Whether or not the node defines an Angular component.
250+
*/
251+
function isAngularComponentDeclaration(node) {
252+
return isAngularComponent(node) &&
253+
isMemberExpression(node.callee) &&
254+
node.callee.property.name === 'component';
255+
}
256+
244257
/**
245258
* Check whether a CallExpression node defines an Angular controller.
246259
*

test/component-limit.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var eslintTester = new RuleTester();
1616
eslintTester.run('component-limit', rule, {
1717
valid: [
1818
'angular.module("").animation();',
19+
'angular.module("").component();',
1920
'angular.module("").config();',
2021
'angular.module("").controller();',
2122
'angular.module("").directive();',
@@ -42,8 +43,8 @@ eslintTester.run('component-limit', rule, {
4243
'describe("", function() {it("", function() {});it("", function() {});});',
4344
'angular.module("").service("", function() {});',
4445
{
45-
code: 'angular.module("").controller("", function() {}).directive("", function() {}).factory("", function() {}).filter("", function() {}).provider("", function() {}).service("", function() {});',
46-
options: [6]
46+
code: 'angular.module("").component("", {}).controller("", function() {}).directive("", function() {}).factory("", function() {}).filter("", function() {}).provider("", function() {}).service("", function() {});',
47+
options: [7]
4748
}
4849
].concat(commonFalsePositives),
4950
invalid: [
@@ -58,6 +59,17 @@ eslintTester.run('component-limit', rule, {
5859
code: 'var app = angular.module(""); app.animation().animation();',
5960
errors: [{message: 'There may be at most 1 AngularJS component per file, but found 2'}]
6061
},
62+
// directive
63+
{
64+
code: 'angular.module("").component().component();',
65+
errors: [{message: 'There may be at most 1 AngularJS component per file, but found 2'}]
66+
}, {
67+
code: 'var app = angular.module("").component(); app.component();',
68+
errors: [{message: 'There may be at most 1 AngularJS component per file, but found 2'}]
69+
}, {
70+
code: 'var app = angular.module(""); app.component().component();',
71+
errors: [{message: 'There may be at most 1 AngularJS component per file, but found 2'}]
72+
},
6173
// config
6274
{
6375
code: 'angular.module("").config().config();',
@@ -148,10 +160,10 @@ eslintTester.run('component-limit', rule, {
148160
},
149161
// Using non-default settings
150162
{
151-
code: 'angular.module("").controller("", function() {}).directive("", function() {}).factory("", function() {}).filter("", function() {}).provider("", function() {}).service("", function() {});',
152-
options: [5],
163+
code: 'angular.module("").component("", {}).controller("", function() {}).directive("", function() {}).factory("", function() {}).filter("", function() {}).provider("", function() {}).service("", function() {});',
164+
options: [6],
153165
errors: [{
154-
message: 'There may be at most 5 AngularJS components per file, but found 6'
166+
message: 'There may be at most 6 AngularJS components per file, but found 7'
155167
}]
156168
}
157169
]

0 commit comments

Comments
 (0)