1 | /**
|
---|
2 | * @fileoverview Disallows or enforces spaces inside of parentheses.
|
---|
3 | * @author Jonathan Rajavuori
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const astUtils = require("./utils/ast-utils");
|
---|
9 |
|
---|
10 | //------------------------------------------------------------------------------
|
---|
11 | // Rule Definition
|
---|
12 | //------------------------------------------------------------------------------
|
---|
13 |
|
---|
14 | /** @type {import('../shared/types').Rule} */
|
---|
15 | module.exports = {
|
---|
16 | meta: {
|
---|
17 | deprecated: true,
|
---|
18 | replacedBy: [],
|
---|
19 | type: "layout",
|
---|
20 |
|
---|
21 | docs: {
|
---|
22 | description: "Enforce consistent spacing inside parentheses",
|
---|
23 | recommended: false,
|
---|
24 | url: "https://eslint.org/docs/latest/rules/space-in-parens"
|
---|
25 | },
|
---|
26 |
|
---|
27 | fixable: "whitespace",
|
---|
28 |
|
---|
29 | schema: [
|
---|
30 | {
|
---|
31 | enum: ["always", "never"]
|
---|
32 | },
|
---|
33 | {
|
---|
34 | type: "object",
|
---|
35 | properties: {
|
---|
36 | exceptions: {
|
---|
37 | type: "array",
|
---|
38 | items: {
|
---|
39 | enum: ["{}", "[]", "()", "empty"]
|
---|
40 | },
|
---|
41 | uniqueItems: true
|
---|
42 | }
|
---|
43 | },
|
---|
44 | additionalProperties: false
|
---|
45 | }
|
---|
46 | ],
|
---|
47 |
|
---|
48 | messages: {
|
---|
49 | missingOpeningSpace: "There must be a space after this paren.",
|
---|
50 | missingClosingSpace: "There must be a space before this paren.",
|
---|
51 | rejectedOpeningSpace: "There should be no space after this paren.",
|
---|
52 | rejectedClosingSpace: "There should be no space before this paren."
|
---|
53 | }
|
---|
54 | },
|
---|
55 |
|
---|
56 | create(context) {
|
---|
57 | const ALWAYS = context.options[0] === "always",
|
---|
58 | exceptionsArrayOptions = (context.options[1] && context.options[1].exceptions) || [],
|
---|
59 | options = {};
|
---|
60 |
|
---|
61 | let exceptions;
|
---|
62 |
|
---|
63 | if (exceptionsArrayOptions.length) {
|
---|
64 | options.braceException = exceptionsArrayOptions.includes("{}");
|
---|
65 | options.bracketException = exceptionsArrayOptions.includes("[]");
|
---|
66 | options.parenException = exceptionsArrayOptions.includes("()");
|
---|
67 | options.empty = exceptionsArrayOptions.includes("empty");
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Produces an object with the opener and closer exception values
|
---|
72 | * @returns {Object} `openers` and `closers` exception values
|
---|
73 | * @private
|
---|
74 | */
|
---|
75 | function getExceptions() {
|
---|
76 | const openers = [],
|
---|
77 | closers = [];
|
---|
78 |
|
---|
79 | if (options.braceException) {
|
---|
80 | openers.push("{");
|
---|
81 | closers.push("}");
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (options.bracketException) {
|
---|
85 | openers.push("[");
|
---|
86 | closers.push("]");
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (options.parenException) {
|
---|
90 | openers.push("(");
|
---|
91 | closers.push(")");
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (options.empty) {
|
---|
95 | openers.push(")");
|
---|
96 | closers.push("(");
|
---|
97 | }
|
---|
98 |
|
---|
99 | return {
|
---|
100 | openers,
|
---|
101 | closers
|
---|
102 | };
|
---|
103 | }
|
---|
104 |
|
---|
105 | //--------------------------------------------------------------------------
|
---|
106 | // Helpers
|
---|
107 | //--------------------------------------------------------------------------
|
---|
108 | const sourceCode = context.sourceCode;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Determines if a token is one of the exceptions for the opener paren
|
---|
112 | * @param {Object} token The token to check
|
---|
113 | * @returns {boolean} True if the token is one of the exceptions for the opener paren
|
---|
114 | */
|
---|
115 | function isOpenerException(token) {
|
---|
116 | return exceptions.openers.includes(token.value);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Determines if a token is one of the exceptions for the closer paren
|
---|
121 | * @param {Object} token The token to check
|
---|
122 | * @returns {boolean} True if the token is one of the exceptions for the closer paren
|
---|
123 | */
|
---|
124 | function isCloserException(token) {
|
---|
125 | return exceptions.closers.includes(token.value);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Determines if an opening paren is immediately followed by a required space
|
---|
130 | * @param {Object} openingParenToken The paren token
|
---|
131 | * @param {Object} tokenAfterOpeningParen The token after it
|
---|
132 | * @returns {boolean} True if the opening paren is missing a required space
|
---|
133 | */
|
---|
134 | function openerMissingSpace(openingParenToken, tokenAfterOpeningParen) {
|
---|
135 | if (sourceCode.isSpaceBetweenTokens(openingParenToken, tokenAfterOpeningParen)) {
|
---|
136 | return false;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (!options.empty && astUtils.isClosingParenToken(tokenAfterOpeningParen)) {
|
---|
140 | return false;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (ALWAYS) {
|
---|
144 | return !isOpenerException(tokenAfterOpeningParen);
|
---|
145 | }
|
---|
146 | return isOpenerException(tokenAfterOpeningParen);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Determines if an opening paren is immediately followed by a disallowed space
|
---|
151 | * @param {Object} openingParenToken The paren token
|
---|
152 | * @param {Object} tokenAfterOpeningParen The token after it
|
---|
153 | * @returns {boolean} True if the opening paren has a disallowed space
|
---|
154 | */
|
---|
155 | function openerRejectsSpace(openingParenToken, tokenAfterOpeningParen) {
|
---|
156 | if (!astUtils.isTokenOnSameLine(openingParenToken, tokenAfterOpeningParen)) {
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (tokenAfterOpeningParen.type === "Line") {
|
---|
161 | return false;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (!sourceCode.isSpaceBetweenTokens(openingParenToken, tokenAfterOpeningParen)) {
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (ALWAYS) {
|
---|
169 | return isOpenerException(tokenAfterOpeningParen);
|
---|
170 | }
|
---|
171 | return !isOpenerException(tokenAfterOpeningParen);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Determines if a closing paren is immediately preceded by a required space
|
---|
176 | * @param {Object} tokenBeforeClosingParen The token before the paren
|
---|
177 | * @param {Object} closingParenToken The paren token
|
---|
178 | * @returns {boolean} True if the closing paren is missing a required space
|
---|
179 | */
|
---|
180 | function closerMissingSpace(tokenBeforeClosingParen, closingParenToken) {
|
---|
181 | if (sourceCode.isSpaceBetweenTokens(tokenBeforeClosingParen, closingParenToken)) {
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (!options.empty && astUtils.isOpeningParenToken(tokenBeforeClosingParen)) {
|
---|
186 | return false;
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (ALWAYS) {
|
---|
190 | return !isCloserException(tokenBeforeClosingParen);
|
---|
191 | }
|
---|
192 | return isCloserException(tokenBeforeClosingParen);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Determines if a closer paren is immediately preceded by a disallowed space
|
---|
197 | * @param {Object} tokenBeforeClosingParen The token before the paren
|
---|
198 | * @param {Object} closingParenToken The paren token
|
---|
199 | * @returns {boolean} True if the closing paren has a disallowed space
|
---|
200 | */
|
---|
201 | function closerRejectsSpace(tokenBeforeClosingParen, closingParenToken) {
|
---|
202 | if (!astUtils.isTokenOnSameLine(tokenBeforeClosingParen, closingParenToken)) {
|
---|
203 | return false;
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (!sourceCode.isSpaceBetweenTokens(tokenBeforeClosingParen, closingParenToken)) {
|
---|
207 | return false;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (ALWAYS) {
|
---|
211 | return isCloserException(tokenBeforeClosingParen);
|
---|
212 | }
|
---|
213 | return !isCloserException(tokenBeforeClosingParen);
|
---|
214 | }
|
---|
215 |
|
---|
216 | //--------------------------------------------------------------------------
|
---|
217 | // Public
|
---|
218 | //--------------------------------------------------------------------------
|
---|
219 |
|
---|
220 | return {
|
---|
221 | Program: function checkParenSpaces(node) {
|
---|
222 | exceptions = getExceptions();
|
---|
223 | const tokens = sourceCode.tokensAndComments;
|
---|
224 |
|
---|
225 | tokens.forEach((token, i) => {
|
---|
226 | const prevToken = tokens[i - 1];
|
---|
227 | const nextToken = tokens[i + 1];
|
---|
228 |
|
---|
229 | // if token is not an opening or closing paren token, do nothing
|
---|
230 | if (!astUtils.isOpeningParenToken(token) && !astUtils.isClosingParenToken(token)) {
|
---|
231 | return;
|
---|
232 | }
|
---|
233 |
|
---|
234 | // if token is an opening paren and is not followed by a required space
|
---|
235 | if (token.value === "(" && openerMissingSpace(token, nextToken)) {
|
---|
236 | context.report({
|
---|
237 | node,
|
---|
238 | loc: token.loc,
|
---|
239 | messageId: "missingOpeningSpace",
|
---|
240 | fix(fixer) {
|
---|
241 | return fixer.insertTextAfter(token, " ");
|
---|
242 | }
|
---|
243 | });
|
---|
244 | }
|
---|
245 |
|
---|
246 | // if token is an opening paren and is followed by a disallowed space
|
---|
247 | if (token.value === "(" && openerRejectsSpace(token, nextToken)) {
|
---|
248 | context.report({
|
---|
249 | node,
|
---|
250 | loc: { start: token.loc.end, end: nextToken.loc.start },
|
---|
251 | messageId: "rejectedOpeningSpace",
|
---|
252 | fix(fixer) {
|
---|
253 | return fixer.removeRange([token.range[1], nextToken.range[0]]);
|
---|
254 | }
|
---|
255 | });
|
---|
256 | }
|
---|
257 |
|
---|
258 | // if token is a closing paren and is not preceded by a required space
|
---|
259 | if (token.value === ")" && closerMissingSpace(prevToken, token)) {
|
---|
260 | context.report({
|
---|
261 | node,
|
---|
262 | loc: token.loc,
|
---|
263 | messageId: "missingClosingSpace",
|
---|
264 | fix(fixer) {
|
---|
265 | return fixer.insertTextBefore(token, " ");
|
---|
266 | }
|
---|
267 | });
|
---|
268 | }
|
---|
269 |
|
---|
270 | // if token is a closing paren and is preceded by a disallowed space
|
---|
271 | if (token.value === ")" && closerRejectsSpace(prevToken, token)) {
|
---|
272 | context.report({
|
---|
273 | node,
|
---|
274 | loc: { start: prevToken.loc.end, end: token.loc.start },
|
---|
275 | messageId: "rejectedClosingSpace",
|
---|
276 | fix(fixer) {
|
---|
277 | return fixer.removeRange([prevToken.range[1], token.range[0]]);
|
---|
278 | }
|
---|
279 | });
|
---|
280 | }
|
---|
281 | });
|
---|
282 | }
|
---|
283 | };
|
---|
284 | }
|
---|
285 | };
|
---|