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