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 1 commit
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
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"];
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

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

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

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")
]

mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}
]
});