From b438563221eb28d3c74268640f5d88043f01d053 Mon Sep 17 00:00:00 2001 From: Bardi Harborow Date: Sun, 18 Dec 2016 17:30:09 +1100 Subject: [PATCH] Refactor minify-numeric-literals. --- .../__tests__/numeric-literals-test.js | 52 ++++++++++++++++--- .../src/index.js | 36 ++++++++----- 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js b/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js index 5b931371c..383194fcd 100644 --- a/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js +++ b/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js @@ -6,23 +6,59 @@ const thePlugin = require("../../../utils/test-transform")( describe("numeric-literals", () => { thePlugin( - "should shorten numeric literals properly", + "should shorten integer literals correctly", ` - [10, 100, 1000, 10000, -2, -30000]; - [1e3, -1e4, 1e-5, 1.5e12, 1.23456, .1]; - [0x000001, 0o23420, 0b10011100010000]; + [1, 10, 100, 1000, 10000, -1, -10, -100, -1000, -10000]; + [0x1, 0xa, 0x64, 0x3e8, 0xfffffff, -0x1, -0xa, -0x64, -0x3e8, -0xfffffff]; + [0o1, 0o12, 0o144, 0o1750, -0o1, -0o12, -0o144, -0o1750]; + [0b1, 0b1010, 0b1100100, 0b1111101000, -0b1, -0b1010, -0b1100100, -0b1111101000] `, ` - [10, 100, 1e3, 1e4, -2, -3e4]; - [1e3, -1e4, 1e-5, 1.5e12, 1.23456, .1]; - [1, 1e4, 1e4]; + [1, 10, 100, 1e3, 1e4, -1, -10, -100, -1e3, -1e4]; + [1, 10, 100, 1e3, 0xfffffff, -1, -10, -100, -1e3, -0xfffffff]; + [1, 10, 100, 1e3, -1, -10, -100, -1e3]; + [1, 10, 100, 1e3, -1, -10, -100, -1e3]; + ` + ); + + thePlugin( + "should shorten floats correctly", + ` + [.123456, 1.23456, 12.3456, -0.123456, -1.23456, -12.3456]; + [0.10, 0.010, 0.0010]; + `, + ` + [.123456, 1.23456, 12.3456, -.123456, -1.23456, -12.3456]; + [.1, .01, .001]; + ` + ); + + thePlugin( + "should shorten existing exponential literals correctly", + ` + [1e1, 1e2, 1.5e3, -1e1, -1e2, -1.5e3, 1e-1, 1e-2, 1.5e-3, 1e-4]; + [1.5e4, 15e-2, 1.5e-4]; + `, + ` + [10, 1e2, 1500, -10, -1e2, -1500, .1, .01, .0015, 1e-4]; + [15e3, .15, 15e-5]; + ` + ); + + thePlugin( + "should represent literals in exponential form when optimal", + ` + [123000, 12345600000]; + `, + ` + [123e3, 123456e5]; ` ); // TODO: this seems to be specific to how Babel output numbers // for some reason it adds + in the beginning thePlugin.skip( - "should have correct signs", + "should handle extreme float resolution correctly", ` [+0.000000000001, -0.00000000001]; `, diff --git a/packages/babel-plugin-minify-numeric-literals/src/index.js b/packages/babel-plugin-minify-numeric-literals/src/index.js index 1fa9ec9f8..f244700d0 100644 --- a/packages/babel-plugin-minify-numeric-literals/src/index.js +++ b/packages/babel-plugin-minify-numeric-literals/src/index.js @@ -1,24 +1,36 @@ "use strict"; -module.exports = function({ types: t }) { +module.exports = function() { return { name: "minify-numeric-literals", visitor: { NumericLiteral(path) { if (!path.node.extra) return; - const exponential = path.node.value - .toExponential() - .replace(/\+/g, "") - .replace(/e0/, ""); + const normal = path.node.value.toString().replace(/^0\./, "."); + let exponential = path.node.value.toExponential().replace(/\+/g, ""); - if (path.node.extra.raw.length > exponential.length) { - const literal = t.numericLiteral(path.node.value); - literal.extra = { - raw: exponential, - rawValue: path.node.value - }; - path.replaceWith(literal); + if (exponential.indexOf(".") >= 0 && exponential.indexOf("e") >= 0) { + const lastChar = exponential.substr(exponential.lastIndexOf("e") + 1); + const dotIndex = exponential.lastIndexOf(".") + 1; + const subLength = exponential.substr( + dotIndex, + exponential.lastIndexOf("e") - dotIndex + ).length; + exponential = (exponential.substr( + 0, + exponential.lastIndexOf("e") + 1 + ) + + (lastChar - subLength)) + .replace(".", "") + .replace(/e0/, ""); + } + + const replacement = + normal.length > exponential.length ? exponential : normal; + + if (path.node.extra.raw.length > replacement.length) { + path.node.extra.raw = replacement; } } }