UNPKG

3.98 kBJavaScriptView Raw
1/**
2 * require and specify a prefix for all service names
3 *
4 * All your services 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 services by "$" (reserved keyword for AngularJS services) ("service-name": [2, "ng"])
7 **
8 * @styleguideReference {johnpapa} `y125` Naming - Factory and Service Names
9 * @version 0.1.0
10 * @category naming
11 * @sinceAngularVersion 1.x
12 */
13'use strict';
14
15
16var utils = require('./utils/utils');
17
18/**
19 * @param {Array.<*>} options
20 * @returns {?string}
21 */
22function getPrefixFromOptions(options) {
23 return options.find(function(option) {
24 return ['String', 'RegExp', 'Null', 'Undefined'].indexOf(utils.getToStringTagType(option)) !== -1;
25 });
26}
27
28/**
29 * @param {Array.<*>} options
30 * @returns {Object}
31 */
32function getConfig(options) {
33 var config = options.find(function(option) {
34 return utils.getToStringTagType(option) === 'Object';
35 });
36
37 config = config || {};
38 if (typeof config.oldBehavior !== 'boolean') {
39 config = Object.assign({
40 oldBehavior: true
41 });
42 }
43
44 return config;
45}
46
47/**
48 * Used only by `ForDeprecatedBehavior()` for making sure it was run only one time
49 * @type {boolean}
50 */
51var didWarnForDeprecatedBehavior = false;
52
53/**
54 * Warn if API is deprecated
55 * @param {Array.<*>} options
56 */
57function warnForDeprecatedBehavior(options) {
58 if (didWarnForDeprecatedBehavior) {
59 return;
60 }
61 didWarnForDeprecatedBehavior = true;
62
63 var config = getConfig(options);
64
65 /* istanbul ignore if */
66 if (config.oldBehavior) {
67 // eslint-disable-next-line
68 console.warn('The rule `angular/service-name` will be split up to different rules in the next version. Please read the docs for more information');
69 }
70}
71
72module.exports = {
73 meta: {
74 schema: [{
75 type: ['string', 'object']
76 }, {
77 type: 'object'
78 }]
79 },
80 create: function(context) {
81 warnForDeprecatedBehavior(context.options);
82
83 return {
84
85 CallExpression: function(node) {
86 var config = getConfig(context.options);
87 var prefix = getPrefixFromOptions(context.options);
88 var convertedPrefix; // convert string from JSON .eslintrc to regex
89 var isService;
90
91 if (prefix === undefined) {
92 return;
93 }
94
95 convertedPrefix = utils.convertPrefixToRegex(prefix);
96
97 if (config.oldBehavior) {
98 isService = utils.isAngularServiceDeclarationDeprecated(node);
99 } else {
100 isService = utils.isAngularServiceDeclaration(node);
101 }
102
103 if (isService) {
104 var name = node.arguments[0].value;
105
106 if (name !== undefined && name.indexOf('$') === 0) {
107 context.report(node, 'The {{service}} service should not start with "$". This is reserved for AngularJS services', {
108 service: name
109 });
110 } else if (name !== undefined && !convertedPrefix.test(name)) {
111 if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
112 context.report(node, 'The {{service}} service should be prefixed by {{prefix}}', {
113 service: name,
114 prefix: prefix
115 });
116 } else {
117 context.report(node, 'The {{service}} service should follow this pattern: {{prefix}}', {
118 service: name,
119 prefix: prefix.toString()
120 });
121 }
122 }
123 }
124 }
125 };
126 }
127};