]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/for-direction.js
7df3d7e48028194670b3ff8e75eabaf15e8ce1c9
2 * @fileoverview enforce "for" loop update clause moving the counter in the right direction.(for-direction)
3 * @author Aladdin-ADD<hh_2013@foxmail.com>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 /** @type {import('../shared/types').Rule} */
18 description
: "Enforce \"for\" loop update clause moving the counter in the right direction.",
20 url
: "https://eslint.org/docs/rules/for-direction"
27 incorrectDirection
: "The update clause in this loop moves the variable in the wrong direction."
35 * @param {ASTNode} node the node to report.
38 function report(node
) {
41 messageId
: "incorrectDirection"
46 * check the right side of the assignment
47 * @param {ASTNode} update UpdateExpression to check
48 * @param {int} dir expected direction that could either be turned around or invalidated
49 * @returns {int} return dir, the negated dir or zero if it's not clear for identifiers
51 function getRightDirection(update
, dir
) {
52 if (update
.right
.type
=== "UnaryExpression") {
53 if (update
.right
.operator
=== "-") {
56 } else if (update
.right
.type
=== "Identifier") {
63 * check UpdateExpression add/sub the counter
64 * @param {ASTNode} update UpdateExpression to check
65 * @param {string} counter variable name to check
66 * @returns {int} if add return 1, if sub return -1, if nochange, return 0
68 function getUpdateDirection(update
, counter
) {
69 if (update
.argument
.type
=== "Identifier" && update
.argument
.name
=== counter
) {
70 if (update
.operator
=== "++") {
73 if (update
.operator
=== "--") {
81 * check AssignmentExpression add/sub the counter
82 * @param {ASTNode} update AssignmentExpression to check
83 * @param {string} counter variable name to check
84 * @returns {int} if add return 1, if sub return -1, if nochange, return 0
86 function getAssignmentDirection(update
, counter
) {
87 if (update
.left
.name
=== counter
) {
88 if (update
.operator
=== "+=") {
89 return getRightDirection(update
, 1);
91 if (update
.operator
=== "-=") {
92 return getRightDirection(update
, -1);
100 if (node
.test
&& node
.test
.type
=== "BinaryExpression" && node
.test
.left
.type
=== "Identifier" && node
.update
) {
101 const counter
= node
.test
.left
.name
;
102 const operator
= node
.test
.operator
;
103 const update
= node
.update
;
107 if (operator
=== "<" || operator
=== "<=") {
109 } else if (operator
=== ">" || operator
=== ">=") {
115 if (update
.type
=== "UpdateExpression") {
116 if (getUpdateDirection(update
, counter
) === wrongDirection
) {
119 } else if (update
.type
=== "AssignmentExpression" && getAssignmentDirection(update
, counter
) === wrongDirection
) {