1 | /**
|
---|
2 | * @fileoverview A rule to disallow or enforce spaces inside of single line blocks.
|
---|
3 | * @author Toru Nagashima
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 |
|
---|
7 | "use strict";
|
---|
8 |
|
---|
9 | const util = require("./utils/ast-utils");
|
---|
10 |
|
---|
11 | //------------------------------------------------------------------------------
|
---|
12 | // Rule Definition
|
---|
13 | //------------------------------------------------------------------------------
|
---|
14 |
|
---|
15 | /** @type {import('../shared/types').Rule} */
|
---|
16 | module.exports = {
|
---|
17 | meta: {
|
---|
18 | deprecated: true,
|
---|
19 | replacedBy: [],
|
---|
20 | type: "layout",
|
---|
21 |
|
---|
22 | docs: {
|
---|
23 | description: "Disallow or enforce spaces inside of blocks after opening block and before closing block",
|
---|
24 | recommended: false,
|
---|
25 | url: "https://eslint.org/docs/latest/rules/block-spacing"
|
---|
26 | },
|
---|
27 |
|
---|
28 | fixable: "whitespace",
|
---|
29 |
|
---|
30 | schema: [
|
---|
31 | { enum: ["always", "never"] }
|
---|
32 | ],
|
---|
33 |
|
---|
34 | messages: {
|
---|
35 | missing: "Requires a space {{location}} '{{token}}'.",
|
---|
36 | extra: "Unexpected space(s) {{location}} '{{token}}'."
|
---|
37 | }
|
---|
38 | },
|
---|
39 |
|
---|
40 | create(context) {
|
---|
41 | const always = (context.options[0] !== "never"),
|
---|
42 | messageId = always ? "missing" : "extra",
|
---|
43 | sourceCode = context.sourceCode;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Gets the open brace token from a given node.
|
---|
47 | * @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to get.
|
---|
48 | * @returns {Token} The token of the open brace.
|
---|
49 | */
|
---|
50 | function getOpenBrace(node) {
|
---|
51 | if (node.type === "SwitchStatement") {
|
---|
52 | if (node.cases.length > 0) {
|
---|
53 | return sourceCode.getTokenBefore(node.cases[0]);
|
---|
54 | }
|
---|
55 | return sourceCode.getLastToken(node, 1);
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (node.type === "StaticBlock") {
|
---|
59 | return sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token
|
---|
60 | }
|
---|
61 |
|
---|
62 | // "BlockStatement"
|
---|
63 | return sourceCode.getFirstToken(node);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Checks whether or not:
|
---|
68 | * - given tokens are on same line.
|
---|
69 | * - there is/isn't a space between given tokens.
|
---|
70 | * @param {Token} left A token to check.
|
---|
71 | * @param {Token} right The token which is next to `left`.
|
---|
72 | * @returns {boolean}
|
---|
73 | * When the option is `"always"`, `true` if there are one or more spaces between given tokens.
|
---|
74 | * When the option is `"never"`, `true` if there are not any spaces between given tokens.
|
---|
75 | * If given tokens are not on same line, it's always `true`.
|
---|
76 | */
|
---|
77 | function isValid(left, right) {
|
---|
78 | return (
|
---|
79 | !util.isTokenOnSameLine(left, right) ||
|
---|
80 | sourceCode.isSpaceBetweenTokens(left, right) === always
|
---|
81 | );
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Checks and reports invalid spacing style inside braces.
|
---|
86 | * @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to check.
|
---|
87 | * @returns {void}
|
---|
88 | */
|
---|
89 | function checkSpacingInsideBraces(node) {
|
---|
90 |
|
---|
91 | // Gets braces and the first/last token of content.
|
---|
92 | const openBrace = getOpenBrace(node);
|
---|
93 | const closeBrace = sourceCode.getLastToken(node);
|
---|
94 | const firstToken = sourceCode.getTokenAfter(openBrace, { includeComments: true });
|
---|
95 | const lastToken = sourceCode.getTokenBefore(closeBrace, { includeComments: true });
|
---|
96 |
|
---|
97 | // Skip if the node is invalid or empty.
|
---|
98 | if (openBrace.type !== "Punctuator" ||
|
---|
99 | openBrace.value !== "{" ||
|
---|
100 | closeBrace.type !== "Punctuator" ||
|
---|
101 | closeBrace.value !== "}" ||
|
---|
102 | firstToken === closeBrace
|
---|
103 | ) {
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | // Skip line comments for option never
|
---|
108 | if (!always && firstToken.type === "Line") {
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | // Check.
|
---|
113 | if (!isValid(openBrace, firstToken)) {
|
---|
114 | let loc = openBrace.loc;
|
---|
115 |
|
---|
116 | if (messageId === "extra") {
|
---|
117 | loc = {
|
---|
118 | start: openBrace.loc.end,
|
---|
119 | end: firstToken.loc.start
|
---|
120 | };
|
---|
121 | }
|
---|
122 |
|
---|
123 | context.report({
|
---|
124 | node,
|
---|
125 | loc,
|
---|
126 | messageId,
|
---|
127 | data: {
|
---|
128 | location: "after",
|
---|
129 | token: openBrace.value
|
---|
130 | },
|
---|
131 | fix(fixer) {
|
---|
132 | if (always) {
|
---|
133 | return fixer.insertTextBefore(firstToken, " ");
|
---|
134 | }
|
---|
135 |
|
---|
136 | return fixer.removeRange([openBrace.range[1], firstToken.range[0]]);
|
---|
137 | }
|
---|
138 | });
|
---|
139 | }
|
---|
140 | if (!isValid(lastToken, closeBrace)) {
|
---|
141 | let loc = closeBrace.loc;
|
---|
142 |
|
---|
143 | if (messageId === "extra") {
|
---|
144 | loc = {
|
---|
145 | start: lastToken.loc.end,
|
---|
146 | end: closeBrace.loc.start
|
---|
147 | };
|
---|
148 | }
|
---|
149 | context.report({
|
---|
150 | node,
|
---|
151 | loc,
|
---|
152 | messageId,
|
---|
153 | data: {
|
---|
154 | location: "before",
|
---|
155 | token: closeBrace.value
|
---|
156 | },
|
---|
157 | fix(fixer) {
|
---|
158 | if (always) {
|
---|
159 | return fixer.insertTextAfter(lastToken, " ");
|
---|
160 | }
|
---|
161 |
|
---|
162 | return fixer.removeRange([lastToken.range[1], closeBrace.range[0]]);
|
---|
163 | }
|
---|
164 | });
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | return {
|
---|
169 | BlockStatement: checkSpacingInsideBraces,
|
---|
170 | StaticBlock: checkSpacingInsideBraces,
|
---|
171 | SwitchStatement: checkSpacingInsideBraces
|
---|
172 | };
|
---|
173 | }
|
---|
174 | };
|
---|