Refactor minify-numeric-literals.#349
Refactor minify-numeric-literals.#349vigneshshanmugam merged 1 commit intobabel:masterfrom bardiharborow:refactor-numeric-literals
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.
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:
0xashould transform to10, instead of0xa0x64should transform to100instead of1e2(opinionated, better gzip?)0o12should transform to10instead of1e10o144should transform to100instead of1e2(opinionated, better gzip?)0b1010should transform to10instead of1e10b1100100should transform to100instead of1e2(opinionated, better gzip?)0.123456should transform to.123456instead of0.1234561.5e3should transform to1500instead of1.5e31e-1should transform to.1instead of1e-11e-2should transform to.01instead of1e-21e-3should transform to.001instead of1e-3(opinionated, better gzip?)1.5e-3should transform to.0015instead of1.5e-3I 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.replaceWithin favor of editingpath.node.extra.rawin-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.