Sha256: 056d1e8e52899e9567f1a0f28f6ae1f470ecdb92438b74f4189cf7506570b75a

Contents?: true

Size: 1.57 KB

Versions: 45

Compression:

Stored size: 1.57 KB

Contents

/**
 * @fileoverview Rule to flag use of duplicate keys in an object.
 * @author Ian Christian Myers
 */

"use strict";

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

module.exports = {
    meta: {
        docs: {
            description: "disallow duplicate keys in object literals",
            category: "Possible Errors",
            recommended: true
        },

        schema: []
    },

    create: function(context) {

        return {

            ObjectExpression: function(node) {

                // Object that will be a map of properties--safe because we will
                // prefix all of the keys.
                var nodeProps = Object.create(null);

                node.properties.forEach(function(property) {

                    if (property.type !== "Property") {
                        return;
                    }

                    var keyName = property.key.name || property.key.value,
                        key = property.kind + "-" + keyName,
                        checkProperty = (!property.computed || property.key.type === "Literal");

                    if (checkProperty) {
                        if (nodeProps[key]) {
                            context.report(node, property.loc.start, "Duplicate key '{{key}}'.", { key: keyName });
                        } else {
                            nodeProps[key] = true;
                        }
                    }
                });

            }
        };

    }
};

Version data entries

45 entries across 45 versions & 2 rubygems

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