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 fixer for no-floating-decimal (fixes #7070) #7081

Merged
merged 1 commit into from Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions docs/rules/no-floating-decimal.md
@@ -1,5 +1,7 @@
# Disallow Floating Decimals (no-floating-decimal)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.

Float values in JavaScript contain a decimal point, and there is no requirement that the decimal point be preceded or followed by a number. For example, the following are all valid JavaScript numbers:

```js
Expand Down
16 changes: 13 additions & 3 deletions lib/rules/no-floating-decimal.js
Expand Up @@ -17,7 +17,9 @@ module.exports = {
recommended: false
},

schema: []
schema: [],

fixable: "code"
},

create(context) {
Expand All @@ -27,10 +29,18 @@ module.exports = {

if (typeof node.value === "number") {
if (node.raw.indexOf(".") === 0) {
context.report(node, "A leading decimal point can be confused with a dot.");
context.report({
node,
message: "A leading decimal point can be confused with a dot.",
fix: fixer => fixer.insertTextBefore(node, "0")
});
}
if (node.raw.indexOf(".") === node.raw.length - 1) {
context.report(node, "A trailing decimal point can be confused with a dot.");
context.report({
node,
message: "A trailing decimal point can be confused with a dot.",
fix: fixer => fixer.insertTextAfter(node, "0")
Copy link
Member

@mysticatea mysticatea Sep 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Though it's not related to this PR, this rule seems to miss 2.e+3-like notation originally.
Is it a bug?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it is-- the rule is specifically about decimal points being confused for dot operators, I think, and so a decimal point in the middle of a numeric literal (even if it is on the trailing each of the mantissa) should be okay.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it, thank you for the explanation!

LGTM.

});
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions tests/lib/rules/no-floating-decimal.js
Expand Up @@ -24,8 +24,25 @@ ruleTester.run("no-floating-decimal", rule, {
"var x = \"2.5\";"
],
invalid: [
{ code: "var x = .5;", errors: [{ message: "A leading decimal point can be confused with a dot.", type: "Literal"}] },
{ code: "var x = -.5;", errors: [{ message: "A leading decimal point can be confused with a dot.", type: "Literal"}] },
{ code: "var x = 2.;", errors: [{ message: "A trailing decimal point can be confused with a dot.", type: "Literal"}] }
{
code: "var x = .5;",
output: "var x = 0.5;",
errors: [{ message: "A leading decimal point can be confused with a dot.", type: "Literal" }]
},
{
code: "var x = -.5;",
output: "var x = -0.5;",
errors: [{ message: "A leading decimal point can be confused with a dot.", type: "Literal" }]
},
{
code: "var x = 2.;",
output: "var x = 2.0;",
errors: [{ message: "A trailing decimal point can be confused with a dot.", type: "Literal" }]
},
{
code: "var x = -2.;",
output: "var x = -2.0;",
errors: [{ message: "A trailing decimal point can be confused with a dot.", type: "Literal" }]
}
]
});