forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-implicit-injections.js
242 lines (214 loc) · 7.16 KB
/
no-implicit-injections.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
'use strict';
const assert = require('assert');
const emberUtils = require('../utils/ember');
const { getImportIdentifier } = require('../utils/import');
const Stack = require('../utils/stack');
const types = require('../utils/types');
const camelCase = require('lodash.camelcase');
const defaultServiceConfig = { service: 'store', moduleNames: ['Route', 'Controller'] };
const MODULE_TYPES = [
'Component',
'GlimmerComponent',
'Controller',
'Mixin',
'Route',
'Service',
'ArrayProxy',
'ObjectProxy',
'EmberObject',
'Helper',
];
// ----- -------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
function fixService(fixer, currentClass, serviceInjectImportName, failedConfig) {
const serviceInjectPath = failedConfig.service;
return currentClass.node.type === 'CallExpression'
? fixer.insertTextBefore(
currentClass.node.arguments[0].properties[0],
`${failedConfig.propertyName}: ${serviceInjectImportName}('${serviceInjectPath}'),\n`
)
: fixer.insertTextBefore(
currentClass.node.body.body[0],
`@${serviceInjectImportName}('${serviceInjectPath}') ${failedConfig.propertyName};\n`
);
}
function normalizeConfiguration(denyList) {
return denyList.map((config) => ({
service: config.service,
propertyName: config.propertyName ?? camelCase(config.service),
moduleNames: config.moduleNames ?? MODULE_TYPES,
}));
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce usage of implicit service injections',
category: 'Deprecations',
recommended: false,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-implicit-injections.md',
},
fixable: 'code',
schema: [
{
type: 'object',
required: ['denyList'],
properties: {
denyList: {
minItems: 1,
type: 'array',
items: {
type: 'object',
default: [defaultServiceConfig],
required: ['service'],
properties: {
service: {
type: 'string',
minLength: 1,
},
propertyName: {
type: 'string',
minLength: 1,
},
moduleNames: {
type: 'array',
items: {
enum: MODULE_TYPES,
},
},
},
additionalProperties: false,
},
},
},
additionalProperties: false,
},
],
messages: {
main: 'Do not rely on implicit service injections for the "{{serviceName}}" service. Implicit service injections were deprecated in Ember 3.26 and will not work in 4.0.',
},
},
create(context) {
const options = context.options[0] || {
denyList: [defaultServiceConfig],
};
const denyList = options.denyList || [defaultServiceConfig];
for (const config of denyList) {
assert(
emberUtils.convertServiceNameToKebabCase(config.service) === config.service,
'Service name should be passed in kebab-case (all lower case)'
);
assert(
!config.service.includes('/') || config.propertyName,
'Nested services must declare a property name'
);
}
// State being tracked for this file.
let serviceInjectImportName = undefined;
const normalizedConfiguration = normalizeConfiguration(denyList);
// State being tracked for the current class we're inside.
const classStack = new Stack();
// Array of posible types that could declare existing properties on native or legacy modules
const propertyDefintionTypes = new Set([
'Property',
'ClassProperty',
'PropertyDefinition',
'MethodDefinition',
]);
function onModuleFound(node) {
// Get the name of services for the current module type
let configToCheckFor = normalizedConfiguration.filter((serviceConfig) => {
return (
serviceConfig.moduleNames === undefined ||
serviceConfig.moduleNames.some((moduleName) =>
emberUtils.isEmberCoreModule(context, node, moduleName)
)
);
});
const modulePropertyDeclarations =
node.type === 'CallExpression' ? node.arguments[0].properties : node.body.body;
// Get Services that don't have properties/service injections declared
configToCheckFor = modulePropertyDeclarations.reduce((accum, n) => {
if (propertyDefintionTypes.has(n.type)) {
return accum.filter((config) => !(config.propertyName === n.key.name));
}
return accum;
}, configToCheckFor);
classStack.push({
node,
isEmberModule: true,
configToCheckFor,
});
}
function onClassEnter(node) {
if (emberUtils.isAnyEmberCoreModule(context, node)) {
onModuleFound(node);
} else {
classStack.push({
node,
isEmberModule: false,
});
}
}
function onClassExit(node) {
// Leaving current (native) class.
if (classStack.size() > 0 && classStack.peek().node === node) {
classStack.pop();
}
}
return {
ImportDeclaration(node) {
if (node.source.value === '@ember/service') {
serviceInjectImportName =
serviceInjectImportName || getImportIdentifier(node, '@ember/service', 'inject');
}
},
ClassDeclaration: onClassEnter,
ClassExpression: onClassEnter,
CallExpression(node) {
if (emberUtils.isAnyEmberCoreModule(context, node)) {
onModuleFound(node);
}
},
'ClassDeclaration:exit': onClassExit,
'ClassExpression:exit': onClassExit,
'CallExpression:exit': onClassExit,
MemberExpression(node) {
const currentClass = classStack.peek();
if (!currentClass || !currentClass.isEmberModule) {
return;
}
if (types.isThisExpression(node.object) && types.isIdentifier(node.property)) {
const failedConfig = currentClass.configToCheckFor.find(
(s) => s.propertyName === node.property.name
);
if (failedConfig) {
context.report({
node,
messageId: 'main',
data: {
serviceName: failedConfig.service,
},
fix(fixer) {
const sourceCode = context.getSourceCode();
// service inject is already declared
if (serviceInjectImportName) {
return fixService(fixer, currentClass, serviceInjectImportName, failedConfig);
}
return [
fixer.insertTextBefore(
sourceCode.ast,
"import { inject as service } from '@ember/service';\n"
),
fixService(fixer, currentClass, 'service', failedConfig),
];
},
});
}
}
},
};
},
};