From 2fec753fc942b0ba01b472838329ca8bb9159e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 22 Oct 2020 15:20:12 -0400 Subject: [PATCH] feat: support bigInt in numeric-separator transform --- .../src/index.js | 21 +++++++++++++------ .../test/fixtures/removal/bigint-exec/exec.js | 1 + .../fixtures/removal/bigint-exec/options.json | 4 ++++ .../test/fixtures/removal/bigint/input.js | 1 + .../test/fixtures/removal/bigint/output.js | 1 + .../fixtures/removal/{ => numeric}/exec.js | 0 6 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/exec.js create mode 100644 packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/options.json create mode 100644 packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/input.js create mode 100644 packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/output.js rename packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/{ => numeric}/exec.js (100%) diff --git a/packages/babel-plugin-proposal-numeric-separator/src/index.js b/packages/babel-plugin-proposal-numeric-separator/src/index.js index 915088dffe4b..9b16a6267511 100644 --- a/packages/babel-plugin-proposal-numeric-separator/src/index.js +++ b/packages/babel-plugin-proposal-numeric-separator/src/index.js @@ -1,6 +1,19 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxNumericSeparator from "@babel/plugin-syntax-numeric-separator"; +/** + * Given a bigIntLiteral or NumericLiteral, remove numeric + * separator `_` from its raw representation + * + * @param {NodePath} { node }: A Babel AST node path + */ +function remover({ node }: NodePath) { + const { extra } = node; + if (extra && extra.raw.includes("_")) { + extra.raw = extra.raw.replace(/_/g, ""); + } +} + export default declare(api => { api.assertVersion(7); @@ -9,12 +22,8 @@ export default declare(api => { inherits: syntaxNumericSeparator, visitor: { - NumericLiteral({ node }) { - const { extra } = node; - if (extra && /_/.test(extra.raw)) { - extra.raw = extra.raw.replace(/_/g, ""); - } - }, + NumericLiteral: remover, + BigIntLiteral: remover, }, }; }); diff --git a/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/exec.js b/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/exec.js new file mode 100644 index 000000000000..829e2856238a --- /dev/null +++ b/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/exec.js @@ -0,0 +1 @@ +expect(9_007_199_254_740_993n).toBe(9007199254740993n); diff --git a/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/options.json b/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/options.json new file mode 100644 index 000000000000..ec80ea0affe6 --- /dev/null +++ b/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["proposal-numeric-separator"], + "minNodeVersion": "10.4.0" +} diff --git a/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/input.js b/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/input.js new file mode 100644 index 000000000000..b56a08a58a9d --- /dev/null +++ b/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/input.js @@ -0,0 +1 @@ +9_007_199_254_740_993n; diff --git a/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/output.js b/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/output.js new file mode 100644 index 000000000000..df6e6fd25d84 --- /dev/null +++ b/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/output.js @@ -0,0 +1 @@ +9007199254740993n; diff --git a/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/exec.js b/packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/numeric/exec.js similarity index 100% rename from packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/exec.js rename to packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/numeric/exec.js