Skip to content

Commit

Permalink
Fix: prefer-const autofix multiline assignment (fixes eslint#10582)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstern6 committed Oct 19, 2018
1 parent 2f87bb3 commit e16985a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
8 changes: 0 additions & 8 deletions lib/rules/prefer-const.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,6 @@ module.exports = {
if (nodes.length && (shouldMatchAnyDestructuredVariable || nodesToReport.length === nodes.length)) {
const varDeclParent = findUp(nodes[0], "VariableDeclaration", parentNode => parentNode.type.endsWith("Statement"));
const shouldFix = varDeclParent &&

/*
* If there are multiple variable declarations, like {let a = 1, b = 2}, then
* do not attempt to fix if one of the declarations should be `const`. It's
* too hard to know how the developer would want to automatically resolve the issue.
*/
varDeclParent.declarations.length === 1 &&

// Don't do a fix unless the variable is initialized (or it's in a for-in or for-of loop)
(varDeclParent.parent.type === "ForInStatement" || varDeclParent.parent.type === "ForOfStatement" || varDeclParent.declarations[0].init) &&

Expand Down
4 changes: 2 additions & 2 deletions tests/lib/rules/prefer-const.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ ruleTester.run("prefer-const", rule, {
},
{
code: "let {a = 0, b} = obj, c = a; b = a;",
output: null,
output: "const {a = 0, b} = obj, c = a; b = a;",
options: [{ destructuring: "any" }],
errors: [
{ message: "'a' is never reassigned. Use 'const' instead.", type: "Identifier" },
Expand All @@ -366,7 +366,7 @@ ruleTester.run("prefer-const", rule, {
},
{
code: "let {a = 0, b} = obj, c = a; b = a;",
output: null,
output: "const {a = 0, b} = obj, c = a; b = a;" ,
options: [{ destructuring: "all" }],
errors: [{ message: "'c' is never reassigned. Use 'const' instead.", type: "Identifier" }]
},
Expand Down

0 comments on commit e16985a

Please sign in to comment.