1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | 'use strict';
|
14 |
|
15 |
|
16 | var utils = require('./utils/utils');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function getPrefixFromOptions(options) {
|
23 | return options.find(function(option) {
|
24 | return ['String', 'RegExp', 'Null', 'Undefined'].indexOf(utils.getToStringTagType(option)) !== -1;
|
25 | });
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function 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 |
|
49 |
|
50 |
|
51 | var didWarnForDeprecatedBehavior = false;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function warnForDeprecatedBehavior(options) {
|
58 | if (didWarnForDeprecatedBehavior) {
|
59 | return;
|
60 | }
|
61 | didWarnForDeprecatedBehavior = true;
|
62 |
|
63 | var config = getConfig(options);
|
64 |
|
65 |
|
66 | if (config.oldBehavior) {
|
67 |
|
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 |
|
72 | module.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;
|
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 | };
|