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 16, 2018
1 parent 577cbf1 commit 20cbea1
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
Expand Up @@ -319,14 +319,6 @@ module.exports = {
if (nodes.length && (checkingMixedDestructuring || 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
Expand Up @@ -309,7 +309,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 @@ -318,7 +318,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 20cbea1

Please sign in to comment.