1 | /**
|
---|
2 | * @fileoverview Rule to enforce line breaks after each array element
|
---|
3 | * @author Jan Peer Stöcklmair <https://github.com/JPeer264>
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 |
|
---|
7 | "use strict";
|
---|
8 |
|
---|
9 | const astUtils = 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: "Enforce line breaks after each array element",
|
---|
24 | recommended: false,
|
---|
25 | url: "https://eslint.org/docs/latest/rules/array-element-newline"
|
---|
26 | },
|
---|
27 |
|
---|
28 | fixable: "whitespace",
|
---|
29 |
|
---|
30 | schema: {
|
---|
31 | definitions: {
|
---|
32 | basicConfig: {
|
---|
33 | oneOf: [
|
---|
34 | {
|
---|
35 | enum: ["always", "never", "consistent"]
|
---|
36 | },
|
---|
37 | {
|
---|
38 | type: "object",
|
---|
39 | properties: {
|
---|
40 | multiline: {
|
---|
41 | type: "boolean"
|
---|
42 | },
|
---|
43 | minItems: {
|
---|
44 | type: ["integer", "null"],
|
---|
45 | minimum: 0
|
---|
46 | }
|
---|
47 | },
|
---|
48 | additionalProperties: false
|
---|
49 | }
|
---|
50 | ]
|
---|
51 | }
|
---|
52 | },
|
---|
53 | type: "array",
|
---|
54 | items: [
|
---|
55 | {
|
---|
56 | oneOf: [
|
---|
57 | {
|
---|
58 | $ref: "#/definitions/basicConfig"
|
---|
59 | },
|
---|
60 | {
|
---|
61 | type: "object",
|
---|
62 | properties: {
|
---|
63 | ArrayExpression: {
|
---|
64 | $ref: "#/definitions/basicConfig"
|
---|
65 | },
|
---|
66 | ArrayPattern: {
|
---|
67 | $ref: "#/definitions/basicConfig"
|
---|
68 | }
|
---|
69 | },
|
---|
70 | additionalProperties: false,
|
---|
71 | minProperties: 1
|
---|
72 | }
|
---|
73 | ]
|
---|
74 | }
|
---|
75 | ]
|
---|
76 | },
|
---|
77 |
|
---|
78 | messages: {
|
---|
79 | unexpectedLineBreak: "There should be no linebreak here.",
|
---|
80 | missingLineBreak: "There should be a linebreak after this element."
|
---|
81 | }
|
---|
82 | },
|
---|
83 |
|
---|
84 | create(context) {
|
---|
85 | const sourceCode = context.sourceCode;
|
---|
86 |
|
---|
87 | //----------------------------------------------------------------------
|
---|
88 | // Helpers
|
---|
89 | //----------------------------------------------------------------------
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Normalizes a given option value.
|
---|
93 | * @param {string|Object|undefined} providedOption An option value to parse.
|
---|
94 | * @returns {{multiline: boolean, minItems: number}} Normalized option object.
|
---|
95 | */
|
---|
96 | function normalizeOptionValue(providedOption) {
|
---|
97 | let consistent = false;
|
---|
98 | let multiline = false;
|
---|
99 | let minItems;
|
---|
100 |
|
---|
101 | const option = providedOption || "always";
|
---|
102 |
|
---|
103 | if (!option || option === "always" || option.minItems === 0) {
|
---|
104 | minItems = 0;
|
---|
105 | } else if (option === "never") {
|
---|
106 | minItems = Number.POSITIVE_INFINITY;
|
---|
107 | } else if (option === "consistent") {
|
---|
108 | consistent = true;
|
---|
109 | minItems = Number.POSITIVE_INFINITY;
|
---|
110 | } else {
|
---|
111 | multiline = Boolean(option.multiline);
|
---|
112 | minItems = option.minItems || Number.POSITIVE_INFINITY;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return { consistent, multiline, minItems };
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Normalizes a given option value.
|
---|
120 | * @param {string|Object|undefined} options An option value to parse.
|
---|
121 | * @returns {{ArrayExpression: {multiline: boolean, minItems: number}, ArrayPattern: {multiline: boolean, minItems: number}}} Normalized option object.
|
---|
122 | */
|
---|
123 | function normalizeOptions(options) {
|
---|
124 | if (options && (options.ArrayExpression || options.ArrayPattern)) {
|
---|
125 | let expressionOptions, patternOptions;
|
---|
126 |
|
---|
127 | if (options.ArrayExpression) {
|
---|
128 | expressionOptions = normalizeOptionValue(options.ArrayExpression);
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (options.ArrayPattern) {
|
---|
132 | patternOptions = normalizeOptionValue(options.ArrayPattern);
|
---|
133 | }
|
---|
134 |
|
---|
135 | return { ArrayExpression: expressionOptions, ArrayPattern: patternOptions };
|
---|
136 | }
|
---|
137 |
|
---|
138 | const value = normalizeOptionValue(options);
|
---|
139 |
|
---|
140 | return { ArrayExpression: value, ArrayPattern: value };
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Reports that there shouldn't be a line break after the first token
|
---|
145 | * @param {Token} token The token to use for the report.
|
---|
146 | * @returns {void}
|
---|
147 | */
|
---|
148 | function reportNoLineBreak(token) {
|
---|
149 | const tokenBefore = sourceCode.getTokenBefore(token, { includeComments: true });
|
---|
150 |
|
---|
151 | context.report({
|
---|
152 | loc: {
|
---|
153 | start: tokenBefore.loc.end,
|
---|
154 | end: token.loc.start
|
---|
155 | },
|
---|
156 | messageId: "unexpectedLineBreak",
|
---|
157 | fix(fixer) {
|
---|
158 | if (astUtils.isCommentToken(tokenBefore)) {
|
---|
159 | return null;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (!astUtils.isTokenOnSameLine(tokenBefore, token)) {
|
---|
163 | return fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], " ");
|
---|
164 | }
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * This will check if the comma is on the same line as the next element
|
---|
168 | * Following array:
|
---|
169 | * [
|
---|
170 | * 1
|
---|
171 | * , 2
|
---|
172 | * , 3
|
---|
173 | * ]
|
---|
174 | *
|
---|
175 | * will be fixed to:
|
---|
176 | * [
|
---|
177 | * 1, 2, 3
|
---|
178 | * ]
|
---|
179 | */
|
---|
180 | const twoTokensBefore = sourceCode.getTokenBefore(tokenBefore, { includeComments: true });
|
---|
181 |
|
---|
182 | if (astUtils.isCommentToken(twoTokensBefore)) {
|
---|
183 | return null;
|
---|
184 | }
|
---|
185 |
|
---|
186 | return fixer.replaceTextRange([twoTokensBefore.range[1], tokenBefore.range[0]], "");
|
---|
187 |
|
---|
188 | }
|
---|
189 | });
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Reports that there should be a line break after the first token
|
---|
194 | * @param {Token} token The token to use for the report.
|
---|
195 | * @returns {void}
|
---|
196 | */
|
---|
197 | function reportRequiredLineBreak(token) {
|
---|
198 | const tokenBefore = sourceCode.getTokenBefore(token, { includeComments: true });
|
---|
199 |
|
---|
200 | context.report({
|
---|
201 | loc: {
|
---|
202 | start: tokenBefore.loc.end,
|
---|
203 | end: token.loc.start
|
---|
204 | },
|
---|
205 | messageId: "missingLineBreak",
|
---|
206 | fix(fixer) {
|
---|
207 | return fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], "\n");
|
---|
208 | }
|
---|
209 | });
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Reports a given node if it violated this rule.
|
---|
214 | * @param {ASTNode} node A node to check. This is an ObjectExpression node or an ObjectPattern node.
|
---|
215 | * @returns {void}
|
---|
216 | */
|
---|
217 | function check(node) {
|
---|
218 | const elements = node.elements;
|
---|
219 | const normalizedOptions = normalizeOptions(context.options[0]);
|
---|
220 | const options = normalizedOptions[node.type];
|
---|
221 |
|
---|
222 | if (!options) {
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | let elementBreak = false;
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * MULTILINE: true
|
---|
230 | * loop through every element and check
|
---|
231 | * if at least one element has linebreaks inside
|
---|
232 | * this ensures that following is not valid (due to elements are on the same line):
|
---|
233 | *
|
---|
234 | * [
|
---|
235 | * 1,
|
---|
236 | * 2,
|
---|
237 | * 3
|
---|
238 | * ]
|
---|
239 | */
|
---|
240 | if (options.multiline) {
|
---|
241 | elementBreak = elements
|
---|
242 | .filter(element => element !== null)
|
---|
243 | .some(element => element.loc.start.line !== element.loc.end.line);
|
---|
244 | }
|
---|
245 |
|
---|
246 | let linebreaksCount = 0;
|
---|
247 |
|
---|
248 | for (let i = 0; i < node.elements.length; i++) {
|
---|
249 | const element = node.elements[i];
|
---|
250 |
|
---|
251 | const previousElement = elements[i - 1];
|
---|
252 |
|
---|
253 | if (i === 0 || element === null || previousElement === null) {
|
---|
254 | continue;
|
---|
255 | }
|
---|
256 |
|
---|
257 | const commaToken = sourceCode.getFirstTokenBetween(previousElement, element, astUtils.isCommaToken);
|
---|
258 | const lastTokenOfPreviousElement = sourceCode.getTokenBefore(commaToken);
|
---|
259 | const firstTokenOfCurrentElement = sourceCode.getTokenAfter(commaToken);
|
---|
260 |
|
---|
261 | if (!astUtils.isTokenOnSameLine(lastTokenOfPreviousElement, firstTokenOfCurrentElement)) {
|
---|
262 | linebreaksCount++;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | const needsLinebreaks = (
|
---|
267 | elements.length >= options.minItems ||
|
---|
268 | (
|
---|
269 | options.multiline &&
|
---|
270 | elementBreak
|
---|
271 | ) ||
|
---|
272 | (
|
---|
273 | options.consistent &&
|
---|
274 | linebreaksCount > 0 &&
|
---|
275 | linebreaksCount < node.elements.length
|
---|
276 | )
|
---|
277 | );
|
---|
278 |
|
---|
279 | elements.forEach((element, i) => {
|
---|
280 | const previousElement = elements[i - 1];
|
---|
281 |
|
---|
282 | if (i === 0 || element === null || previousElement === null) {
|
---|
283 | return;
|
---|
284 | }
|
---|
285 |
|
---|
286 | const commaToken = sourceCode.getFirstTokenBetween(previousElement, element, astUtils.isCommaToken);
|
---|
287 | const lastTokenOfPreviousElement = sourceCode.getTokenBefore(commaToken);
|
---|
288 | const firstTokenOfCurrentElement = sourceCode.getTokenAfter(commaToken);
|
---|
289 |
|
---|
290 | if (needsLinebreaks) {
|
---|
291 | if (astUtils.isTokenOnSameLine(lastTokenOfPreviousElement, firstTokenOfCurrentElement)) {
|
---|
292 | reportRequiredLineBreak(firstTokenOfCurrentElement);
|
---|
293 | }
|
---|
294 | } else {
|
---|
295 | if (!astUtils.isTokenOnSameLine(lastTokenOfPreviousElement, firstTokenOfCurrentElement)) {
|
---|
296 | reportNoLineBreak(firstTokenOfCurrentElement);
|
---|
297 | }
|
---|
298 | }
|
---|
299 | });
|
---|
300 | }
|
---|
301 |
|
---|
302 | //----------------------------------------------------------------------
|
---|
303 | // Public
|
---|
304 | //----------------------------------------------------------------------
|
---|
305 |
|
---|
306 | return {
|
---|
307 | ArrayPattern: check,
|
---|
308 | ArrayExpression: check
|
---|
309 | };
|
---|
310 | }
|
---|
311 | };
|
---|