Skip to content
Permalink
Browse files
Update: add ignoreNonDeclaration to no-multi-assign rule (fixes #12545)…
… (#14185)

* Update: add an option to ignore non declaration (refs #12545)

* Doc: add description of the option (refs #12545)

* Chore: reflect review comments (refs #12545)

* Chore: modify the document according to comments (refs #12545)
  • Loading branch information
t-mangoe committed Apr 3, 2021
1 parent c981fb1 commit b51d0778d76c2aa27578caca3ea82c867dced3e4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
@@ -43,6 +43,38 @@ let a = c;
let b = c;
```

## Options

This rule has an object option:

* `"ignoreNonDeclaration"`: When set to `true`, the rule allows chains that don't include initializing a variable in a declaration. Default is `false`.

### ignoreNonDeclaration

Examples of **correct** code for the `{ "ignoreNonDeclaration": true }` option:

```js
/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/
let a;
let b;
a = b = "baz";
const x = {};
const y = {};
x.one = y.one = 1;
```

Examples of **incorrect** code for the `{ "ignoreNonDeclaration": true }` option:

```js
/*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/
let a = b = "baz";
const foo = bar = 1;
```

## Related Rules

* [max-statements-per-line](max-statements-per-line.md)
@@ -21,7 +21,16 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-multi-assign"
},

schema: [],
schema: [{
type: "object",
properties: {
ignoreNonDeclaration: {
type: "boolean",
default: false
}
},
additionalProperties: false
}],

messages: {
unexpectedChain: "Unexpected chained assignment."
@@ -33,10 +42,14 @@ module.exports = {
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
const options = context.options[0] || {
ignoreNonDeclaration: false
};
const targetParent = options.ignoreNonDeclaration ? ["VariableDeclarator"] : ["AssignmentExpression", "VariableDeclarator"];

return {
AssignmentExpression(node) {
if (["AssignmentExpression", "VariableDeclarator"].indexOf(node.parent.type) !== -1) {
if (targetParent.indexOf(node.parent.type) !== -1) {
context.report({
node,
messageId: "unexpectedChain"
@@ -51,7 +51,9 @@ ruleTester.run("no-mutli-assign", rule, {
{ code: "for(let a = 0, b = 0;;){}", parserOptions: { ecmaVersion: 6 } },
{ code: "for(const a = 0, b = 0;;){}", parserOptions: { ecmaVersion: 6 } },
{ code: "export let a, b;", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export let a,\n b = 0;", parserOptions: { ecmaVersion: 6, sourceType: "module" } }
{ code: "export let a,\n b = 0;", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "const x = {};const y = {};x.one = y.one = 1;", options: [{ ignoreNonDeclaration: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "let a, b;a = b = 1", options: [{ ignoreNonDeclaration: true }], parserOptions: { ecmaVersion: 6 } }
],

invalid: [
@@ -137,6 +139,39 @@ ruleTester.run("no-mutli-assign", rule, {
errors: [
errorAt(1, 5, "AssignmentExpression")
]
},
{
code: "const x = {};\nconst y = x.one = 1;",
options: [{ ignoreNonDeclaration: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
errorAt(2, 11, "AssignmentExpression")
]

},
{
code: "let a, b;a = b = 1",
options: [{}],
parserOptions: { ecmaVersion: 6 },
errors: [
errorAt(1, 14, "AssignmentExpression")
]
},
{
code: "let x, y;x = y = 'baz'",
options: [{ ignoreNonDeclaration: false }],
parserOptions: { ecmaVersion: 6 },
errors: [
errorAt(1, 14, "AssignmentExpression")
]
},
{
code: "const a = b = 1",
options: [{ ignoreNonDeclaration: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
errorAt(1, 11, "AssignmentExpression")
]
}
]
});

0 comments on commit b51d077

Please sign in to comment.