Sha256: eb87f1c4cf18a6e708883b1f423e63dbc0491b66d40bc060e1db85826fe55413

Contents?: true

Size: 1.7 KB

Versions: 43

Compression:

Stored size: 1.7 KB

Contents

/**
 * @fileoverview Rule to flag comparison where left part is the same as the right
 * part.
 * @author Ilya Volodin
 */

"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
    meta: {
        docs: {
            description: "disallow comparisons where both sides are exactly the same",
            category: "Best Practices",
            recommended: false,
            url: "https://eslint.org/docs/rules/no-self-compare"
        },

        schema: []
    },

    create(context) {
        const sourceCode = context.getSourceCode();

        /**
         * Determines whether two nodes are composed of the same tokens.
         * @param {ASTNode} nodeA The first node
         * @param {ASTNode} nodeB The second node
         * @returns {boolean} true if the nodes have identical token representations
         */
        function hasSameTokens(nodeA, nodeB) {
            const tokensA = sourceCode.getTokens(nodeA);
            const tokensB = sourceCode.getTokens(nodeB);

            return tokensA.length === tokensB.length &&
                tokensA.every((token, index) => token.type === tokensB[index].type && token.value === tokensB[index].value);
        }

        return {

            BinaryExpression(node) {
                const operators = new Set(["===", "==", "!==", "!=", ">", "<", ">=", "<="]);

                if (operators.has(node.operator) && hasSameTokens(node.left, node.right)) {
                    context.report({ node, message: "Comparing to itself is potentially pointless." });
                }
            }
        };

    }
};

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
govuk_publishing_components-18.0.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.21.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.20.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.19.1 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.19.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.18.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.17.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.16.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.15.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.14.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.13.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.12.2 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.12.1 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.12.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.11.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.10.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.9.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.8.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.7.0 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js
govuk_publishing_components-17.6.1 node_modules/standard/node_modules/eslint/lib/rules/no-self-compare.js