UNPKG

2.43 kBJavaScriptView Raw
1/**
2 * require and specify a prefix for all directive names
3 *
4 * All your directives 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 directives by "ng" (reserved keyword for AngularJS directives) ("directive-name": [2, "ng"])
7 *
8 * @styleguideReference {johnpapa} `y073` Provide a Unique Directive Prefix
9 * @styleguideReference {johnpapa} `y126` Directive Component Names
10 * @version 0.1.0
11 * @category naming
12 * @sinceAngularVersion 1.x
13 */
14'use strict';
15
16var utils = require('./utils/utils');
17
18module.exports = {
19 meta: {
20 schema: [{
21 type: ['string', 'object']
22 }]
23 },
24 create: function(context) {
25 if (context.settings.angular === 2) {
26 return {};
27 }
28
29 return {
30
31 CallExpression: function(node) {
32 var prefix = context.options[0];
33 var convertedPrefix; // convert string from JSON .eslintrc to regex
34
35 if (prefix === undefined) {
36 return;
37 }
38
39 convertedPrefix = utils.convertPrefixToRegex(prefix);
40
41 if (utils.isAngularDirectiveDeclaration(node)) {
42 var name = node.arguments[0].value;
43
44 if (name !== undefined && name.indexOf('ng') === 0) {
45 context.report(node, 'The {{directive}} directive should not start with "ng". This is reserved for AngularJS directives', {
46 directive: name
47 });
48 } else if (name !== undefined && !convertedPrefix.test(name)) {
49 if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
50 context.report(node, 'The {{directive}} directive should be prefixed by {{prefix}}', {
51 directive: name,
52 prefix: prefix
53 });
54 } else {
55 context.report(node, 'The {{directive}} directive should follow this pattern: {{prefix}}', {
56 directive: name,
57 prefix: prefix.toString()
58 });
59 }
60 }
61 }
62 }
63 };
64 }
65};