Skip to content

Commit

Permalink
Update: add an option to ignore non declaration (refs eslint#12545)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-mangoe committed Mar 6, 2021
1 parent 27a67d7 commit 597e136
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 16 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,15 @@ module.exports = {
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
const options = context.options[0] || {
ignoreNonDeclaration: false
};

return {
AssignmentExpression(node) {
if (["AssignmentExpression", "VariableDeclarator"].indexOf(node.parent.type) !== -1) {
const target = options.ignoreNonDeclaration ? ["VariableDeclarator"] : ["AssignmentExpression", "VariableDeclarator"];

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

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

ruleTester.run("no-mutli-assign", rule, {
valid: [
Expand All @@ -51,7 +52,8 @@ 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, parserOptions: { ecmaVersion: 6 } }
],

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

}
]
});

0 comments on commit 597e136

Please sign in to comment.