Skip to content

Commit

Permalink
New: add consistent option to rule (fixes eslint#9136)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom committed Oct 5, 2017
1 parent 434d9e2 commit 0f2026e
Show file tree
Hide file tree
Showing 3 changed files with 466 additions and 4 deletions.
9 changes: 9 additions & 0 deletions docs/rules/array-bracket-newline.md
Expand Up @@ -17,6 +17,7 @@ Or an object option (Requires line breaks if any of properties is satisfied. Oth

* `"multiline": true` (default) requires line breaks if there are line breaks inside elements or between elements. If this is false, this condition is disabled.
* `"minItems": null` (default) 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.
* `"consistent": true` requires that either both square brackets, or neither, directly enclose newlines.

### always

Expand Down Expand Up @@ -223,6 +224,14 @@ var e = [
];
```

### consistent

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

```js

```


## When Not To Use It

Expand Down
21 changes: 18 additions & 3 deletions lib/rules/array-bracket-newline.js
Expand Up @@ -34,6 +34,9 @@ module.exports = {
minItems: {
type: ["integer", "null"],
minimum: 0
},
consistent: {
type: "boolean"
}
},
additionalProperties: false
Expand All @@ -60,6 +63,7 @@ module.exports = {
function normalizeOptionValue(option) {
let multiline = false;
let minItems = 0;
let consistent = false;

if (option) {
if (option === "always" || option.minItems === 0) {
Expand All @@ -69,13 +73,14 @@ module.exports = {
} else {
multiline = Boolean(option.multiline);
minItems = option.minItems || Number.POSITIVE_INFINITY;
consistent = Boolean(option.consistent);
}
} else {
multiline = true;
minItems = Number.POSITIVE_INFINITY;
}

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

/**
Expand Down Expand Up @@ -214,10 +219,20 @@ module.exports = {
reportRequiredEndingLinebreak(node, closeBracket);
}
} else {
if (!astUtils.isTokenOnSameLine(openBracket, first)) {
const consistent = options.consistent;
const hasLineBreakBetweenOpenBracketAndfirst = !astUtils.isTokenOnSameLine(openBracket, first);
const hasLineBreakBetweenCloseBracketAndLast = !astUtils.isTokenOnSameLine(last, closeBracket);

if (
(!consistent && !astUtils.isTokenOnSameLine(openBracket, first)) ||
(consistent && hasLineBreakBetweenOpenBracketAndfirst && !hasLineBreakBetweenCloseBracketAndLast)
) {
reportNoBeginningLinebreak(node, openBracket);
}
if (!astUtils.isTokenOnSameLine(last, closeBracket)) {
if (
(!consistent && !astUtils.isTokenOnSameLine(last, closeBracket)) ||
(consistent && !hasLineBreakBetweenOpenBracketAndfirst && hasLineBreakBetweenCloseBracketAndLast)
) {
reportNoEndingLinebreak(node, closeBracket);
}
}
Expand Down

0 comments on commit 0f2026e

Please sign in to comment.