-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
no-implicit-injections.js
190 lines (169 loc) · 5.96 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
'use strict';
const emberUtils = require('../utils/ember');
const { getImportIdentifier } = require('../utils/import');
const Stack = require('../utils/stack');
const types = require('../utils/types');
const kebabCase = require('lodash.kebabcase');
const camelCase = require('lodash.camelcase');
const defaultServiceConfig = { serviceName: 'store', moduleNames: ['Route', 'Controller'] };
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
function fixService(fixer, currentClass, serviceInjectImportName, servicePropertyName) {
const serviceInjectPath = kebabCase(servicePropertyName);
return fixer.insertTextBefore(
currentClass.node.body.body[0],
`@${serviceInjectImportName}('${serviceInjectPath}') ${servicePropertyName};\n`
);
}
/** @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',
properties: {
services: {
minItems: 1,
type: 'array',
items: {
type: 'object',
default: [{ serviceName: 'store', moduleNames: ['Route', 'Controller'] }],
properties: {
serviceName: {
type: 'string',
},
moduleNames: {
type: 'array',
items: {
enum: [
'Component',
'GlimmerComponent',
'Controller',
'Mixin',
'Route',
'Service',
'ArrayProxy',
'ObjectProxy',
'EmberObject',
'Helper',
],
},
},
},
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] || {
services: [defaultServiceConfig],
};
const servicesConfig = options.services || [defaultServiceConfig];
// State being tracked for this file.
let serviceInjectImportName = undefined;
let serviceNamesWithImplicitInjection = servicesConfig;
let isEmberModule = false;
// State being tracked for the current class we're inside.
const classStack = new Stack();
// This rule does not apply to test files or non class modules
if (emberUtils.isTestFile(context.getFilename())) {
return {};
}
return {
ImportDeclaration(node) {
if (node.source.value === '@ember/service') {
serviceInjectImportName =
serviceInjectImportName || getImportIdentifier(node, '@ember/service', 'inject');
}
},
ClassDeclaration(node) {
if (emberUtils.isAnyEmberCoreModule(context, node)) {
isEmberModule = true;
// Get the name of services for the current module type
const serviceNames = servicesConfig
.filter((serviceConfig) => {
return (
serviceConfig.moduleNames === undefined ||
serviceConfig.moduleNames.some((moduleName) =>
emberUtils.isEmberCoreModule(context, node, moduleName)
)
);
})
.map((a) => camelCase(a.serviceName));
// Get Services that don't have properties/service injections declared
serviceNamesWithImplicitInjection = node.body.body.reduce((accum, n) => {
if (types.isClassPropertyOrPropertyDefinition(n)) {
return accum.filter((serviceName) => !(serviceName === n.key.name));
}
return accum;
}, serviceNames);
classStack.push({
node,
serviceNamesWithImplicitInjection,
});
}
},
'ClassDeclaration:exit'(node) {
// Leaving current (native) class.
if (classStack.size() > 0 && classStack.peek().node === node) {
classStack.pop();
}
},
MemberExpression(node) {
if (!isEmberModule) {
return;
}
if (types.isThisExpression(node.object) && types.isIdentifier(node.property)) {
const serviceMissingInjection = serviceNamesWithImplicitInjection.find(
(s) => s === node.property.name
);
if (serviceMissingInjection) {
context.report({
node,
messageId: 'main',
data: {
serviceName: serviceMissingInjection,
},
fix(fixer) {
const sourceCode = context.getSourceCode();
const currentClass = classStack.peek();
// service inject is already declared
if (serviceInjectImportName) {
return fixService(
fixer,
currentClass,
serviceInjectImportName,
serviceMissingInjection
);
}
return [
fixer.insertTextBefore(
sourceCode.ast,
"import { inject as service } from '@ember/service';\n"
),
fixService(fixer, currentClass, 'service', serviceMissingInjection),
];
},
});
}
}
},
};
},
};