Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * @fileoverview Rule to enforce linebreaks after open and before close array brackets |
| 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 linebreaks after opening and before closing array brackets", |
| 20 | category: "Stylistic Issues", |
| 21 | recommended: false, |
| 22 | url: "https://eslint.org/docs/rules/array-bracket-newline" |
| 23 | }, |
| 24 | |
| 25 | fixable: "whitespace", |
| 26 | |
| 27 | schema: [ |
| 28 | { |
| 29 | oneOf: [ |
| 30 | { |
| 31 | enum: ["always", "never", "consistent"] |
| 32 | }, |
| 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 | |
| 50 | messages: { |
| 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 ']'." |
| 55 | } |
| 56 | }, |
| 57 | |
| 58 | create(context) { |
| 59 | const sourceCode = context.getSourceCode(); |
| 60 | |
| 61 | |
| 62 | //---------------------------------------------------------------------- |
| 63 | // Helpers |
| 64 | //---------------------------------------------------------------------- |
| 65 | |
| 66 | /** |
| 67 | * Normalizes a given option value. |
| 68 | * |
| 69 | * @param {string|Object|undefined} option - An option value to parse. |
| 70 | * @returns {{multiline: boolean, minItems: number}} Normalized option object. |
| 71 | */ |
| 72 | function normalizeOptionValue(option) { |
| 73 | let consistent = false; |
| 74 | let multiline = false; |
| 75 | let minItems = 0; |
| 76 | |
| 77 | if (option) { |
| 78 | if (option === "consistent") { |
| 79 | consistent = true; |
| 80 | minItems = Number.POSITIVE_INFINITY; |
| 81 | } else if (option === "always" || option.minItems === 0) { |
| 82 | minItems = 0; |
| 83 | } else if (option === "never") { |
| 84 | minItems = Number.POSITIVE_INFINITY; |
| 85 | } else { |
| 86 | multiline = Boolean(option.multiline); |
| 87 | minItems = option.minItems || Number.POSITIVE_INFINITY; |
| 88 | } |
| 89 | } else { |
| 90 | consistent = false; |
| 91 | multiline = true; |
| 92 | minItems = Number.POSITIVE_INFINITY; |
| 93 | } |
| 94 | |
| 95 | return { consistent, multiline, minItems }; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Normalizes a given option value. |
| 100 | * |
| 101 | * @param {string|Object|undefined} options - An option value to parse. |
| 102 | * @returns {{ArrayExpression: {multiline: boolean, minItems: number}, ArrayPattern: {multiline: boolean, minItems: number}}} Normalized option object. |
| 103 | */ |
| 104 | function normalizeOptions(options) { |
| 105 | const value = normalizeOptionValue(options); |
| 106 | |
| 107 | return { ArrayExpression: value, ArrayPattern: value }; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Reports that there shouldn't be a linebreak after the first token |
| 112 | * @param {ASTNode} node - The node to report in the event of an error. |
| 113 | * @param {Token} token - The token to use for the report. |
| 114 | * @returns {void} |
| 115 | */ |
| 116 | function reportNoBeginningLinebreak(node, token) { |
| 117 | context.report({ |
| 118 | node, |
| 119 | loc: token.loc, |
| 120 | messageId: "unexpectedOpeningLinebreak", |
| 121 | fix(fixer) { |
| 122 | const nextToken = sourceCode.getTokenAfter(token, { includeComments: true }); |
| 123 | |
| 124 | if (astUtils.isCommentToken(nextToken)) { |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | return fixer.removeRange([token.range[1], nextToken.range[0]]); |
| 129 | } |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Reports that there shouldn't be a linebreak before the last token |
| 135 | * @param {ASTNode} node - The node to report in the event of an error. |
| 136 | * @param {Token} token - The token to use for the report. |
| 137 | * @returns {void} |
| 138 | */ |
| 139 | function reportNoEndingLinebreak(node, token) { |
| 140 | context.report({ |
| 141 | node, |
| 142 | loc: token.loc, |
| 143 | messageId: "unexpectedClosingLinebreak", |
| 144 | fix(fixer) { |
| 145 | const previousToken = sourceCode.getTokenBefore(token, { includeComments: true }); |
| 146 | |
| 147 | if (astUtils.isCommentToken(previousToken)) { |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | return fixer.removeRange([previousToken.range[1], token.range[0]]); |
| 152 | } |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Reports that there should be a linebreak after the first token |
| 158 | * @param {ASTNode} node - The node to report in the event of an error. |
| 159 | * @param {Token} token - The token to use for the report. |
| 160 | * @returns {void} |
| 161 | */ |
| 162 | function reportRequiredBeginningLinebreak(node, token) { |
| 163 | context.report({ |
| 164 | node, |
| 165 | loc: token.loc, |
| 166 | messageId: "missingOpeningLinebreak", |
| 167 | fix(fixer) { |
| 168 | return fixer.insertTextAfter(token, "\n"); |
| 169 | } |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Reports that there should be a linebreak before the last token |
| 175 | * @param {ASTNode} node - The node to report in the event of an error. |
| 176 | * @param {Token} token - The token to use for the report. |
| 177 | * @returns {void} |
| 178 | */ |
| 179 | function reportRequiredEndingLinebreak(node, token) { |
| 180 | context.report({ |
| 181 | node, |
| 182 | loc: token.loc, |
| 183 | messageId: "missingClosingLinebreak", |
| 184 | fix(fixer) { |
| 185 | return fixer.insertTextBefore(token, "\n"); |
| 186 | } |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Reports a given node if it violated this rule. |
| 192 | * |
| 193 | * @param {ASTNode} node - A node to check. This is an ArrayExpression node or an ArrayPattern node. |
| 194 | * @returns {void} |
| 195 | */ |
| 196 | function check(node) { |
| 197 | const elements = node.elements; |
| 198 | const normalizedOptions = normalizeOptions(context.options[0]); |
| 199 | const options = normalizedOptions[node.type]; |
| 200 | const openBracket = sourceCode.getFirstToken(node); |
| 201 | const closeBracket = sourceCode.getLastToken(node); |
| 202 | const firstIncComment = sourceCode.getTokenAfter(openBracket, { includeComments: true }); |
| 203 | const lastIncComment = sourceCode.getTokenBefore(closeBracket, { includeComments: true }); |
| 204 | const first = sourceCode.getTokenAfter(openBracket); |
| 205 | const last = sourceCode.getTokenBefore(closeBracket); |
| 206 | |
| 207 | const needsLinebreaks = ( |
| 208 | elements.length >= options.minItems || |
| 209 | ( |
| 210 | options.multiline && |
| 211 | elements.length > 0 && |
| 212 | firstIncComment.loc.start.line !== lastIncComment.loc.end.line |
| 213 | ) || |
| 214 | ( |
| 215 | elements.length === 0 && |
| 216 | firstIncComment.type === "Block" && |
| 217 | firstIncComment.loc.start.line !== lastIncComment.loc.end.line && |
| 218 | firstIncComment === lastIncComment |
| 219 | ) || |
| 220 | ( |
| 221 | options.consistent && |
| 222 | firstIncComment.loc.start.line !== openBracket.loc.end.line |
| 223 | ) |
| 224 | ); |
| 225 | |
| 226 | /* |
| 227 | * Use tokens or comments to check multiline or not. |
| 228 | * But use only tokens to check whether linebreaks are needed. |
| 229 | * This allows: |
| 230 | * var arr = [ // eslint-disable-line foo |
| 231 | * 'a' |
| 232 | * ] |
| 233 | */ |
| 234 | |
| 235 | if (needsLinebreaks) { |
| 236 | if (astUtils.isTokenOnSameLine(openBracket, first)) { |
| 237 | reportRequiredBeginningLinebreak(node, openBracket); |
| 238 | } |
| 239 | if (astUtils.isTokenOnSameLine(last, closeBracket)) { |
| 240 | reportRequiredEndingLinebreak(node, closeBracket); |
| 241 | } |
| 242 | } else { |
| 243 | if (!astUtils.isTokenOnSameLine(openBracket, first)) { |
| 244 | reportNoBeginningLinebreak(node, openBracket); |
| 245 | } |
| 246 | if (!astUtils.isTokenOnSameLine(last, closeBracket)) { |
| 247 | reportNoEndingLinebreak(node, closeBracket); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | //---------------------------------------------------------------------- |
| 253 | // Public |
| 254 | //---------------------------------------------------------------------- |
| 255 | |
| 256 | return { |
| 257 | ArrayPattern: check, |
| 258 | ArrayExpression: check |
| 259 | }; |
| 260 | } |
| 261 | }; |