Sha256: 801c06f4af5e3524eb5bbe33c9a4857e9b8b766f6f6a3a8ecdcec507d0a25990

Contents?: true

Size: 1.95 KB

Versions: 45

Compression:

Stored size: 1.95 KB

Contents

/**
 * @fileoverview Rule to require sorting of variables within a single Variable Declaration block
 * @author Ilya Volodin
 */

"use strict";

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

module.exports = {
    meta: {
        docs: {
            description: "require variables within the same declaration block to be sorted",
            category: "Stylistic Issues",
            recommended: false
        },

        schema: [
            {
                type: "object",
                properties: {
                    ignoreCase: {
                        type: "boolean"
                    }
                },
                additionalProperties: false
            }
        ]
    },

    create: function(context) {

        var configuration = context.options[0] || {},
            ignoreCase = configuration.ignoreCase || false;

        return {
            VariableDeclaration: function(node) {
                node.declarations.reduce(function(memo, decl) {
                    if (decl.id.type === "ObjectPattern" || decl.id.type === "ArrayPattern") {
                        return memo;
                    }

                    var lastVariableName = memo.id.name,
                        currenVariableName = decl.id.name;

                    if (ignoreCase) {
                        lastVariableName = lastVariableName.toLowerCase();
                        currenVariableName = currenVariableName.toLowerCase();
                    }

                    if (currenVariableName < lastVariableName) {
                        context.report(decl, "Variables within the same declaration block should be sorted alphabetically");
                        return memo;
                    } else {
                        return decl;
                    }
                }, node.declarations[0]);
            }
        };
    }
};

Version data entries

45 entries across 45 versions & 2 rubygems

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