-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Refactor minify-numeric-literals. #349
Refactor minify-numeric-literals. #349
Conversation
Don't the last 5 cases already work this way? I can see it in a REPL. As far as leading 0, isn't this something that's controlled by Babel printer? |
@kangax you are correct, but it's not minify-numeric-literals that's doing that, it's constant-folding. If I run my refactored tests without the associated plugin fixes, I get this:
As for the leading zero, I'm unfamiliar with how the Babel printer works, but it seems to just echo what ever we give it in |
I can't reproduce this (except for
|
Ah. The |
Can you rebase your branch? Looks like the CI didn't run. |
.toExponential() | ||
.replace(/\+/g, "") | ||
.replace(/e0/, ""); | ||
const normal = path.node.value.toString().replace(/^0\./, "."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure why I have used parseFloat instead of this, remember there was a missed optimisation.
@bardiharborow Thanks a lot :) |
@vigneshshanmugam Thanks for your extra optimisations too. :) |
I went ahead and rewrote the test suite for minify-numeric-literals, and came across a number of issues:
0xa
should transform to10
, instead of0xa
0x64
should transform to100
instead of1e2
(opinionated, better gzip?)0o12
should transform to10
instead of1e1
0o144
should transform to100
instead of1e2
(opinionated, better gzip?)0b1010
should transform to10
instead of1e1
0b1100100
should transform to100
instead of1e2
(opinionated, better gzip?)0.123456
should transform to.123456
instead of0.123456
1.5e3
should transform to1500
instead of1.5e3
1e-1
should transform to.1
instead of1e-1
1e-2
should transform to.01
instead of1e-2
1e-3
should transform to.001
instead of1e-3
(opinionated, better gzip?)1.5e-3
should transform to.0015
instead of1.5e-3
I investigated several different ways of writing logic to solve this, but the simplest way I can find is to compare the length of
.toString()
and.toExponential()
and to choose the shortest. Additionally I've removed the.replaceWith
in favor of editingpath.node.extra.raw
in-place. This prevents the NumericLiteral from being re-entered immediately, but I'm unsure if this is good practice or not.This PR has now been rewritten a little and incorporates the optimisations from #467.
Closes #467 and fixes #459.