[d565449] | 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 | * @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 linebreaks after opening and before closing array brackets",
|
---|
| 24 | recommended: false,
|
---|
| 25 | url: "https://eslint.org/docs/latest/rules/array-bracket-newline"
|
---|
| 26 | },
|
---|
| 27 |
|
---|
| 28 | fixable: "whitespace",
|
---|
| 29 |
|
---|
| 30 | schema: [
|
---|
| 31 | {
|
---|
| 32 | oneOf: [
|
---|
| 33 | {
|
---|
| 34 | enum: ["always", "never", "consistent"]
|
---|
| 35 | },
|
---|
| 36 | {
|
---|
| 37 | type: "object",
|
---|
| 38 | properties: {
|
---|
| 39 | multiline: {
|
---|
| 40 | type: "boolean"
|
---|
| 41 | },
|
---|
| 42 | minItems: {
|
---|
| 43 | type: ["integer", "null"],
|
---|
| 44 | minimum: 0
|
---|
| 45 | }
|
---|
| 46 | },
|
---|
| 47 | additionalProperties: false
|
---|
| 48 | }
|
---|
| 49 | ]
|
---|
| 50 | }
|
---|
| 51 | ],
|
---|
| 52 |
|
---|
| 53 | messages: {
|
---|
| 54 | unexpectedOpeningLinebreak: "There should be no linebreak after '['.",
|
---|
| 55 | unexpectedClosingLinebreak: "There should be no linebreak before ']'.",
|
---|
| 56 | missingOpeningLinebreak: "A linebreak is required after '['.",
|
---|
| 57 | missingClosingLinebreak: "A linebreak is required before ']'."
|
---|
| 58 | }
|
---|
| 59 | },
|
---|
| 60 |
|
---|
| 61 | create(context) {
|
---|
| 62 | const sourceCode = context.sourceCode;
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | //----------------------------------------------------------------------
|
---|
| 66 | // Helpers
|
---|
| 67 | //----------------------------------------------------------------------
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Normalizes a given option value.
|
---|
| 71 | * @param {string|Object|undefined} option An option value to parse.
|
---|
| 72 | * @returns {{multiline: boolean, minItems: number}} Normalized option object.
|
---|
| 73 | */
|
---|
| 74 | function normalizeOptionValue(option) {
|
---|
| 75 | let consistent = false;
|
---|
| 76 | let multiline = false;
|
---|
| 77 | let minItems = 0;
|
---|
| 78 |
|
---|
| 79 | if (option) {
|
---|
| 80 | if (option === "consistent") {
|
---|
| 81 | consistent = true;
|
---|
| 82 | minItems = Number.POSITIVE_INFINITY;
|
---|
| 83 | } else if (option === "always" || option.minItems === 0) {
|
---|
| 84 | minItems = 0;
|
---|
| 85 | } else if (option === "never") {
|
---|
| 86 | minItems = Number.POSITIVE_INFINITY;
|
---|
| 87 | } else {
|
---|
| 88 | multiline = Boolean(option.multiline);
|
---|
| 89 | minItems = option.minItems || Number.POSITIVE_INFINITY;
|
---|
| 90 | }
|
---|
| 91 | } else {
|
---|
| 92 | consistent = false;
|
---|
| 93 | multiline = true;
|
---|
| 94 | minItems = Number.POSITIVE_INFINITY;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return { consistent, multiline, minItems };
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * Normalizes a given option value.
|
---|
| 102 | * @param {string|Object|undefined} options An option value to parse.
|
---|
| 103 | * @returns {{ArrayExpression: {multiline: boolean, minItems: number}, ArrayPattern: {multiline: boolean, minItems: number}}} Normalized option object.
|
---|
| 104 | */
|
---|
| 105 | function normalizeOptions(options) {
|
---|
| 106 | const value = normalizeOptionValue(options);
|
---|
| 107 |
|
---|
| 108 | return { ArrayExpression: value, ArrayPattern: value };
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Reports that there shouldn't be a linebreak after the first token
|
---|
| 113 | * @param {ASTNode} node The node to report in the event of an error.
|
---|
| 114 | * @param {Token} token The token to use for the report.
|
---|
| 115 | * @returns {void}
|
---|
| 116 | */
|
---|
| 117 | function reportNoBeginningLinebreak(node, token) {
|
---|
| 118 | context.report({
|
---|
| 119 | node,
|
---|
| 120 | loc: token.loc,
|
---|
| 121 | messageId: "unexpectedOpeningLinebreak",
|
---|
| 122 | fix(fixer) {
|
---|
| 123 | const nextToken = sourceCode.getTokenAfter(token, { includeComments: true });
|
---|
| 124 |
|
---|
| 125 | if (astUtils.isCommentToken(nextToken)) {
|
---|
| 126 | return null;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | return fixer.removeRange([token.range[1], nextToken.range[0]]);
|
---|
| 130 | }
|
---|
| 131 | });
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Reports that there shouldn't be a linebreak before the last token
|
---|
| 136 | * @param {ASTNode} node The node to report in the event of an error.
|
---|
| 137 | * @param {Token} token The token to use for the report.
|
---|
| 138 | * @returns {void}
|
---|
| 139 | */
|
---|
| 140 | function reportNoEndingLinebreak(node, token) {
|
---|
| 141 | context.report({
|
---|
| 142 | node,
|
---|
| 143 | loc: token.loc,
|
---|
| 144 | messageId: "unexpectedClosingLinebreak",
|
---|
| 145 | fix(fixer) {
|
---|
| 146 | const previousToken = sourceCode.getTokenBefore(token, { includeComments: true });
|
---|
| 147 |
|
---|
| 148 | if (astUtils.isCommentToken(previousToken)) {
|
---|
| 149 | return null;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | return fixer.removeRange([previousToken.range[1], token.range[0]]);
|
---|
| 153 | }
|
---|
| 154 | });
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * Reports that there should be a linebreak after the first token
|
---|
| 159 | * @param {ASTNode} node The node to report in the event of an error.
|
---|
| 160 | * @param {Token} token The token to use for the report.
|
---|
| 161 | * @returns {void}
|
---|
| 162 | */
|
---|
| 163 | function reportRequiredBeginningLinebreak(node, token) {
|
---|
| 164 | context.report({
|
---|
| 165 | node,
|
---|
| 166 | loc: token.loc,
|
---|
| 167 | messageId: "missingOpeningLinebreak",
|
---|
| 168 | fix(fixer) {
|
---|
| 169 | return fixer.insertTextAfter(token, "\n");
|
---|
| 170 | }
|
---|
| 171 | });
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /**
|
---|
| 175 | * Reports that there should be a linebreak before the last token
|
---|
| 176 | * @param {ASTNode} node The node to report in the event of an error.
|
---|
| 177 | * @param {Token} token The token to use for the report.
|
---|
| 178 | * @returns {void}
|
---|
| 179 | */
|
---|
| 180 | function reportRequiredEndingLinebreak(node, token) {
|
---|
| 181 | context.report({
|
---|
| 182 | node,
|
---|
| 183 | loc: token.loc,
|
---|
| 184 | messageId: "missingClosingLinebreak",
|
---|
| 185 | fix(fixer) {
|
---|
| 186 | return fixer.insertTextBefore(token, "\n");
|
---|
| 187 | }
|
---|
| 188 | });
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /**
|
---|
| 192 | * Reports a given node if it violated this rule.
|
---|
| 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 | openBracket.loc.end.line !== first.loc.start.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 | };
|
---|