2 * @fileoverview Rule to enforce linebreaks after open and before close array brackets
3 * @author Jan Peer Stöcklmair <https://github.com/JPeer264>
8 const astUtils = require("./utils/ast-utils");
10 //------------------------------------------------------------------------------
12 //------------------------------------------------------------------------------
19 description: "enforce linebreaks after opening and before closing array brackets",
20 category: "Stylistic Issues",
22 url: "https://eslint.org/docs/rules/array-bracket-newline"
25 fixable: "whitespace",
31 enum: ["always", "never", "consistent"]
40 type: ["integer", "null"],
44 additionalProperties: false
51 unexpectedOpeningLinebreak: "There should be no linebreak after '['.",
52 unexpectedClosingLinebreak: "There should be no linebreak before ']'.",
53 missingOpeningLinebreak: "A linebreak is required after '['.",
54 missingClosingLinebreak: "A linebreak is required before ']'."
59 const sourceCode = context.getSourceCode();
62 //----------------------------------------------------------------------
64 //----------------------------------------------------------------------
67 * Normalizes a given option value.
68 * @param {string|Object|undefined} option An option value to parse.
69 * @returns {{multiline: boolean, minItems: number}} Normalized option object.
71 function normalizeOptionValue(option) {
72 let consistent = false;
73 let multiline = false;
77 if (option === "consistent") {
79 minItems = Number.POSITIVE_INFINITY;
80 } else if (option === "always" || option.minItems === 0) {
82 } else if (option === "never") {
83 minItems = Number.POSITIVE_INFINITY;
85 multiline = Boolean(option.multiline);
86 minItems = option.minItems || Number.POSITIVE_INFINITY;
91 minItems = Number.POSITIVE_INFINITY;
94 return { consistent, multiline, minItems };
98 * Normalizes a given option value.
99 * @param {string|Object|undefined} options An option value to parse.
100 * @returns {{ArrayExpression: {multiline: boolean, minItems: number}, ArrayPattern: {multiline: boolean, minItems: number}}} Normalized option object.
102 function normalizeOptions(options) {
103 const value = normalizeOptionValue(options);
105 return { ArrayExpression: value, ArrayPattern: value };
109 * Reports that there shouldn't be a linebreak after the first token
110 * @param {ASTNode} node The node to report in the event of an error.
111 * @param {Token} token The token to use for the report.
114 function reportNoBeginningLinebreak(node, token) {
118 messageId: "unexpectedOpeningLinebreak",
120 const nextToken = sourceCode.getTokenAfter(token, { includeComments: true });
122 if (astUtils.isCommentToken(nextToken)) {
126 return fixer.removeRange([token.range[1], nextToken.range[0]]);
132 * Reports that there shouldn't be a linebreak before the last token
133 * @param {ASTNode} node The node to report in the event of an error.
134 * @param {Token} token The token to use for the report.
137 function reportNoEndingLinebreak(node, token) {
141 messageId: "unexpectedClosingLinebreak",
143 const previousToken = sourceCode.getTokenBefore(token, { includeComments: true });
145 if (astUtils.isCommentToken(previousToken)) {
149 return fixer.removeRange([previousToken.range[1], token.range[0]]);
155 * Reports that there should be a linebreak after the first token
156 * @param {ASTNode} node The node to report in the event of an error.
157 * @param {Token} token The token to use for the report.
160 function reportRequiredBeginningLinebreak(node, token) {
164 messageId: "missingOpeningLinebreak",
166 return fixer.insertTextAfter(token, "\n");
172 * Reports that there should be a linebreak before the last token
173 * @param {ASTNode} node The node to report in the event of an error.
174 * @param {Token} token The token to use for the report.
177 function reportRequiredEndingLinebreak(node, token) {
181 messageId: "missingClosingLinebreak",
183 return fixer.insertTextBefore(token, "\n");
189 * Reports a given node if it violated this rule.
190 * @param {ASTNode} node A node to check. This is an ArrayExpression node or an ArrayPattern node.
193 function check(node) {
194 const elements = node.elements;
195 const normalizedOptions = normalizeOptions(context.options[0]);
196 const options = normalizedOptions[node.type];
197 const openBracket = sourceCode.getFirstToken(node);
198 const closeBracket = sourceCode.getLastToken(node);
199 const firstIncComment = sourceCode.getTokenAfter(openBracket, { includeComments: true });
200 const lastIncComment = sourceCode.getTokenBefore(closeBracket, { includeComments: true });
201 const first = sourceCode.getTokenAfter(openBracket);
202 const last = sourceCode.getTokenBefore(closeBracket);
204 const needsLinebreaks = (
205 elements.length >= options.minItems ||
208 elements.length > 0 &&
209 firstIncComment.loc.start.line !== lastIncComment.loc.end.line
212 elements.length === 0 &&
213 firstIncComment.type === "Block" &&
214 firstIncComment.loc.start.line !== lastIncComment.loc.end.line &&
215 firstIncComment === lastIncComment
218 options.consistent &&
219 openBracket.loc.end.line !== first.loc.start.line
224 * Use tokens or comments to check multiline or not.
225 * But use only tokens to check whether linebreaks are needed.
227 * var arr = [ // eslint-disable-line foo
232 if (needsLinebreaks) {
233 if (astUtils.isTokenOnSameLine(openBracket, first)) {
234 reportRequiredBeginningLinebreak(node, openBracket);
236 if (astUtils.isTokenOnSameLine(last, closeBracket)) {
237 reportRequiredEndingLinebreak(node, closeBracket);
240 if (!astUtils.isTokenOnSameLine(openBracket, first)) {
241 reportNoBeginningLinebreak(node, openBracket);
243 if (!astUtils.isTokenOnSameLine(last, closeBracket)) {
244 reportNoEndingLinebreak(node, closeBracket);
249 //----------------------------------------------------------------------
251 //----------------------------------------------------------------------
255 ArrayExpression: check