UNPKG

2.26 kBJavaScriptView Raw
1/**
2 * require and specify a prefix for all value names
3 *
4 * All your values 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 values by "$" (reserved keyword for AngularJS services) ("value-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 schema: [{
21 type: ['string', 'object']
22 }]
23 },
24 create: function(context) {
25 return {
26
27 CallExpression: function(node) {
28 var prefix = context.options[0];
29 var convertedPrefix; // convert string from JSON .eslintrc to regex
30 var isValue;
31
32 if (prefix === undefined) {
33 return;
34 }
35
36 convertedPrefix = utils.convertPrefixToRegex(prefix);
37 isValue = utils.isAngularValueDeclaration(node);
38
39 if (isValue) {
40 var name = node.arguments[0].value;
41
42 if (name !== undefined && name.indexOf('$') === 0) {
43 context.report(node, 'The {{value}} value should not start with "$". This is reserved for AngularJS services', {
44 value: name
45 });
46 } else if (name !== undefined && !convertedPrefix.test(name)) {
47 if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
48 context.report(node, 'The {{value}} value should be prefixed by {{prefix}}', {
49 value: name,
50 prefix: prefix
51 });
52 } else {
53 context.report(node, 'The {{value}} value should follow this pattern: {{prefix}}', {
54 value: name,
55 prefix: prefix.toString()
56 });
57 }
58 }
59 }
60 }
61 };
62 }
63};