mirror of
https://github.com/github/codeql-action.git
synced 2025-12-27 01:30:10 +08:00
* Bump the npm group with 11 updates Bumps the npm group with 11 updates: | Package | From | To | | --- | --- | --- | | [@actions/artifact](https://github.com/actions/toolkit/tree/HEAD/packages/artifact) | `1.1.1` | `1.1.2` | | [@actions/core](https://github.com/actions/toolkit/tree/HEAD/packages/core) | `1.10.0` | `1.10.1` | | [uuid](https://github.com/uuidjs/uuid) | `9.0.0` | `9.0.1` | | [@types/uuid](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/uuid) | `9.0.3` | `9.0.4` | | [@types/adm-zip](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/adm-zip) | `0.5.0` | `0.5.1` | | [@types/js-yaml](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/js-yaml) | `4.0.5` | `4.0.6` | | [@types/semver](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/semver) | `7.5.1` | `7.5.2` | | [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/eslint-plugin) | `6.5.0` | `6.7.2` | | [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser) | `6.6.0` | `6.7.2` | | [eslint](https://github.com/eslint/eslint) | `8.48.0` | `8.49.0` | | [sinon](https://github.com/sinonjs/sinon) | `15.2.0` | `16.0.0` | Updates `@actions/artifact` from 1.1.1 to 1.1.2 - [Changelog](https://github.com/actions/toolkit/blob/main/packages/artifact/RELEASES.md) - [Commits](https://github.com/actions/toolkit/commits/HEAD/packages/artifact) Updates `@actions/core` from 1.10.0 to 1.10.1 - [Changelog](https://github.com/actions/toolkit/blob/main/packages/core/RELEASES.md) - [Commits](https://github.com/actions/toolkit/commits/HEAD/packages/core) Updates `uuid` from 9.0.0 to 9.0.1 - [Changelog](https://github.com/uuidjs/uuid/blob/main/CHANGELOG.md) - [Commits](https://github.com/uuidjs/uuid/compare/v9.0.0...v9.0.1) Updates `@types/uuid` from 9.0.3 to 9.0.4 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/uuid) Updates `@types/adm-zip` from 0.5.0 to 0.5.1 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/adm-zip) Updates `@types/js-yaml` from 4.0.5 to 4.0.6 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/js-yaml) Updates `@types/semver` from 7.5.1 to 7.5.2 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/semver) Updates `@typescript-eslint/eslint-plugin` from 6.5.0 to 6.7.2 - [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases) - [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/CHANGELOG.md) - [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v6.7.2/packages/eslint-plugin) Updates `@typescript-eslint/parser` from 6.6.0 to 6.7.2 - [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases) - [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/parser/CHANGELOG.md) - [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v6.7.2/packages/parser) Updates `eslint` from 8.48.0 to 8.49.0 - [Release notes](https://github.com/eslint/eslint/releases) - [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md) - [Commits](https://github.com/eslint/eslint/compare/v8.48.0...v8.49.0) Updates `sinon` from 15.2.0 to 16.0.0 - [Release notes](https://github.com/sinonjs/sinon/releases) - [Changelog](https://github.com/sinonjs/sinon/blob/main/docs/changelog.md) - [Commits](https://github.com/sinonjs/sinon/compare/v15.2.0...v16.0.0) --- updated-dependencies: - dependency-name: "@actions/artifact" dependency-type: direct:production update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@actions/core" dependency-type: direct:production update-type: version-update:semver-patch dependency-group: npm - dependency-name: uuid dependency-type: direct:production update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@types/uuid" dependency-type: direct:production update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@types/adm-zip" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@types/js-yaml" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@types/semver" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@typescript-eslint/eslint-plugin" dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm - dependency-name: "@typescript-eslint/parser" dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm - dependency-name: eslint dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm - dependency-name: sinon dependency-type: direct:development update-type: version-update:semver-major dependency-group: npm ... Signed-off-by: dependabot[bot] <support@github.com> * Update checked-in dependencies --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
447 lines
15 KiB
JavaScript
447 lines
15 KiB
JavaScript
/**
|
|
* @fileoverview A rule to verify `super()` callings in constructor.
|
|
* @author Toru Nagashima
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Helpers
|
|
//------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Checks all segments in a set and returns true if any are reachable.
|
|
* @param {Set<CodePathSegment>} segments The segments to check.
|
|
* @returns {boolean} True if any segment is reachable; false otherwise.
|
|
*/
|
|
function isAnySegmentReachable(segments) {
|
|
|
|
for (const segment of segments) {
|
|
if (segment.reachable) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 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"
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Checks whether a given node can be a constructor or not.
|
|
* @param {ASTNode} node A node to check.
|
|
* @returns {boolean} `true` if the node can be a 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)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* All other assignment operators are mathematical assignment operators (arithmetic or bitwise).
|
|
* An assignment expression with a mathematical operator can either evaluate to a primitive value,
|
|
* or throw, depending on the operands. Thus, it cannot evaluate to a constructor function.
|
|
*/
|
|
return false;
|
|
|
|
case "LogicalExpression":
|
|
|
|
/*
|
|
* If the && operator short-circuits, the left side was falsy and therefore not a constructor, and if
|
|
* it doesn't short-circuit, it takes the value from the right side, so the right side must always be a
|
|
* possible constructor. A future improvement could verify that the left side could be truthy by
|
|
* excluding falsy literals.
|
|
*/
|
|
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;
|
|
}
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** @type {import('../shared/types').Rule} */
|
|
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) {
|
|
|
|
/*
|
|
* {{hasExtends: boolean, scope: Scope, codePath: CodePath}[]}
|
|
* Information for each constructor.
|
|
* - upper: Information of the upper constructor.
|
|
* - hasExtends: A flag which shows whether own class has a valid `extends`
|
|
* part.
|
|
* - scope: The scope of own class.
|
|
* - codePath: The code path object of the constructor.
|
|
*/
|
|
let funcInfo = null;
|
|
|
|
/*
|
|
* {Map<string, {calledInSomePaths: boolean, calledInEveryPaths: boolean}>}
|
|
* Information for each code path segment.
|
|
* - calledInSomePaths: A flag of be called `super()` in some code paths.
|
|
* - calledInEveryPaths: A flag of be called `super()` in all code paths.
|
|
* - validNodes:
|
|
*/
|
|
let segInfoMap = Object.create(null);
|
|
|
|
/**
|
|
* Gets the flag which shows `super()` is called in some paths.
|
|
* @param {CodePathSegment} segment A code path segment to get.
|
|
* @returns {boolean} The flag which shows `super()` is called in some paths
|
|
*/
|
|
function isCalledInSomePath(segment) {
|
|
return segment.reachable && segInfoMap[segment.id].calledInSomePaths;
|
|
}
|
|
|
|
/**
|
|
* Gets the flag which shows `super()` is called in all paths.
|
|
* @param {CodePathSegment} segment A code path segment to get.
|
|
* @returns {boolean} The flag which shows `super()` is called in all paths.
|
|
*/
|
|
function isCalledInEveryPath(segment) {
|
|
|
|
/*
|
|
* If specific segment is the looped segment of the current segment,
|
|
* skip the segment.
|
|
* If not skipped, this never becomes true after a loop.
|
|
*/
|
|
if (segment.nextSegments.length === 1 &&
|
|
segment.nextSegments[0].isLoopedPrevSegment(segment)
|
|
) {
|
|
return true;
|
|
}
|
|
return segment.reachable && segInfoMap[segment.id].calledInEveryPaths;
|
|
}
|
|
|
|
return {
|
|
|
|
/**
|
|
* Stacks a constructor information.
|
|
* @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;
|
|
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()
|
|
};
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Pops a constructor information.
|
|
* And reports if `super()` lacked.
|
|
* @param {CodePath} codePath A code path which was ended.
|
|
* @param {ASTNode} node The current node.
|
|
* @returns {void}
|
|
*/
|
|
onCodePathEnd(codePath, node) {
|
|
const hasExtends = funcInfo.hasExtends;
|
|
|
|
// Pop.
|
|
funcInfo = funcInfo.upper;
|
|
|
|
if (!hasExtends) {
|
|
return;
|
|
}
|
|
|
|
// Reports if `super()` lacked.
|
|
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
|
|
});
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Initialize information of a given code path segment.
|
|
* @param {CodePathSegment} segment A code path segment to initialize.
|
|
* @returns {void}
|
|
*/
|
|
onCodePathSegmentStart(segment) {
|
|
|
|
funcInfo.currentSegments.add(segment);
|
|
|
|
if (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) {
|
|
return;
|
|
}
|
|
|
|
// Initialize info.
|
|
const info = segInfoMap[segment.id] = {
|
|
calledInSomePaths: false,
|
|
calledInEveryPaths: false,
|
|
validNodes: []
|
|
};
|
|
|
|
// When there are previous segments, aggregates these.
|
|
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);
|
|
},
|
|
|
|
|
|
/**
|
|
* 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 (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) {
|
|
return;
|
|
}
|
|
|
|
// Update information inside of the loop.
|
|
const isRealLoop = toSegment.prevSegments.length >= 2;
|
|
|
|
funcInfo.codePath.traverseSegments(
|
|
{ first: toSegment, last: fromSegment },
|
|
segment => {
|
|
const info = segInfoMap[segment.id];
|
|
const prevSegments = segment.prevSegments;
|
|
|
|
// Updates flags.
|
|
info.calledInSomePaths = prevSegments.some(isCalledInSomePath);
|
|
info.calledInEveryPaths = prevSegments.every(isCalledInEveryPath);
|
|
|
|
// If flags become true anew, reports the valid nodes.
|
|
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
|
|
});
|
|
}
|
|
}
|
|
}
|
|
);
|
|
},
|
|
|
|
/**
|
|
* Checks for a call of `super()`.
|
|
* @param {ASTNode} node A CallExpression node to check.
|
|
* @returns {void}
|
|
*/
|
|
"CallExpression:exit"(node) {
|
|
if (!(funcInfo && funcInfo.isConstructor)) {
|
|
return;
|
|
}
|
|
|
|
// Skips except `super()`.
|
|
if (node.callee.type !== "Super") {
|
|
return;
|
|
}
|
|
|
|
// Reports if needed.
|
|
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
|
|
});
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Set the mark to the returned path as `super()` was called.
|
|
* @param {ASTNode} node A ReturnStatement node to check.
|
|
* @returns {void}
|
|
*/
|
|
ReturnStatement(node) {
|
|
if (!(funcInfo && funcInfo.isConstructor && funcInfo.hasExtends)) {
|
|
return;
|
|
}
|
|
|
|
// Skips if no argument.
|
|
if (!node.argument) {
|
|
return;
|
|
}
|
|
|
|
// Returning argument is a substitute of 'super()'.
|
|
const segments = funcInfo.currentSegments;
|
|
|
|
for (const segment of segments) {
|
|
|
|
if (segment.reachable) {
|
|
const info = segInfoMap[segment.id];
|
|
|
|
info.calledInSomePaths = info.calledInEveryPaths = true;
|
|
}
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Resets state.
|
|
* @returns {void}
|
|
*/
|
|
"Program:exit"() {
|
|
segInfoMap = Object.create(null);
|
|
}
|
|
};
|
|
}
|
|
};
|