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