Sha256: a3e979993e2b743ef33362a85331e904c9aca65714ca35e895d4be4d54e87a26

Contents?: true

Size: 1.81 KB

Versions: 45

Compression:

Stored size: 1.81 KB

Contents

/**
 * @fileoverview Rule to require parens in arrow function arguments.
 * @author Jxck
 */
"use strict";

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

module.exports = {
    meta: {
        docs: {
            description: "require parentheses around arrow function arguments",
            category: "ECMAScript 6",
            recommended: false
        },

        schema: [
            {
                enum: ["always", "as-needed"]
            }
        ]
    },

    create: function(context) {
        var message = "Expected parentheses around arrow function argument.";
        var asNeededMessage = "Unexpected parentheses around single function argument";
        var asNeeded = context.options[0] === "as-needed";

        var sourceCode = context.getSourceCode();

        /**
         * Determines whether a arrow function argument end with `)`
         * @param {ASTNode} node The arrow function node.
         * @returns {void}
         */
        function parens(node) {
            var token = sourceCode.getFirstToken(node);

            // as-needed: x => x
            if (asNeeded && node.params.length === 1 && node.params[0].type === "Identifier") {
                if (token.type === "Punctuator" && token.value === "(") {
                    context.report(node, asNeededMessage);
                }
                return;
            }

            if (token.type === "Identifier") {
                var after = sourceCode.getTokenAfter(token);

                // (x) => x
                if (after.value !== ")") {
                    context.report(node, message);
                }
            }
        }

        return {
            ArrowFunctionExpression: parens
        };
    }
};

Version data entries

45 entries across 45 versions & 2 rubygems

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