123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- "use strict";
- function isAnySegmentReachable(segments) {
- for (const segment of segments) {
- if (segment.reachable) {
- return true;
- }
- }
- return false;
- }
- function isConstructorFunction(node) {
- return (
- node.type === "FunctionExpression" &&
- node.parent.type === "MethodDefinition" &&
- node.parent.kind === "constructor"
- );
- }
- function isPossibleConstructor(node) {
- if (!node) {
- return false;
- }
- switch (node.type) {
- case "ClassExpression":
- case "FunctionExpression":
- case "ThisExpression":
- case "MemberExpression":
- case "CallExpression":
- case "NewExpression":
- case "ChainExpression":
- case "YieldExpression":
- case "TaggedTemplateExpression":
- case "MetaProperty":
- return true;
- case "Identifier":
- return node.name !== "undefined";
- case "AssignmentExpression":
- if (["=", "&&="].includes(node.operator)) {
- return isPossibleConstructor(node.right);
- }
- if (["||=", "??="].includes(node.operator)) {
- return (
- isPossibleConstructor(node.left) ||
- isPossibleConstructor(node.right)
- );
- }
-
- return false;
- case "LogicalExpression":
-
- if (node.operator === "&&") {
- return isPossibleConstructor(node.right);
- }
- return (
- isPossibleConstructor(node.left) ||
- isPossibleConstructor(node.right)
- );
- case "ConditionalExpression":
- return (
- isPossibleConstructor(node.alternate) ||
- isPossibleConstructor(node.consequent)
- );
- case "SequenceExpression": {
- const lastExpression = node.expressions[node.expressions.length - 1];
- return isPossibleConstructor(lastExpression);
- }
- default:
- return false;
- }
- }
- module.exports = {
- meta: {
- type: "problem",
- docs: {
- description: "Require `super()` calls in constructors",
- recommended: true,
- url: "https://eslint.org/docs/latest/rules/constructor-super"
- },
- schema: [],
- messages: {
- missingSome: "Lacked a call of 'super()' in some code paths.",
- missingAll: "Expected to call 'super()'.",
- duplicate: "Unexpected duplicate 'super()'.",
- badSuper: "Unexpected 'super()' because 'super' is not a constructor.",
- unexpected: "Unexpected 'super()'."
- }
- },
- create(context) {
-
- let funcInfo = null;
-
- let segInfoMap = Object.create(null);
-
- function isCalledInSomePath(segment) {
- return segment.reachable && segInfoMap[segment.id].calledInSomePaths;
- }
-
- function isCalledInEveryPath(segment) {
-
- if (segment.nextSegments.length === 1 &&
- segment.nextSegments[0].isLoopedPrevSegment(segment)
- ) {
- return true;
- }
- return segment.reachable && segInfoMap[segment.id].calledInEveryPaths;
- }
- return {
-
- onCodePathStart(codePath, node) {
- if (isConstructorFunction(node)) {
-
- const classNode = node.parent.parent.parent;
- const superClass = classNode.superClass;
- funcInfo = {
- upper: funcInfo,
- isConstructor: true,
- hasExtends: Boolean(superClass),
- superIsConstructor: isPossibleConstructor(superClass),
- codePath,
- currentSegments: new Set()
- };
- } else {
- funcInfo = {
- upper: funcInfo,
- isConstructor: false,
- hasExtends: false,
- superIsConstructor: false,
- codePath,
- currentSegments: new Set()
- };
- }
- },
-
- onCodePathEnd(codePath, node) {
- const hasExtends = funcInfo.hasExtends;
-
- funcInfo = funcInfo.upper;
- if (!hasExtends) {
- return;
- }
-
- const segments = codePath.returnedSegments;
- const calledInEveryPaths = segments.every(isCalledInEveryPath);
- const calledInSomePaths = segments.some(isCalledInSomePath);
- if (!calledInEveryPaths) {
- context.report({
- messageId: calledInSomePaths
- ? "missingSome"
- : "missingAll",
- node: node.parent
- });
- }
- },
-
- onCodePathSegmentStart(segment) {
- funcInfo.currentSegments.add(segment);
- if (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) {
- return;
- }
-
- const info = segInfoMap[segment.id] = {
- calledInSomePaths: false,
- calledInEveryPaths: false,
- validNodes: []
- };
-
- const prevSegments = segment.prevSegments;
- if (prevSegments.length > 0) {
- info.calledInSomePaths = prevSegments.some(isCalledInSomePath);
- info.calledInEveryPaths = prevSegments.every(isCalledInEveryPath);
- }
- },
- onUnreachableCodePathSegmentStart(segment) {
- funcInfo.currentSegments.add(segment);
- },
- onUnreachableCodePathSegmentEnd(segment) {
- funcInfo.currentSegments.delete(segment);
- },
- onCodePathSegmentEnd(segment) {
- funcInfo.currentSegments.delete(segment);
- },
-
- onCodePathSegmentLoop(fromSegment, toSegment) {
- if (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) {
- return;
- }
-
- const isRealLoop = toSegment.prevSegments.length >= 2;
- funcInfo.codePath.traverseSegments(
- { first: toSegment, last: fromSegment },
- segment => {
- const info = segInfoMap[segment.id];
- const prevSegments = segment.prevSegments;
-
- info.calledInSomePaths = prevSegments.some(isCalledInSomePath);
- info.calledInEveryPaths = prevSegments.every(isCalledInEveryPath);
-
- if (info.calledInSomePaths || isRealLoop) {
- const nodes = info.validNodes;
- info.validNodes = [];
- for (let i = 0; i < nodes.length; ++i) {
- const node = nodes[i];
- context.report({
- messageId: "duplicate",
- node
- });
- }
- }
- }
- );
- },
-
- "CallExpression:exit"(node) {
- if (!(funcInfo && funcInfo.isConstructor)) {
- return;
- }
-
- if (node.callee.type !== "Super") {
- return;
- }
-
- if (funcInfo.hasExtends) {
- const segments = funcInfo.currentSegments;
- let duplicate = false;
- let info = null;
- for (const segment of segments) {
- if (segment.reachable) {
- info = segInfoMap[segment.id];
- duplicate = duplicate || info.calledInSomePaths;
- info.calledInSomePaths = info.calledInEveryPaths = true;
- }
- }
- if (info) {
- if (duplicate) {
- context.report({
- messageId: "duplicate",
- node
- });
- } else if (!funcInfo.superIsConstructor) {
- context.report({
- messageId: "badSuper",
- node
- });
- } else {
- info.validNodes.push(node);
- }
- }
- } else if (isAnySegmentReachable(funcInfo.currentSegments)) {
- context.report({
- messageId: "unexpected",
- node
- });
- }
- },
-
- ReturnStatement(node) {
- if (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) {
- return;
- }
-
- if (!node.argument) {
- return;
- }
-
- const segments = funcInfo.currentSegments;
- for (const segment of segments) {
- if (segment.reachable) {
- const info = segInfoMap[segment.id];
- info.calledInSomePaths = info.calledInEveryPaths = true;
- }
- }
- },
-
- "Program:exit"() {
- segInfoMap = Object.create(null);
- }
- };
- }
- };
|