From e59d77536bd8db57e8a75cd5245f6f320aa699f8 Mon Sep 17 00:00:00 2001 From: jacobparish Date: Fri, 17 Jan 2020 10:27:06 -0600 Subject: [PATCH] Update: Separate pattern/expression options for array-element-newline (#11796) * Update: Separate pattern/expression options for array-element-newline * Docs: Add pattern vs expression examples for array-element-newline --- docs/rules/array-element-newline.md | 71 ++++++++++++++++++++ lib/rules/array-element-newline.js | 82 ++++++++++++++++++------ tests/lib/rules/array-element-newline.js | 57 +++++++++++++++- 3 files changed, 188 insertions(+), 22 deletions(-) diff --git a/docs/rules/array-element-newline.md b/docs/rules/array-element-newline.md index f67661f60a6..44099056606 100644 --- a/docs/rules/array-element-newline.md +++ b/docs/rules/array-element-newline.md @@ -19,6 +19,20 @@ Or an object option (Requires line breaks if any of properties is satisfied. Oth * `"multiline": ` requires line breaks if there are line breaks inside elements. If this is false, this condition is disabled. * `"minItems": ` requires line breaks if the number of elements is at least the given integer. If this is 0, this condition will act the same as the option `"always"`. If this is `null` (the default), this condition is disabled. +Alternatively, different configurations can be specified for array expressions and array patterns: + +```json +{ + "array-element-newline": ["error", { + "ArrayExpression": "consistent", + "ArrayPattern": { "minItems": 3 }, + }] +} +``` + +* `"ArrayExpression"` configuration for array expressions (if unspecified, this rule will not apply to array expressions) +* `"ArrayPattern"` configuration for array patterns of destructuring assignments (if unspecified, this rule will not apply to array patterns) + ### always Examples of **incorrect** code for this rule with the default `"always"` option: @@ -283,6 +297,63 @@ var e = [ ]; ``` +### ArrayExpression and ArrayPattern + +Examples of **incorrect** code for this rule with the `{ "ArrayExpression": "always", "ArrayPattern": "never" }` options: + +```js +/*eslint array-element-newline: ["error", { "ArrayExpression": "always", "ArrayPattern": "never" }]*/ + +var a = [1, 2]; +var b = [1, 2, 3]; +var c = [ + function foo() { + dosomething(); + }, function bar() { + dosomething(); + } +]; + +var [d, + e] = arr; +var [f, + g, + h] = arr; +var [i = function foo() { + dosomething() +}, +j = function bar() { + dosomething() +}] = arr +``` + +Examples of **correct** code for this rule with the `{ "ArrayExpression": "always", "ArrayPattern": "never" }` options: + +```js +/*eslint object-curly-newline: ["error", { "ArrayExpression": "always", "ArrayPattern": "never" }]*/ + +var a = [1, + 2]; +var b = [1, + 2, + 3]; +var c = [ + function foo() { + dosomething(); + }, + function bar() { + dosomething(); + } +]; + +var [d, e] = arr +var [f, g, h] = arr +var [i = function foo() { + dosomething() +}, j = function bar() { + dosomething() +}] = arr +``` ## When Not To Use It diff --git a/lib/rules/array-element-newline.js b/lib/rules/array-element-newline.js index 1da67667bee..b7a967865b9 100644 --- a/lib/rules/array-element-newline.js +++ b/lib/rules/array-element-newline.js @@ -24,28 +24,52 @@ module.exports = { fixable: "whitespace", - schema: [ - { - oneOf: [ - { - enum: ["always", "never", "consistent"] - }, - { - type: "object", - properties: { - multiline: { - type: "boolean" + schema: { + definitions: { + basicConfig: { + oneOf: [ + { + enum: ["always", "never", "consistent"] + }, + { + type: "object", + properties: { + multiline: { + type: "boolean" + }, + minItems: { + type: ["integer", "null"], + minimum: 0 + } }, - minItems: { - type: ["integer", "null"], - minimum: 0 - } + additionalProperties: false + } + ] + } + }, + items: [ + { + oneOf: [ + { + $ref: "#/definitions/basicConfig" }, - additionalProperties: false - } - ] - } - ], + { + type: "object", + properties: { + ArrayExpression: { + $ref: "#/definitions/basicConfig" + }, + ArrayPattern: { + $ref: "#/definitions/basicConfig" + } + }, + additionalProperties: false, + minProperties: 1 + } + ] + } + ] + }, messages: { unexpectedLineBreak: "There should be no linebreak here.", @@ -93,6 +117,20 @@ module.exports = { * @returns {{ArrayExpression: {multiline: boolean, minItems: number}, ArrayPattern: {multiline: boolean, minItems: number}}} Normalized option object. */ function normalizeOptions(options) { + if (options && (options.ArrayExpression || options.ArrayPattern)) { + let expressionOptions, patternOptions; + + if (options.ArrayExpression) { + expressionOptions = normalizeOptionValue(options.ArrayExpression); + } + + if (options.ArrayPattern) { + patternOptions = normalizeOptionValue(options.ArrayPattern); + } + + return { ArrayExpression: expressionOptions, ArrayPattern: patternOptions }; + } + const value = normalizeOptionValue(options); return { ArrayExpression: value, ArrayPattern: value }; @@ -177,6 +215,10 @@ module.exports = { const normalizedOptions = normalizeOptions(context.options[0]); const options = normalizedOptions[node.type]; + if (!options) { + return; + } + let elementBreak = false; /* diff --git a/tests/lib/rules/array-element-newline.js b/tests/lib/rules/array-element-newline.js index 691e6afb604..b17af83763d 100644 --- a/tests/lib/rules/array-element-newline.js +++ b/tests/lib/rules/array-element-newline.js @@ -135,9 +135,13 @@ ruleTester.run("array-element-newline", rule, { { code: "var [] = foo;", options: [{ minItems: 3 }], parserOptions: { ecmaVersion: 6 } }, { code: "var [a] = foo;", options: [{ minItems: 3 }], parserOptions: { ecmaVersion: 6 } }, { code: "var [a, b] = foo;", options: [{ minItems: 3 }], parserOptions: { ecmaVersion: 6 } }, - { code: "var [a,\nb,\nc] = foo;", options: [{ minItems: 3 }], parserOptions: { ecmaVersion: 6 } } + { code: "var [a,\nb,\nc] = foo;", options: [{ minItems: 3 }], parserOptions: { ecmaVersion: 6 } }, - ], + /* + * ArrayExpression & ArrayPattern + * { ArrayExpression: "always", ArrayPattern: "never" } + */ + { code: "var [a, b] = [1,\n2]", options: [{ ArrayExpression: "always", ArrayPattern: "never" }], parserOptions: { ecmaVersion: 6 } }], invalid: [ @@ -826,6 +830,55 @@ ruleTester.run("array-element-newline", rule, { column: 11 } ] + }, + + /* + * ArrayExpression & ArrayPattern + * { ArrayExpression: "always", ArrayPattern: "never" } + */ + { + code: "var [a,\nb] = [1, 2]", + output: "var [a, b] = [1,\n2]", + options: [{ ArrayExpression: "always", ArrayPattern: "never" }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedLineBreak", + line: 1, + column: 8 + }, + { + messageId: "missingLineBreak", + line: 2, + column: 9 + } + ] + }, + { + code: "var [a, b] = [1, 2]", + output: "var [a, b] = [1,\n2]", + options: [{ ArrayExpression: "always", ArrayPattern: "never" }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "missingLineBreak", + line: 1, + column: 17 + } + ] + }, + { + code: "var [a,\nb] = [1,\n2]", + output: "var [a, b] = [1,\n2]", + options: [{ ArrayExpression: "always", ArrayPattern: "never" }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedLineBreak", + line: 1, + column: 8 + } + ] } ]