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: Add "consistent" option to array-bracket-newline (#9136) #9206

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 44 additions & 0 deletions docs/rules/array-bracket-newline.md
Expand Up @@ -12,6 +12,7 @@ This rule has either a string option:

* `"always"` requires line breaks inside brackets
* `"never"` disallows line breaks inside brackets
* `"consistent"` requires consistent usage of linebreaks for each pair of brackets. It reports an error if one bracket in the pair has a linebreak inside it and the other bracket does not.

Or an object option (Requires line breaks if any of properties is satisfied. Otherwise, disallows line breaks):

Expand Down Expand Up @@ -100,6 +101,49 @@ var e = [function foo() {
}];
```

### consistent

Examples of **incorrect** code for this rule with the `"consistent"` option:

```js
/*eslint array-bracket-newline: ["error", "consistent"]*/

var a = [1
];
var b = [
1];
var c = [function foo() {
dosomething();
}
]
var d = [
function foo() {
dosomething();
}]
```

Examples of **correct** code for this rule with the `"consistent"` option:

```js
/*eslint array-bracket-newline: ["error", "consistent"]*/

var a = [];
var b = [
];
var c = [1];
var d = [
1
];
var e = [function foo() {
dosomething();
}];
var f = [
function foo() {
dosomething();
}
];
```

### multiline

Examples of **incorrect** code for this rule with the default `{ "multiline": true }` option:
Expand Down
18 changes: 13 additions & 5 deletions lib/rules/array-bracket-newline.js
Expand Up @@ -23,7 +23,7 @@ module.exports = {
{
oneOf: [
{
enum: ["always", "never"]
enum: ["always", "never", "consistent"]
},
{
type: "object",
Expand Down Expand Up @@ -58,11 +58,15 @@ module.exports = {
* @returns {{multiline: boolean, minItems: number}} Normalized option object.
*/
function normalizeOptionValue(option) {
let consistent = false;
let multiline = false;
let minItems = 0;

if (option) {
if (option === "always" || option.minItems === 0) {
if (option === "consistent") {
consistent = true;
minItems = Number.POSITIVE_INFINITY;
} else if (option === "always" || option.minItems === 0) {
minItems = 0;
} else if (option === "never") {
minItems = Number.POSITIVE_INFINITY;
Expand All @@ -71,11 +75,12 @@ module.exports = {
minItems = option.minItems || Number.POSITIVE_INFINITY;
}
} else {
consistent = false;
multiline = true;
minItems = Number.POSITIVE_INFINITY;
}

return { multiline, minItems };
return { consistent, multiline, minItems };
}

/**
Expand Down Expand Up @@ -173,8 +178,7 @@ module.exports = {
/**
* Reports a given node if it violated this rule.
*
* @param {ASTNode} node - A node to check. This is an ObjectExpression node or an ObjectPattern node.
* @param {{multiline: boolean, minItems: number}} options - An option object.
* @param {ASTNode} node - A node to check. This is an ArrayExpression node or an ArrayPattern node.
* @returns {void}
*/
function check(node) {
Expand All @@ -200,6 +204,10 @@ module.exports = {
firstIncComment.type === "Block" &&
firstIncComment.loc.start.line !== lastIncComment.loc.end.line &&
firstIncComment === lastIncComment
) ||
(
options.consistent &&
firstIncComment.loc.start.line !== openBracket.loc.end.line
)
);

Expand Down
78 changes: 78 additions & 0 deletions tests/lib/rules/array-bracket-newline.js
Expand Up @@ -63,6 +63,12 @@ ruleTester.run("array-bracket-newline", rule, {
{ code: "var foo = [1,\n/* any comment */\n2];", options: ["never"] },
{ code: "var foo = [function foo() {\ndosomething();\n}];", options: ["never"] },

// "consistent"
{ code: "var a = []", options: ["consistent"] },
{ code: "var a = [\n]", options: ["consistent"] },
{ code: "var a = [1]", options: ["consistent"] },
{ code: "var a = [\n1\n]", options: ["consistent"] },

// { multiline: true }
{ code: "var foo = [];", options: [{ multiline: true }] },
{ code: "var foo = [1];", options: [{ multiline: true }] },
Expand Down Expand Up @@ -146,6 +152,12 @@ ruleTester.run("array-bracket-newline", rule, {
{ code: "var [\na, b /* any comment */\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } },
{ code: "var [\na,\nb\n] = foo;", options: ["always"], parserOptions: { ecmaVersion: 6 } },

// "consistent"
{ code: "var [] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } },
{ code: "var [\n] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } },
{ code: "var [a] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } },
{ code: "var [\na\n] = foo", options: ["consistent"], parserOptions: { ecmaVersion: 6 } },

// { multiline: true }
{ code: "var [] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "var [a] = foo;", options: [{ multiline: true }], parserOptions: { ecmaVersion: 6 } },
Expand Down Expand Up @@ -485,6 +497,38 @@ ruleTester.run("array-bracket-newline", rule, {
]
},

// "consistent"
{
code: "var foo = [\n1]",
output: "var foo = [\n1\n]",
options: ["consistent"],
errors: [
{
message: ERR_BREAK_BEFORE,
type: "ArrayExpression",
line: 2,
column: 2,
endLine: 2,
endColumn: 3
}
]
},
{
code: "var foo = [1\n]",
output: "var foo = [1]",
options: ["consistent"],
errors: [
{
message: ERR_NO_BREAK_BEFORE,
type: "ArrayExpression",
line: 2,
column: 1,
endLine: 2,
endColumn: 2
}
]
},

// { multiline: true }
{
code: "var foo = [\n];",
Expand Down Expand Up @@ -1377,6 +1421,40 @@ ruleTester.run("array-bracket-newline", rule, {
]
},

// "consistent"
{
code: "var [\na] = foo",
output: "var [\na\n] = foo",
options: ["consistent"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: ERR_BREAK_BEFORE,
type: "ArrayPattern",
line: 2,
column: 2,
endLine: 2,
endColumn: 3
}
]
},
{
code: "var [a\n] = foo",
output: "var [a] = foo",
options: ["consistent"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: ERR_NO_BREAK_BEFORE,
type: "ArrayPattern",
line: 2,
column: 1,
endLine: 2,
endColumn: 2
}
]
},

// { minItems: 2 }
{
code: "var [\n] = foo;",
Expand Down