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"
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" });