Fix numeric-separator transform (#5968)
This commit is contained in:
committed by
Henry Zhu
parent
579499c66d
commit
c6edce115c
@@ -1,33 +1,29 @@
|
||||
import syntaxNumericSeparator from "babel-plugin-syntax-numeric-separator";
|
||||
|
||||
export default function({ types: t }) {
|
||||
function replacer(value) {
|
||||
return value.replace(/_/g, "");
|
||||
}
|
||||
|
||||
function replaceNumberArg({ node }) {
|
||||
if (node.callee.name !== "Number") {
|
||||
return;
|
||||
}
|
||||
|
||||
const arg = node.arguments[0];
|
||||
if (!t.isStringLiteral(arg)) {
|
||||
return;
|
||||
}
|
||||
arg.value = replacer(arg.value);
|
||||
}
|
||||
|
||||
const CallExpression = replaceNumberArg;
|
||||
const NewExpression = replaceNumberArg;
|
||||
arg.value = arg.value.replace(/_/g, "");
|
||||
}
|
||||
|
||||
return {
|
||||
inherits: syntaxNumericSeparator,
|
||||
|
||||
visitor: {
|
||||
CallExpression,
|
||||
NewExpression,
|
||||
CallExpression: replaceNumberArg,
|
||||
NewExpression: replaceNumberArg,
|
||||
NumericLiteral({ node }) {
|
||||
if (node.extra && /_/.test(node.extra.raw)) {
|
||||
node.value = replacer(node.extra.raw);
|
||||
const { extra } = node;
|
||||
if (extra && /_/.test(extra.raw)) {
|
||||
extra.raw = extra.raw.replace(/_/g, "");
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
@@ -38,7 +38,7 @@ suite("validators", function() {
|
||||
t.objectProperty(
|
||||
t.identifier("a"),
|
||||
t.objectPattern([
|
||||
t.objectProperty(t.identifier("b"), t.stringLiteral("foo")),
|
||||
t.objectProperty(t.identifier("b"), t.identifier("foo")),
|
||||
t.objectProperty(
|
||||
t.identifier("c"),
|
||||
t.arrayPattern([t.identifier("value")]),
|
||||
|
||||
Reference in New Issue
Block a user