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",
19 category: "Best Practices",
21 url: "https://eslint.org/docs/rules/no-self-compare"
28 const sourceCode = context.getSourceCode();
31 * Determines whether two nodes are composed of the same tokens.
32 * @param {ASTNode} nodeA The first node
33 * @param {ASTNode} nodeB The second node
34 * @returns {boolean} true if the nodes have identical token representations
36 function hasSameTokens(nodeA, nodeB) {
37 const tokensA = sourceCode.getTokens(nodeA);
38 const tokensB = sourceCode.getTokens(nodeB);
40 return tokensA.length === tokensB.length &&
41 tokensA.every((token, index) => token.type === tokensB[index].type && token.value === tokensB[index].value);
46 BinaryExpression(node) {
47 const operators = new Set(["===", "==", "!==", "!=", ">", "<", ">=", "<="]);
49 if (operators.has(node.operator) && hasSameTokens(node.left, node.right)) {
50 context.report({ node, message: "Comparing to itself is potentially pointless." });