Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Separate pattern/expression options for array-element-newline #11796

Merged
merged 2 commits into from Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/rules/array-element-newline.md
Expand Up @@ -19,6 +19,20 @@ Or an object option (Requires line breaks if any of properties is satisfied. Oth
* `"multiline": <boolean>` requires line breaks if there are line breaks inside elements. If this is false, this condition is disabled.
* `"minItems": <number>` 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 (if unspecified, this rule will not apply to array patterns)
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved

### always

Examples of **incorrect** code for this rule with the default `"always"` option:
Expand Down
82 changes: 62 additions & 20 deletions lib/rules/array-element-newline.js
Expand Up @@ -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.",
Expand Down Expand Up @@ -95,6 +119,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 };
Expand Down Expand Up @@ -180,6 +218,10 @@ module.exports = {
const normalizedOptions = normalizeOptions(context.options[0]);
const options = normalizedOptions[node.type];

if (!options) {
return;
}

let elementBreak = false;

/*
Expand Down
57 changes: 55 additions & 2 deletions tests/lib/rules/array-element-newline.js
Expand Up @@ -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: [

Expand Down Expand Up @@ -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
}
]
}
]

Expand Down