UNPKG

2.49 kBJavaScriptView Raw
1/**
2 * require and specify a prefix for all constant names
3 *
4 * All your constants 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 constants by "$" (reserved keyword for AngularJS services) ("constant-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
15var utils = require('./utils/utils');
16
17
18module.exports = {
19 meta: {
20 docs: {
21 url: 'https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/constant-name.md'
22 },
23 schema: [{
24 type: ['string', 'object']
25 }, {
26 type: 'object'
27 }]
28 },
29 create: function(context) {
30 return {
31
32 CallExpression: function(node) {
33 var prefix = context.options[0];
34 var convertedPrefix; // convert string from JSON .eslintrc to regex
35 var isConstant;
36
37 if (prefix === undefined) {
38 return;
39 }
40
41 convertedPrefix = utils.convertPrefixToRegex(prefix);
42 isConstant = utils.isAngularConstantDeclaration(node);
43
44 if (isConstant) {
45 var name = node.arguments[0].value;
46
47 if (name !== undefined && name.indexOf('$') === 0) {
48 context.report(node, 'The {{constant}} constant should not start with "$". This is reserved for AngularJS services', {
49 constant: name
50 });
51 } else if (name !== undefined && !convertedPrefix.test(name)) {
52 if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
53 context.report(node, 'The {{constant}} constant should be prefixed by {{prefix}}', {
54 constant: name,
55 prefix: prefix
56 });
57 } else {
58 context.report(node, 'The {{constant}} constant should follow this pattern: {{prefix}}', {
59 constant: name,
60 prefix: prefix.toString()
61 });
62 }
63 }
64 }
65 }
66 };
67 }
68};