google_forms/node_modules/eslint/lib/rules/for-direction.js

141 lines
5.0 KiB
JavaScript
Raw Normal View History

2024-08-09 12:04:48 +00:00
/**
* @fileoverview enforce "for" loop update clause moving the counter in the right direction.(for-direction)
* @author Aladdin-ADD<hh_2013@foxmail.com>
*/
"use strict";
2024-08-21 06:34:30 +00:00
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const { getStaticValue } = require("@eslint-community/eslint-utils");
2024-08-09 12:04:48 +00:00
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
2024-08-21 06:34:30 +00:00
/** @type {import('../shared/types').Rule} */
2024-08-09 12:04:48 +00:00
module.exports = {
meta: {
type: "problem",
docs: {
2024-08-21 06:34:30 +00:00
description: "Enforce \"for\" loop update clause moving the counter in the right direction",
2024-08-09 12:04:48 +00:00
recommended: true,
2024-08-21 06:34:30 +00:00
url: "https://eslint.org/docs/latest/rules/for-direction"
2024-08-09 12:04:48 +00:00
},
fixable: null,
schema: [],
messages: {
incorrectDirection: "The update clause in this loop moves the variable in the wrong direction."
}
},
create(context) {
2024-08-21 06:34:30 +00:00
const { sourceCode } = context;
2024-08-09 12:04:48 +00:00
/**
* 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
2024-08-21 06:34:30 +00:00
* @returns {int} return dir, the negated dir, or zero if the counter does not change or the direction is not clear
2024-08-09 12:04:48 +00:00
*/
function getRightDirection(update, dir) {
2024-08-21 06:34:30 +00:00
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;
2024-08-09 12:04:48 +00:00
}
2024-08-21 06:34:30 +00:00
return 0;
2024-08-09 12:04:48 +00:00
}
/**
* 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;
}
2024-08-21 06:34:30 +00:00
2024-08-09 12:04:48 +00:00
return {
ForStatement(node) {
2024-08-21 06:34:30 +00:00
if (node.test && node.test.type === "BinaryExpression" && node.update) {
for (const counterPosition of ["left", "right"]) {
if (node.test[counterPosition].type !== "Identifier") {
continue;
}
const counter = node.test[counterPosition].name;
const operator = node.test.operator;
const update = node.update;
2024-08-09 12:04:48 +00:00
2024-08-21 06:34:30 +00:00
let wrongDirection;
2024-08-09 12:04:48 +00:00
2024-08-21 06:34:30 +00:00
if (operator === "<" || operator === "<=") {
wrongDirection = counterPosition === "left" ? -1 : 1;
} else if (operator === ">" || operator === ">=") {
wrongDirection = counterPosition === "left" ? 1 : -1;
} else {
return;
}
2024-08-09 12:04:48 +00:00
2024-08-21 06:34:30 +00:00
if (update.type === "UpdateExpression") {
if (getUpdateDirection(update, counter) === wrongDirection) {
report(node);
}
} else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) {
2024-08-09 12:04:48 +00:00
report(node);
}
}
}
}
};
}
};