forked from EmmanuelDemey/eslint-plugin-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component-name.js
66 lines (58 loc) · 2.37 KB
/
component-name.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
/**
* require and specify a prefix for all component names
*
* All your components should have a name starting with the parameter you can define in your config object.
* The second parameter can be a Regexp wrapped in quotes.
* You can not prefix your components by "ng" (reserved keyword for AngularJS components) ("component-name": [2, "ng"])
*
* @version 0.1.0
* @category naming
* @sinceAngularVersion 1.x
*/
'use strict';
var utils = require('./utils/utils');
module.exports = {
meta: {
docs: {
url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/component-name.md'
},
schema: [{
type: ['string', 'object']
}]
},
create: function(context) {
if (context.settings.angular === 2) {
return {};
}
return {
CallExpression: function(node) {
var prefix = context.options[0];
var convertedPrefix; // convert string from JSON .eslintrc to regex
if (prefix === undefined) {
return;
}
convertedPrefix = utils.convertPrefixToRegex(prefix);
if (utils.isAngularComponentDeclaration(node)) {
var name = node.arguments[0].value;
if (name !== undefined && name.indexOf('ng') === 0) {
context.report(node, 'The {{component}} component should not start with "ng". This is reserved for AngularJS components', {
component: name
});
} else if (name !== undefined && !convertedPrefix.test(name)) {
if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
context.report(node, 'The {{component}} component should be prefixed by {{prefix}}', {
component: name,
prefix: prefix
});
} else {
context.report(node, 'The {{component}} component should follow this pattern: {{prefix}}', {
component: name,
prefix: prefix.toString()
});
}
}
}
}
};
}
};