Skip to content

Commit

Permalink
Fix: dot-notation autofix produces syntax errors for object called "l…
Browse files Browse the repository at this point in the history
…et" (#8807)

The `dot-notation` autofixer previously fixed code like `let.if` to `let["if"]`. However, this is a syntax error because a statement beginning with `let[` is parsed as the start of a variable declaration, not a MemberExpression.
  • Loading branch information
not-an-aardvark authored and gyandeeps committed Jun 27, 2017
1 parent a53ef7e commit 616587f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/rules/dot-notation.js
Expand Up @@ -116,6 +116,15 @@ module.exports = {
return null;
}

if (node.object.type === "Identifier" && node.object.name === "let") {

/*
* A statement that starts with `let[` is parsed as a destructuring variable declaration, not
* a MemberExpression.
*/
return null;
}

return fixer.replaceTextRange(
[dot.range[0], node.property.range[1]],
`[${textAfterDot}"${node.property.name}"]`
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/dot-notation.js
Expand Up @@ -178,6 +178,12 @@ ruleTester.run("dot-notation", rule, {
code: "foo['bar']instanceof baz",
output: "foo.bar instanceof baz",
errors: [{ message: "[\"bar\"] is better written in dot notation." }]
},
{
code: "let.if()",
output: null, // `let["if"]()` is a syntax error because `let[` indicates a destructuring variable declaration
options: [{ allowKeywords: false }],
errors: [{ message: ".if is a syntax error." }]
}
]
});

0 comments on commit 616587f

Please sign in to comment.