2 * @fileoverview Disallows or enforces spaces inside of array brackets.
3 * @author Jamund Ferguson
7 const astUtils
= require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description
: "enforce consistent spacing inside array brackets",
20 url
: "https://eslint.org/docs/rules/array-bracket-spacing"
23 fixable
: "whitespace",
27 enum: ["always", "never"]
42 additionalProperties
: false
47 unexpectedSpaceAfter
: "There should be no space after '{{tokenValue}}'.",
48 unexpectedSpaceBefore
: "There should be no space before '{{tokenValue}}'.",
49 missingSpaceAfter
: "A space is required after '{{tokenValue}}'.",
50 missingSpaceBefore
: "A space is required before '{{tokenValue}}'."
54 const spaced
= context
.options
[0] === "always",
55 sourceCode
= context
.getSourceCode();
58 * Determines whether an option is set, relative to the spacing option.
59 * If spaced is "always", then check whether option is set to false.
60 * If spaced is "never", then check whether option is set to true.
61 * @param {Object} option The option to exclude.
62 * @returns {boolean} Whether or not the property is excluded.
64 function isOptionSet(option
) {
65 return context
.options
[1] ? context
.options
[1][option
] === !spaced
: false;
70 singleElementException
: isOptionSet("singleValue"),
71 objectsInArraysException
: isOptionSet("objectsInArrays"),
72 arraysInArraysException
: isOptionSet("arraysInArrays")
75 //--------------------------------------------------------------------------
77 //--------------------------------------------------------------------------
80 * Reports that there shouldn't be a space after the first token
81 * @param {ASTNode} node The node to report in the event of an error.
82 * @param {Token} token The token to use for the report.
85 function reportNoBeginningSpace(node
, token
) {
86 const nextToken
= sourceCode
.getTokenAfter(token
);
90 loc
: { start
: token
.loc
.end
, end
: nextToken
.loc
.start
},
91 messageId
: "unexpectedSpaceAfter",
93 tokenValue
: token
.value
96 return fixer
.removeRange([token
.range
[1], nextToken
.range
[0]]);
102 * Reports that there shouldn't be a space before the last token
103 * @param {ASTNode} node The node to report in the event of an error.
104 * @param {Token} token The token to use for the report.
107 function reportNoEndingSpace(node
, token
) {
108 const previousToken
= sourceCode
.getTokenBefore(token
);
112 loc
: { start
: previousToken
.loc
.end
, end
: token
.loc
.start
},
113 messageId
: "unexpectedSpaceBefore",
115 tokenValue
: token
.value
118 return fixer
.removeRange([previousToken
.range
[1], token
.range
[0]]);
124 * Reports that there should be a space after the first token
125 * @param {ASTNode} node The node to report in the event of an error.
126 * @param {Token} token The token to use for the report.
129 function reportRequiredBeginningSpace(node
, token
) {
133 messageId
: "missingSpaceAfter",
135 tokenValue
: token
.value
138 return fixer
.insertTextAfter(token
, " ");
144 * Reports that there should be a space before the last token
145 * @param {ASTNode} node The node to report in the event of an error.
146 * @param {Token} token The token to use for the report.
149 function reportRequiredEndingSpace(node
, token
) {
153 messageId
: "missingSpaceBefore",
155 tokenValue
: token
.value
158 return fixer
.insertTextBefore(token
, " ");
164 * Determines if a node is an object type
165 * @param {ASTNode} node The node to check.
166 * @returns {boolean} Whether or not the node is an object type.
168 function isObjectType(node
) {
169 return node
&& (node
.type
=== "ObjectExpression" || node
.type
=== "ObjectPattern");
173 * Determines if a node is an array type
174 * @param {ASTNode} node The node to check.
175 * @returns {boolean} Whether or not the node is an array type.
177 function isArrayType(node
) {
178 return node
&& (node
.type
=== "ArrayExpression" || node
.type
=== "ArrayPattern");
182 * Validates the spacing around array brackets
183 * @param {ASTNode} node The node we're checking for spacing
186 function validateArraySpacing(node
) {
187 if (options
.spaced
&& node
.elements
.length
=== 0) {
191 const first
= sourceCode
.getFirstToken(node
),
192 second
= sourceCode
.getFirstToken(node
, 1),
193 last
= node
.typeAnnotation
194 ? sourceCode
.getTokenBefore(node
.typeAnnotation
)
195 : sourceCode
.getLastToken(node
),
196 penultimate
= sourceCode
.getTokenBefore(last
),
197 firstElement
= node
.elements
[0],
198 lastElement
= node
.elements
[node
.elements
.length
- 1];
200 const openingBracketMustBeSpaced
=
201 options
.objectsInArraysException
&& isObjectType(firstElement
) ||
202 options
.arraysInArraysException
&& isArrayType(firstElement
) ||
203 options
.singleElementException
&& node
.elements
.length
=== 1
204 ? !options
.spaced
: options
.spaced
;
206 const closingBracketMustBeSpaced
=
207 options
.objectsInArraysException
&& isObjectType(lastElement
) ||
208 options
.arraysInArraysException
&& isArrayType(lastElement
) ||
209 options
.singleElementException
&& node
.elements
.length
=== 1
210 ? !options
.spaced
: options
.spaced
;
212 if (astUtils
.isTokenOnSameLine(first
, second
)) {
213 if (openingBracketMustBeSpaced
&& !sourceCode
.isSpaceBetweenTokens(first
, second
)) {
214 reportRequiredBeginningSpace(node
, first
);
216 if (!openingBracketMustBeSpaced
&& sourceCode
.isSpaceBetweenTokens(first
, second
)) {
217 reportNoBeginningSpace(node
, first
);
221 if (first
!== penultimate
&& astUtils
.isTokenOnSameLine(penultimate
, last
)) {
222 if (closingBracketMustBeSpaced
&& !sourceCode
.isSpaceBetweenTokens(penultimate
, last
)) {
223 reportRequiredEndingSpace(node
, last
);
225 if (!closingBracketMustBeSpaced
&& sourceCode
.isSpaceBetweenTokens(penultimate
, last
)) {
226 reportNoEndingSpace(node
, last
);
231 //--------------------------------------------------------------------------
233 //--------------------------------------------------------------------------
236 ArrayPattern
: validateArraySpacing
,
237 ArrayExpression
: validateArraySpacing