• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @fileoverview Rule to check multiple var declarations per line
3 * @author Alberto Rodríguez
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11module.exports = {
12    meta: {
13        type: "suggestion",
14
15        docs: {
16            description: "require or disallow newlines around variable declarations",
17            category: "Stylistic Issues",
18            recommended: false,
19            url: "https://eslint.org/docs/rules/one-var-declaration-per-line"
20        },
21
22        schema: [
23            {
24                enum: ["always", "initializations"]
25            }
26        ],
27
28        fixable: "whitespace",
29
30        messages: {
31            expectVarOnNewline: "Expected variable declaration to be on a new line."
32        }
33    },
34
35    create(context) {
36
37        const always = context.options[0] === "always";
38
39        //--------------------------------------------------------------------------
40        // Helpers
41        //--------------------------------------------------------------------------
42
43
44        /**
45         * Determine if provided keyword is a variant of for specifiers
46         * @private
47         * @param {string} keyword keyword to test
48         * @returns {boolean} True if `keyword` is a variant of for specifier
49         */
50        function isForTypeSpecifier(keyword) {
51            return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
52        }
53
54        /**
55         * Checks newlines around variable declarations.
56         * @private
57         * @param {ASTNode} node `VariableDeclaration` node to test
58         * @returns {void}
59         */
60        function checkForNewLine(node) {
61            if (isForTypeSpecifier(node.parent.type)) {
62                return;
63            }
64
65            const declarations = node.declarations;
66            let prev;
67
68            declarations.forEach(current => {
69                if (prev && prev.loc.end.line === current.loc.start.line) {
70                    if (always || prev.init || current.init) {
71                        context.report({
72                            node,
73                            messageId: "expectVarOnNewline",
74                            loc: current.loc,
75                            fix: fixer => fixer.insertTextBefore(current, "\n")
76                        });
77                    }
78                }
79                prev = current;
80            });
81        }
82
83        //--------------------------------------------------------------------------
84        // Public
85        //--------------------------------------------------------------------------
86
87        return {
88            VariableDeclaration: checkForNewLine
89        };
90
91    }
92};
93