UNPKG

2.28 kBJavaScriptView Raw
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 * @sinceAngularVersion 1.x
11 */
12'use strict';
13
14var utils = require('./utils/utils');
15
16module.exports = {
17 meta: {
18 schema: [{
19 type: ['string', 'object']
20 }]
21 },
22 create: function(context) {
23 if (context.settings.angular === 2) {
24 return {};
25 }
26
27 return {
28
29 CallExpression: function(node) {
30 var prefix = context.options[0];
31 var convertedPrefix; // convert string from JSON .eslintrc to regex
32
33 if (prefix === undefined) {
34 return;
35 }
36
37 convertedPrefix = utils.convertPrefixToRegex(prefix);
38
39 if (utils.isAngularComponentDeclaration(node)) {
40 var name = node.arguments[0].value;
41
42 if (name !== undefined && name.indexOf('ng') === 0) {
43 context.report(node, 'The {{component}} component should not start with "ng". This is reserved for AngularJS components', {
44 component: name
45 });
46 } else if (name !== undefined && !convertedPrefix.test(name)) {
47 if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
48 context.report(node, 'The {{component}} component should be prefixed by {{prefix}}', {
49 component: name,
50 prefix: prefix
51 });
52 } else {
53 context.report(node, 'The {{component}} component should follow this pattern: {{prefix}}', {
54 component: name,
55 prefix: prefix.toString()
56 });
57 }
58 }
59 }
60 }
61 };
62 }
63};