source: imaps-frontend/node_modules/eslint/lib/rules/no-self-compare.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/**
2 * @fileoverview Rule to flag comparison where left part is the same as the right
3 * part.
4 * @author Ilya Volodin
5 */
6
7"use strict";
8
9//------------------------------------------------------------------------------
10// Rule Definition
11//------------------------------------------------------------------------------
12
13/** @type {import('../shared/types').Rule} */
14module.exports = {
15 meta: {
16 type: "problem",
17
18 docs: {
19 description: "Disallow comparisons where both sides are exactly the same",
20 recommended: false,
21 url: "https://eslint.org/docs/latest/rules/no-self-compare"
22 },
23
24 schema: [],
25
26 messages: {
27 comparingToSelf: "Comparing to itself is potentially pointless."
28 }
29 },
30
31 create(context) {
32 const sourceCode = context.sourceCode;
33
34 /**
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
39 */
40 function hasSameTokens(nodeA, nodeB) {
41 const tokensA = sourceCode.getTokens(nodeA);
42 const tokensB = sourceCode.getTokens(nodeB);
43
44 return tokensA.length === tokensB.length &&
45 tokensA.every((token, index) => token.type === tokensB[index].type && token.value === tokensB[index].value);
46 }
47
48 return {
49
50 BinaryExpression(node) {
51 const operators = new Set(["===", "==", "!==", "!=", ">", "<", ">=", "<="]);
52
53 if (operators.has(node.operator) && hasSameTokens(node.left, node.right)) {
54 context.report({ node, messageId: "comparingToSelf" });
55 }
56 }
57 };
58
59 }
60};
Note: See TracBrowser for help on using the repository browser.