Skip to content

Commit

Permalink
Update: Separate pattern/expression options for array-element-newline (
Browse files Browse the repository at this point in the history
…#11796)

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

* Docs: Add pattern vs expression examples for array-element-newline
  • Loading branch information
jacobparish authored and kaicataldo committed Jan 17, 2020
1 parent f8f115a commit e59d775
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 22 deletions.
71 changes: 71 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 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:
Expand Down Expand Up @@ -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

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 @@ -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 };
Expand Down Expand Up @@ -177,6 +215,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

0 comments on commit e59d775

Please sign in to comment.