-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathrequire-super-in-lifecycle-hooks.js
More file actions
273 lines (243 loc) · 7.42 KB
/
require-super-in-lifecycle-hooks.js
File metadata and controls
273 lines (243 loc) · 7.42 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'use strict';
const ember = require('../utils/ember');
const types = require('../utils/types');
const estraverse = require('estraverse');
function hasMatchingNode(node, matcher) {
let foundMatch = false;
estraverse.traverse(node, {
enter(child) {
if (!foundMatch && matcher(child)) {
foundMatch = true;
}
},
fallback: 'iteration',
});
return foundMatch;
}
/**
* Checks for this._super() call.
* @param {node} node
* @returns {Boolean}
*/
function isClassicSuper(node) {
return (
types.isCallExpression(node) &&
types.isMemberExpression(node.callee) &&
types.isThisExpression(node.callee.object) &&
types.isIdentifier(node.callee.property) &&
node.callee.property.name === '_super'
);
}
/**
* Checks for a call like super.init() or super.didInsertElement().
* @param {node} node
* @param {string} hook - name of hook
* @returns {Boolean}
*/
function isNativeSuper(node, hook) {
return (
types.isCallExpression(node) &&
types.isMemberExpression(node.callee) &&
node.callee.object.type === 'Super' &&
types.isIdentifier(node.callee.property) &&
node.callee.property.name === hook &&
(hook !== 'init' || node.arguments.length > 0)
);
}
//----------------------------------------------
// General rule - Call super in lifecycle hooks
//----------------------------------------------
const ERROR_MESSAGE = 'Call super in lifecycle hooks';
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
ERROR_MESSAGE,
meta: {
type: 'problem',
docs: {
description: 'require super to be called in lifecycle hooks',
category: 'Ember Object',
recommended: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/require-super-in-lifecycle-hooks.md',
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
checkInitOnly: {
type: 'boolean',
default: false,
},
checkNativeClasses: {
type: 'boolean',
default: true,
},
},
additionalProperties: false,
},
],
},
create(context) {
const checkInitOnly = context.options[0] && context.options[0].checkInitOnly;
const checkNativeClasses = !context.options[0] || context.options[0].checkNativeClasses;
function report(node, isNativeClass, lifecycleHookName) {
context.report({
node,
message: ERROR_MESSAGE,
fix(fixer) {
// attrs hooks should call super without ...arguments to satisfy ember/no-attrs-snapshot rule.
const replacementArgs = ['didReceiveAttrs', 'didUpdateAttrs'].includes(lifecycleHookName)
? ''
: '...arguments';
const replacement = isNativeClass
? `super.${lifecycleHookName}(${replacementArgs});`
: `this._super(${replacementArgs});`;
// Insert right after function curly brace.
const sourceCode = context.getSourceCode();
const startOfBlockStatement = sourceCode.getFirstToken(node.value.body);
return fixer.insertTextAfter(startOfBlockStatement, `\n${replacement}`);
},
});
}
function isLifecycleHook(node, isGlimmerComponent) {
return (
types.isIdentifier(node.key) &&
types.isFunctionExpression(node.value) &&
((checkInitOnly && node.key.name === 'init') ||
(!checkInitOnly &&
(node.key.name === 'init' ||
(!isGlimmerComponent && ember.isComponentLifecycleHook(node)) ||
(isGlimmerComponent && ember.isGlimmerComponentLifecycleHook(node)))))
);
}
function checkAndReport(
node,
isInEmberComponent,
isInEmberController,
isInEmberRoute,
isInEmberMixin,
isInEmberService,
isInGlimmerComponent,
isNativeClass
) {
const hookName = node.key.name;
if (
hookName === 'init' &&
!isInEmberComponent &&
!isInEmberController &&
!isInEmberRoute &&
!isInEmberMixin &&
!isInEmberService
) {
// Checking `init` hook but not inside any Ember class.
return;
} else if (
hookName !== 'init' &&
!isInEmberComponent &&
!isInEmberMixin &&
!isInGlimmerComponent
) {
// Checking a component lifecycle hook but not inside a component/mixin which could have them.
return;
}
const body = isNativeSuper ? node.value.body : node.body;
const hasSuper = hasMatchingNode(body, (bodyChild) =>
isNativeClass ? isNativeSuper(bodyChild, hookName) : isClassicSuper(bodyChild)
);
if (!hasSuper) {
report(node, isNativeClass, hookName);
}
}
let currentEmberComponent = null;
let currentEmberController = null;
let currentEmberRoute = null;
let currentEmberMixin = null;
let currentEmberService = null;
let currentGlimmerComponent = null;
return {
ClassDeclaration(node) {
if (ember.isEmberComponent(context, node)) {
currentEmberComponent = node;
} else if (ember.isEmberController(context, node)) {
currentEmberController = node;
} else if (ember.isEmberRoute(context, node)) {
currentEmberRoute = node;
} else if (ember.isEmberMixin(context, node)) {
currentEmberMixin = node;
} else if (ember.isEmberService(context, node)) {
currentEmberService = node;
} else if (ember.isGlimmerComponent(context, node)) {
currentGlimmerComponent = node;
}
},
'ClassDeclaration:exit'(node) {
switch (node) {
case currentEmberComponent: {
currentEmberComponent = null;
break;
}
case currentEmberController: {
currentEmberController = null;
break;
}
case currentEmberRoute: {
currentEmberRoute = null;
break;
}
case currentEmberMixin: {
currentEmberMixin = null;
break;
}
case currentEmberService: {
currentEmberService = null;
break;
}
case currentGlimmerComponent: {
currentGlimmerComponent = null;
break;
}
// No default
}
},
MethodDefinition(node) {
if (!checkNativeClasses) {
// Option off.
return;
}
if (!isLifecycleHook(node, currentGlimmerComponent)) {
return;
}
checkAndReport(
node,
currentEmberComponent,
currentEmberController,
currentEmberRoute,
currentEmberMixin,
currentEmberService,
currentGlimmerComponent,
true
);
},
Property(node) {
const parentParent = node.parent.parent;
if (!types.isCallExpression(parentParent)) {
// Not inside potential Ember class.
return;
}
if (!isLifecycleHook(node)) {
return;
}
checkAndReport(
node,
ember.isEmberComponent(context, parentParent),
ember.isEmberController(context, parentParent),
ember.isEmberRoute(context, parentParent),
ember.isEmberMixin(context, parentParent),
ember.isEmberService(context, parentParent),
false,
false
);
},
};
},
};