]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-self-compare.js
c3512895e13474e421909daa2d4c55a4d40c12b6
2 * @fileoverview Rule to flag comparison where left part is the same as the right
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 /** @type {import('../shared/types').Rule} */
19 description
: "disallow comparisons where both sides are exactly the same",
21 url
: "https://eslint.org/docs/rules/no-self-compare"
27 comparingToSelf
: "Comparing to itself is potentially pointless."
32 const sourceCode
= context
.getSourceCode();
35 * Determines whether two nodes are composed of the same tokens.
36 * @param {ASTNode} nodeA The first node
37 * @param {ASTNode} nodeB The second node
38 * @returns {boolean} true if the nodes have identical token representations
40 function hasSameTokens(nodeA
, nodeB
) {
41 const tokensA
= sourceCode
.getTokens(nodeA
);
42 const tokensB
= sourceCode
.getTokens(nodeB
);
44 return tokensA
.length
=== tokensB
.length
&&
45 tokensA
.every((token
, index
) => token
.type
=== tokensB
[index
].type
&& token
.value
=== tokensB
[index
].value
);
50 BinaryExpression(node
) {
51 const operators
= new Set(["===", "==", "!==", "!=", ">", "<", ">=", "<="]);
53 if (operators
.has(node
.operator
) && hasSameTokens(node
.left
, node
.right
)) {
54 context
.report({ node
, messageId
: "comparingToSelf" });