no-this-before-super.js
9.93 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* @fileoverview A rule to disallow using `this`/`super` before `super()`.
* @author Toru Nagashima
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("../ast-utils");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Checks whether or not a given node is a constructor.
* @param {ASTNode} node - A node to check. This node type is one of
* `Program`, `FunctionDeclaration`, `FunctionExpression`, and
* `ArrowFunctionExpression`.
* @returns {boolean} `true` if the node is a constructor.
*/
function isConstructorFunction(node) {
return (
node.type === "FunctionExpression" &&
node.parent.type === "MethodDefinition" &&
node.parent.kind === "constructor"
);
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow `this`/`super` before calling `super()` in constructors",
category: "ECMAScript 6",
recommended: true
},
schema: []
},
create(context) {
/*
* Information for each constructor.
* - upper: Information of the upper constructor.
* - hasExtends: A flag which shows whether the owner class has a valid
* `extends` part.
* - scope: The scope of the owner class.
* - codePath: The code path of this constructor.
*/
let funcInfo = null;
/*
* Information for each code path segment.
* Each key is the id of a code path segment.
* Each value is an object:
* - superCalled: The flag which shows `super()` called in all code paths.
* - invalidNodes: The array of invalid ThisExpression and Super nodes.
*/
let segInfoMap = Object.create(null);
/**
* Gets whether or not `super()` is called in a given code path segment.
* @param {CodePathSegment} segment - A code path segment to get.
* @returns {boolean} `true` if `super()` is called.
*/
function isCalled(segment) {
return !segment.reachable || segInfoMap[segment.id].superCalled;
}
/**
* Checks whether or not this is in a constructor.
* @returns {boolean} `true` if this is in a constructor.
*/
function isInConstructorOfDerivedClass() {
return Boolean(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends);
}
/**
* Checks whether or not this is before `super()` is called.
* @returns {boolean} `true` if this is before `super()` is called.
*/
function isBeforeCallOfSuper() {
return (
isInConstructorOfDerivedClass(funcInfo) &&
!funcInfo.codePath.currentSegments.every(isCalled)
);
}
/**
* Sets a given node as invalid.
* @param {ASTNode} node - A node to set as invalid. This is one of
* a ThisExpression and a Super.
* @returns {void}
*/
function setInvalid(node) {
const segments = funcInfo.codePath.currentSegments;
for (let i = 0; i < segments.length; ++i) {
const segment = segments[i];
if (segment.reachable) {
segInfoMap[segment.id].invalidNodes.push(node);
}
}
}
/**
* Sets the current segment as `super` was called.
* @returns {void}
*/
function setSuperCalled() {
const segments = funcInfo.codePath.currentSegments;
for (let i = 0; i < segments.length; ++i) {
const segment = segments[i];
if (segment.reachable) {
segInfoMap[segment.id].superCalled = true;
}
}
}
return {
/**
* Adds information of a constructor into the stack.
* @param {CodePath} codePath - A code path which was started.
* @param {ASTNode} node - The current node.
* @returns {void}
*/
onCodePathStart(codePath, node) {
if (isConstructorFunction(node)) {
// Class > ClassBody > MethodDefinition > FunctionExpression
const classNode = node.parent.parent.parent;
funcInfo = {
upper: funcInfo,
isConstructor: true,
hasExtends: Boolean(
classNode.superClass &&
!astUtils.isNullOrUndefined(classNode.superClass)
),
codePath
};
} else {
funcInfo = {
upper: funcInfo,
isConstructor: false,
hasExtends: false,
codePath
};
}
},
/**
* Removes the top of stack item.
*
* And this treverses all segments of this code path then reports every
* invalid node.
*
* @param {CodePath} codePath - A code path which was ended.
* @param {ASTNode} node - The current node.
* @returns {void}
*/
onCodePathEnd(codePath) {
const isDerivedClass = funcInfo.hasExtends;
funcInfo = funcInfo.upper;
if (!isDerivedClass) {
return;
}
codePath.traverseSegments((segment, controller) => {
const info = segInfoMap[segment.id];
for (let i = 0; i < info.invalidNodes.length; ++i) {
const invalidNode = info.invalidNodes[i];
context.report({
message: "'{{kind}}' is not allowed before 'super()'.",
node: invalidNode,
data: {
kind: invalidNode.type === "Super" ? "super" : "this"
}
});
}
if (info.superCalled) {
controller.skip();
}
});
},
/**
* Initialize information of a given code path segment.
* @param {CodePathSegment} segment - A code path segment to initialize.
* @returns {void}
*/
onCodePathSegmentStart(segment) {
if (!isInConstructorOfDerivedClass(funcInfo)) {
return;
}
// Initialize info.
segInfoMap[segment.id] = {
superCalled: (
segment.prevSegments.length > 0 &&
segment.prevSegments.every(isCalled)
),
invalidNodes: []
};
},
/**
* Update information of the code path segment when a code path was
* looped.
* @param {CodePathSegment} fromSegment - The code path segment of the
* end of a loop.
* @param {CodePathSegment} toSegment - A code path segment of the head
* of a loop.
* @returns {void}
*/
onCodePathSegmentLoop(fromSegment, toSegment) {
if (!isInConstructorOfDerivedClass(funcInfo)) {
return;
}
// Update information inside of the loop.
funcInfo.codePath.traverseSegments(
{ first: toSegment, last: fromSegment },
(segment, controller) => {
const info = segInfoMap[segment.id];
if (info.superCalled) {
info.invalidNodes = [];
controller.skip();
} else if (
segment.prevSegments.length > 0 &&
segment.prevSegments.every(isCalled)
) {
info.superCalled = true;
info.invalidNodes = [];
}
}
);
},
/**
* Reports if this is before `super()`.
* @param {ASTNode} node - A target node.
* @returns {void}
*/
ThisExpression(node) {
if (isBeforeCallOfSuper()) {
setInvalid(node);
}
},
/**
* Reports if this is before `super()`.
* @param {ASTNode} node - A target node.
* @returns {void}
*/
Super(node) {
if (!astUtils.isCallee(node) && isBeforeCallOfSuper()) {
setInvalid(node);
}
},
/**
* Marks `super()` called.
* @param {ASTNode} node - A target node.
* @returns {void}
*/
"CallExpression:exit"(node) {
if (node.callee.type === "Super" && isBeforeCallOfSuper()) {
setSuperCalled();
}
},
/**
* Resets state.
* @returns {void}
*/
"Program:exit"() {
segInfoMap = Object.create(null);
}
};
}
};