2 * @fileoverview A rule to disallow using `this`/`super` before `super()`.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
19 * Checks whether or not a given node is a constructor.
20 * @param {ASTNode} node A node to check. This node type is one of
21 * `Program`, `FunctionDeclaration`, `FunctionExpression`, and
22 * `ArrowFunctionExpression`.
23 * @returns {boolean} `true` if the node is a constructor.
25 function isConstructorFunction(node) {
27 node.type === "FunctionExpression" &&
28 node.parent.type === "MethodDefinition" &&
29 node.parent.kind === "constructor"
33 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
42 description: "disallow `this`/`super` before calling `super()` in constructors",
43 category: "ECMAScript 6",
45 url: "https://eslint.org/docs/rules/no-this-before-super"
54 * Information for each constructor.
55 * - upper: Information of the upper constructor.
56 * - hasExtends: A flag which shows whether the owner class has a valid
58 * - scope: The scope of the owner class.
59 * - codePath: The code path of this constructor.
64 * Information for each code path segment.
65 * Each key is the id of a code path segment.
66 * Each value is an object:
67 * - superCalled: The flag which shows `super()` called in all code paths.
68 * - invalidNodes: The array of invalid ThisExpression and Super nodes.
70 let segInfoMap = Object.create(null);
73 * Gets whether or not `super()` is called in a given code path segment.
74 * @param {CodePathSegment} segment A code path segment to get.
75 * @returns {boolean} `true` if `super()` is called.
77 function isCalled(segment) {
78 return !segment.reachable || segInfoMap[segment.id].superCalled;
82 * Checks whether or not this is in a constructor.
83 * @returns {boolean} `true` if this is in a constructor.
85 function isInConstructorOfDerivedClass() {
86 return Boolean(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends);
90 * Checks whether or not this is before `super()` is called.
91 * @returns {boolean} `true` if this is before `super()` is called.
93 function isBeforeCallOfSuper() {
95 isInConstructorOfDerivedClass() &&
96 !funcInfo.codePath.currentSegments.every(isCalled)
101 * Sets a given node as invalid.
102 * @param {ASTNode} node A node to set as invalid. This is one of
103 * a ThisExpression and a Super.
106 function setInvalid(node) {
107 const segments = funcInfo.codePath.currentSegments;
109 for (let i = 0; i < segments.length; ++i) {
110 const segment = segments[i];
112 if (segment.reachable) {
113 segInfoMap[segment.id].invalidNodes.push(node);
119 * Sets the current segment as `super` was called.
122 function setSuperCalled() {
123 const segments = funcInfo.codePath.currentSegments;
125 for (let i = 0; i < segments.length; ++i) {
126 const segment = segments[i];
128 if (segment.reachable) {
129 segInfoMap[segment.id].superCalled = true;
137 * Adds information of a constructor into the stack.
138 * @param {CodePath} codePath A code path which was started.
139 * @param {ASTNode} node The current node.
142 onCodePathStart(codePath, node) {
143 if (isConstructorFunction(node)) {
145 // Class > ClassBody > MethodDefinition > FunctionExpression
146 const classNode = node.parent.parent.parent;
152 classNode.superClass &&
153 !astUtils.isNullOrUndefined(classNode.superClass)
160 isConstructor: false,
168 * Removes the top of stack item.
170 * And this treverses all segments of this code path then reports every
172 * @param {CodePath} codePath A code path which was ended.
175 onCodePathEnd(codePath) {
176 const isDerivedClass = funcInfo.hasExtends;
178 funcInfo = funcInfo.upper;
179 if (!isDerivedClass) {
183 codePath.traverseSegments((segment, controller) => {
184 const info = segInfoMap[segment.id];
186 for (let i = 0; i < info.invalidNodes.length; ++i) {
187 const invalidNode = info.invalidNodes[i];
190 message: "'{{kind}}' is not allowed before 'super()'.",
193 kind: invalidNode.type === "Super" ? "super" : "this"
198 if (info.superCalled) {
205 * Initialize information of a given code path segment.
206 * @param {CodePathSegment} segment A code path segment to initialize.
209 onCodePathSegmentStart(segment) {
210 if (!isInConstructorOfDerivedClass()) {
215 segInfoMap[segment.id] = {
217 segment.prevSegments.length > 0 &&
218 segment.prevSegments.every(isCalled)
225 * Update information of the code path segment when a code path was
227 * @param {CodePathSegment} fromSegment The code path segment of the
229 * @param {CodePathSegment} toSegment A code path segment of the head
233 onCodePathSegmentLoop(fromSegment, toSegment) {
234 if (!isInConstructorOfDerivedClass()) {
238 // Update information inside of the loop.
239 funcInfo.codePath.traverseSegments(
240 { first: toSegment, last: fromSegment },
241 (segment, controller) => {
242 const info = segInfoMap[segment.id];
244 if (info.superCalled) {
245 info.invalidNodes = [];
248 segment.prevSegments.length > 0 &&
249 segment.prevSegments.every(isCalled)
251 info.superCalled = true;
252 info.invalidNodes = [];
259 * Reports if this is before `super()`.
260 * @param {ASTNode} node A target node.
263 ThisExpression(node) {
264 if (isBeforeCallOfSuper()) {
270 * Reports if this is before `super()`.
271 * @param {ASTNode} node A target node.
275 if (!astUtils.isCallee(node) && isBeforeCallOfSuper()) {
281 * Marks `super()` called.
282 * @param {ASTNode} node A target node.
285 "CallExpression:exit"(node) {
286 if (node.callee.type === "Super" && isBeforeCallOfSuper()) {
296 segInfoMap = Object.create(null);