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