UNPKG

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