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

fix: support bigInt in numeric-separator transform #12240

Merged
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
21 changes: 15 additions & 6 deletions 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<BigIntLiteral | NumericLiteral>} { node }: A Babel AST node path
*/
function remover({ node }: NodePath<BigIntLiteral | NumericLiteral>) {
const { extra } = node;
if (extra && extra.raw.includes("_")) {
extra.raw = extra.raw.replace(/_/g, "");
}
}

export default declare(api => {
api.assertVersion(7);

Expand All @@ -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,
},
};
});
@@ -0,0 +1 @@
expect(9_007_199_254_740_993n).toBe(9007199254740993n);
@@ -0,0 +1,4 @@
{
"plugins": ["proposal-numeric-separator"],
"minNodeVersion": "10.4.0"
}
@@ -0,0 +1 @@
9_007_199_254_740_993n;
@@ -0,0 +1 @@
9007199254740993n;