mirror of
https://github.com/github/codeql-action.git
synced 2026-01-01 12:10:20 +08:00
* Bump the npm group with 5 updates Bumps the npm group with 5 updates: | Package | From | To | | --- | --- | --- | | [@types/semver](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/semver) | `7.5.0` | `7.5.1` | | [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/eslint-plugin) | `6.4.1` | `6.5.0` | | [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser) | `6.4.1` | `6.5.0` | | [eslint](https://github.com/eslint/eslint) | `8.47.0` | `8.48.0` | | [typescript](https://github.com/Microsoft/TypeScript) | `5.1.6` | `5.2.2` | Updates `@types/semver` from 7.5.0 to 7.5.1 - [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.4.1 to 6.5.0 - [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.5.0/packages/eslint-plugin) Updates `@typescript-eslint/parser` from 6.4.1 to 6.5.0 - [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.5.0/packages/parser) Updates `eslint` from 8.47.0 to 8.48.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.47.0...v8.48.0) Updates `typescript` from 5.1.6 to 5.2.2 - [Release notes](https://github.com/Microsoft/TypeScript/releases) - [Commits](https://github.com/Microsoft/TypeScript/compare/v5.1.6...v5.2.2) --- updated-dependencies: - 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: typescript dependency-type: direct:development update-type: version-update:semver-minor 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>
134 lines
4.7 KiB
JavaScript
134 lines
4.7 KiB
JavaScript
/**
|
|
* @fileoverview enforce "for" loop update clause moving the counter in the right direction.(for-direction)
|
|
* @author Aladdin-ADD<hh_2013@foxmail.com>
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Requirements
|
|
//------------------------------------------------------------------------------
|
|
|
|
const { getStaticValue } = require("@eslint-community/eslint-utils");
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** @type {import('../shared/types').Rule} */
|
|
module.exports = {
|
|
meta: {
|
|
type: "problem",
|
|
|
|
docs: {
|
|
description: "Enforce \"for\" loop update clause moving the counter in the right direction",
|
|
recommended: true,
|
|
url: "https://eslint.org/docs/latest/rules/for-direction"
|
|
},
|
|
|
|
fixable: null,
|
|
schema: [],
|
|
|
|
messages: {
|
|
incorrectDirection: "The update clause in this loop moves the variable in the wrong direction."
|
|
}
|
|
},
|
|
|
|
create(context) {
|
|
const { sourceCode } = context;
|
|
|
|
/**
|
|
* report an error.
|
|
* @param {ASTNode} node the node to report.
|
|
* @returns {void}
|
|
*/
|
|
function report(node) {
|
|
context.report({
|
|
node,
|
|
messageId: "incorrectDirection"
|
|
});
|
|
}
|
|
|
|
/**
|
|
* check the right side of the assignment
|
|
* @param {ASTNode} update UpdateExpression to check
|
|
* @param {int} dir expected direction that could either be turned around or invalidated
|
|
* @returns {int} return dir, the negated dir, or zero if the counter does not change or the direction is not clear
|
|
*/
|
|
function getRightDirection(update, dir) {
|
|
const staticValue = getStaticValue(update.right, sourceCode.getScope(update));
|
|
|
|
if (staticValue && ["bigint", "boolean", "number"].includes(typeof staticValue.value)) {
|
|
const sign = Math.sign(Number(staticValue.value)) || 0; // convert NaN to 0
|
|
|
|
return dir * sign;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* check UpdateExpression add/sub the counter
|
|
* @param {ASTNode} update UpdateExpression to check
|
|
* @param {string} counter variable name to check
|
|
* @returns {int} if add return 1, if sub return -1, if nochange, return 0
|
|
*/
|
|
function getUpdateDirection(update, counter) {
|
|
if (update.argument.type === "Identifier" && update.argument.name === counter) {
|
|
if (update.operator === "++") {
|
|
return 1;
|
|
}
|
|
if (update.operator === "--") {
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* check AssignmentExpression add/sub the counter
|
|
* @param {ASTNode} update AssignmentExpression to check
|
|
* @param {string} counter variable name to check
|
|
* @returns {int} if add return 1, if sub return -1, if nochange, return 0
|
|
*/
|
|
function getAssignmentDirection(update, counter) {
|
|
if (update.left.name === counter) {
|
|
if (update.operator === "+=") {
|
|
return getRightDirection(update, 1);
|
|
}
|
|
if (update.operator === "-=") {
|
|
return getRightDirection(update, -1);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
return {
|
|
ForStatement(node) {
|
|
|
|
if (node.test && node.test.type === "BinaryExpression" && node.test.left.type === "Identifier" && node.update) {
|
|
const counter = node.test.left.name;
|
|
const operator = node.test.operator;
|
|
const update = node.update;
|
|
|
|
let wrongDirection;
|
|
|
|
if (operator === "<" || operator === "<=") {
|
|
wrongDirection = -1;
|
|
} else if (operator === ">" || operator === ">=") {
|
|
wrongDirection = 1;
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
if (update.type === "UpdateExpression") {
|
|
if (getUpdateDirection(update, counter) === wrongDirection) {
|
|
report(node);
|
|
}
|
|
} else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) {
|
|
report(node);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
};
|