Skip to content

Commit

Permalink
Chore: reflect review comments (refs eslint#12545)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-mangoe committed Mar 27, 2021
1 parent bcf8026 commit 44be9d3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
21 changes: 19 additions & 2 deletions docs/rules/no-multi-assign.md
Expand Up @@ -47,15 +47,32 @@ let b = c;

This rule has an object option:

* `"ignoreNonDeclaration": false` (default) disallows using multiple assignments to already declared variables.
* `"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; // this is allowed.
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
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/no-multi-assign.js
Expand Up @@ -45,12 +45,11 @@ module.exports = {
const options = context.options[0] || {
ignoreNonDeclaration: false
};
const targetParent = options.ignoreNonDeclaration ? ["VariableDeclarator"] : ["AssignmentExpression", "VariableDeclarator"];

return {
AssignmentExpression(node) {
const target = options.ignoreNonDeclaration ? ["VariableDeclarator"] : ["AssignmentExpression", "VariableDeclarator"];

if (target.indexOf(node.parent.type) !== -1) {
if (targetParent.indexOf(node.parent.type) !== -1) {
context.report({
node,
messageId: "unexpectedChain"
Expand Down
30 changes: 27 additions & 3 deletions tests/lib/rules/no-multi-assign.js
Expand Up @@ -39,7 +39,6 @@ function errorAt(line, column, type) {
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();
const options = [{ ignoreNonDeclaration: true }];

ruleTester.run("no-mutli-assign", rule, {
valid: [
Expand All @@ -53,7 +52,8 @@ ruleTester.run("no-mutli-assign", rule, {
{ 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: "const x = {};const y = {};x.one = y.one = 1;", options, parserOptions: { ecmaVersion: 6 } }
{ 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 @@ -142,12 +142,36 @@ ruleTester.run("no-mutli-assign", rule, {
},
{
code: "const x = {};\nconst y = x.one = 1;",
options,
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 44be9d3

Please sign in to comment.