• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @fileoverview enforce the location of single-line statements
3 * @author Teddy Katz
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11const POSITION_SCHEMA = { enum: ["beside", "below", "any"] };
12
13module.exports = {
14    meta: {
15        type: "layout",
16
17        docs: {
18            description: "enforce the location of single-line statements",
19            category: "Stylistic Issues",
20            recommended: false,
21            url: "https://eslint.org/docs/rules/nonblock-statement-body-position"
22        },
23
24        fixable: "whitespace",
25
26        schema: [
27            POSITION_SCHEMA,
28            {
29                properties: {
30                    overrides: {
31                        properties: {
32                            if: POSITION_SCHEMA,
33                            else: POSITION_SCHEMA,
34                            while: POSITION_SCHEMA,
35                            do: POSITION_SCHEMA,
36                            for: POSITION_SCHEMA
37                        },
38                        additionalProperties: false
39                    }
40                },
41                additionalProperties: false
42            }
43        ],
44
45        messages: {
46            expectNoLinebreak: "Expected no linebreak before this statement.",
47            expectLinebreak: "Expected a linebreak before this statement."
48        }
49    },
50
51    create(context) {
52        const sourceCode = context.getSourceCode();
53
54        //----------------------------------------------------------------------
55        // Helpers
56        //----------------------------------------------------------------------
57
58        /**
59         * Gets the applicable preference for a particular keyword
60         * @param {string} keywordName The name of a keyword, e.g. 'if'
61         * @returns {string} The applicable option for the keyword, e.g. 'beside'
62         */
63        function getOption(keywordName) {
64            return context.options[1] && context.options[1].overrides && context.options[1].overrides[keywordName] ||
65                context.options[0] ||
66                "beside";
67        }
68
69        /**
70         * Validates the location of a single-line statement
71         * @param {ASTNode} node The single-line statement
72         * @param {string} keywordName The applicable keyword name for the single-line statement
73         * @returns {void}
74         */
75        function validateStatement(node, keywordName) {
76            const option = getOption(keywordName);
77
78            if (node.type === "BlockStatement" || option === "any") {
79                return;
80            }
81
82            const tokenBefore = sourceCode.getTokenBefore(node);
83
84            if (tokenBefore.loc.end.line === node.loc.start.line && option === "below") {
85                context.report({
86                    node,
87                    messageId: "expectLinebreak",
88                    fix: fixer => fixer.insertTextBefore(node, "\n")
89                });
90            } else if (tokenBefore.loc.end.line !== node.loc.start.line && option === "beside") {
91                context.report({
92                    node,
93                    messageId: "expectNoLinebreak",
94                    fix(fixer) {
95                        if (sourceCode.getText().slice(tokenBefore.range[1], node.range[0]).trim()) {
96                            return null;
97                        }
98                        return fixer.replaceTextRange([tokenBefore.range[1], node.range[0]], " ");
99                    }
100                });
101            }
102        }
103
104        //----------------------------------------------------------------------
105        // Public
106        //----------------------------------------------------------------------
107
108        return {
109            IfStatement(node) {
110                validateStatement(node.consequent, "if");
111
112                // Check the `else` node, but don't check 'else if' statements.
113                if (node.alternate && node.alternate.type !== "IfStatement") {
114                    validateStatement(node.alternate, "else");
115                }
116            },
117            WhileStatement: node => validateStatement(node.body, "while"),
118            DoWhileStatement: node => validateStatement(node.body, "do"),
119            ForStatement: node => validateStatement(node.body, "for"),
120            ForInStatement: node => validateStatement(node.body, "for"),
121            ForOfStatement: node => validateStatement(node.body, "for")
122        };
123    }
124};
125