feat: support bigInt in numeric-separator transform (#12240)
This commit is contained in:
parent
d6816f0e6c
commit
8b579a27dc
@ -1,6 +1,19 @@
|
|||||||
import { declare } from "@babel/helper-plugin-utils";
|
import { declare } from "@babel/helper-plugin-utils";
|
||||||
import syntaxNumericSeparator from "@babel/plugin-syntax-numeric-separator";
|
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 => {
|
export default declare(api => {
|
||||||
api.assertVersion(7);
|
api.assertVersion(7);
|
||||||
|
|
||||||
@ -9,12 +22,8 @@ export default declare(api => {
|
|||||||
inherits: syntaxNumericSeparator,
|
inherits: syntaxNumericSeparator,
|
||||||
|
|
||||||
visitor: {
|
visitor: {
|
||||||
NumericLiteral({ node }) {
|
NumericLiteral: remover,
|
||||||
const { extra } = node;
|
BigIntLiteral: remover,
|
||||||
if (extra && /_/.test(extra.raw)) {
|
|
||||||
extra.raw = extra.raw.replace(/_/g, "");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
1
packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/exec.js
vendored
Normal file
1
packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint-exec/exec.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
expect(9_007_199_254_740_993n).toBe(9007199254740993n);
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["proposal-numeric-separator"],
|
||||||
|
"minNodeVersion": "10.4.0"
|
||||||
|
}
|
||||||
1
packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/input.js
vendored
Normal file
1
packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
9_007_199_254_740_993n;
|
||||||
1
packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/output.js
vendored
Normal file
1
packages/babel-plugin-proposal-numeric-separator/test/fixtures/removal/bigint/output.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
9007199254740993n;
|
||||||
Loading…
x
Reference in New Issue
Block a user