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 ignoreNonDeclaration to no-multi-assign rule (fixes #12545) #14185

Merged
merged 4 commits into from Apr 3, 2021
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
32 changes: 32 additions & 0 deletions docs/rules/no-multi-assign.md
Expand Up @@ -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:
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

```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;
```
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

## Related Rules

* [max-statements-per-line](max-statements-per-line.md)
17 changes: 15 additions & 2 deletions lib/rules/no-multi-assign.js
Expand Up @@ -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."
Expand All @@ -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"
Expand Down
37 changes: 36 additions & 1 deletion tests/lib/rules/no-multi-assign.js
Expand Up @@ -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: [
Expand Down Expand Up @@ -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")
]

mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
},
{
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")
]
}
]
});