Skip to content

Commit

Permalink
Fix: dot-notation autofix produces invalid syntax for integer propert…
Browse files Browse the repository at this point in the history
…ies (#8332)

Previously, when fixing a computed property access from a number literal, the dot-notation autofixer would place a dot immediately after the number literal. However, a dot after a decimal integer literal gets parsed as a decimal point, not a property accessor. This commit updates the fixer to insert a space before the dot when the object is a decimal integer literal.
  • Loading branch information
not-an-aardvark committed Mar 28, 2017
1 parent a9d1bea commit 927de90
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/rules/dot-notation.js
Expand Up @@ -79,9 +79,11 @@ module.exports = {
return null;
}

const textBeforeDot = astUtils.isDecimalInteger(node.object) ? " " : "";

return fixer.replaceTextRange(
[leftBracket.range[0], rightBracket.range[1]],
`.${node.property.value}`
`${textBeforeDot}.${node.property.value}`
);
}
});
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/rules/dot-notation.js
Expand Up @@ -168,6 +168,11 @@ ruleTester.run("dot-notation", rule, {
code: "(foo)['bar']",
output: "(foo).bar",
errors: [{ message: "[\"bar\"] is better written in dot notation." }]
},
{
code: "1['toString']",
output: "1 .toString",
errors: [{ message: "[\"toString\"] is better written in dot notation." }]
}
]
});

0 comments on commit 927de90

Please sign in to comment.