Run prettier
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import definitions from "./definitions";
|
||||
|
||||
export default function ({ types: t }) {
|
||||
export default function({ types: t }) {
|
||||
function getRuntimeModuleName(opts) {
|
||||
return opts.moduleName || "babel-runtime";
|
||||
}
|
||||
@@ -16,21 +16,35 @@ export default function ({ types: t }) {
|
||||
const moduleName = getRuntimeModuleName(this.opts);
|
||||
|
||||
if (this.opts.helpers !== false) {
|
||||
const baseHelpersDir = this.opts.useBuiltIns ? "helpers/builtin" : "helpers";
|
||||
const helpersDir = this.opts.useESModules ? `${baseHelpersDir}/es6` : baseHelpersDir;
|
||||
file.set("helperGenerator", function (name) {
|
||||
const baseHelpersDir = this.opts.useBuiltIns
|
||||
? "helpers/builtin"
|
||||
: "helpers";
|
||||
const helpersDir = this.opts.useESModules
|
||||
? `${baseHelpersDir}/es6`
|
||||
: baseHelpersDir;
|
||||
file.set("helperGenerator", function(name) {
|
||||
if (HELPER_BLACKLIST.indexOf(name) < 0) {
|
||||
return file.addImport(`${moduleName}/${helpersDir}/${name}`, "default", name);
|
||||
return file.addImport(
|
||||
`${moduleName}/${helpersDir}/${name}`,
|
||||
"default",
|
||||
name,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (this.opts.polyfill && this.opts.useBuiltIns) {
|
||||
throw new Error("The polyfill option conflicts with useBuiltIns; use one or the other");
|
||||
throw new Error(
|
||||
"The polyfill option conflicts with useBuiltIns; use one or the other",
|
||||
);
|
||||
}
|
||||
|
||||
this.setDynamic("regeneratorIdentifier", function () {
|
||||
return file.addImport(`${moduleName}/regenerator`, "default", "regeneratorRuntime");
|
||||
this.setDynamic("regeneratorIdentifier", function() {
|
||||
return file.addImport(
|
||||
`${moduleName}/regenerator`,
|
||||
"default",
|
||||
"regeneratorRuntime",
|
||||
);
|
||||
});
|
||||
},
|
||||
|
||||
@@ -38,7 +52,10 @@ export default function ({ types: t }) {
|
||||
ReferencedIdentifier(path, state) {
|
||||
const { node, parent, scope } = path;
|
||||
|
||||
if (node.name === "regeneratorRuntime" && state.opts.regenerator !== false) {
|
||||
if (
|
||||
node.name === "regeneratorRuntime" &&
|
||||
state.opts.regenerator !== false
|
||||
) {
|
||||
path.replaceWith(state.get("regeneratorIdentifier"));
|
||||
return;
|
||||
}
|
||||
@@ -51,11 +68,13 @@ export default function ({ types: t }) {
|
||||
|
||||
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(state.addImport(
|
||||
`${moduleName}/core-js/${definitions.builtins[node.name]}`,
|
||||
"default",
|
||||
node.name
|
||||
));
|
||||
path.replaceWith(
|
||||
state.addImport(
|
||||
`${moduleName}/core-js/${definitions.builtins[node.name]}`,
|
||||
"default",
|
||||
node.name,
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
// arr[Symbol.iterator]() -> _core.$for.getIterator(arr)
|
||||
@@ -68,17 +87,21 @@ export default function ({ types: t }) {
|
||||
const callee = path.node.callee;
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (!callee.computed) return;
|
||||
if (!path.get("callee.property").matchesPattern("Symbol.iterator")) return;
|
||||
if (!path.get("callee.property").matchesPattern("Symbol.iterator")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(t.callExpression(
|
||||
state.addImport(
|
||||
`${moduleName}/core-js/get-iterator`,
|
||||
"default",
|
||||
"getIterator"
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
state.addImport(
|
||||
`${moduleName}/core-js/get-iterator`,
|
||||
"default",
|
||||
"getIterator",
|
||||
),
|
||||
[callee.object],
|
||||
),
|
||||
[callee.object]
|
||||
));
|
||||
);
|
||||
},
|
||||
|
||||
// Symbol.iterator in arr -> core.$for.isIterable(arr)
|
||||
@@ -89,14 +112,16 @@ export default function ({ types: t }) {
|
||||
if (!path.get("left").matchesPattern("Symbol.iterator")) return;
|
||||
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(t.callExpression(
|
||||
state.addImport(
|
||||
`${moduleName}/core-js/is-iterable`,
|
||||
"default",
|
||||
"isIterable"
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
state.addImport(
|
||||
`${moduleName}/core-js/is-iterable`,
|
||||
"default",
|
||||
"isIterable",
|
||||
),
|
||||
[path.node.right],
|
||||
),
|
||||
[path.node.right]
|
||||
));
|
||||
);
|
||||
},
|
||||
|
||||
// Array.from -> _core.Array.from
|
||||
@@ -120,17 +145,25 @@ export default function ({ types: t }) {
|
||||
if (path.scope.getBindingIdentifier(obj.name)) return;
|
||||
|
||||
// special case Object.defineProperty to not use core-js when using string keys
|
||||
if (obj.name === "Object" && prop.name === "defineProperty" && path.parentPath.isCallExpression()) {
|
||||
if (
|
||||
obj.name === "Object" &&
|
||||
prop.name === "defineProperty" &&
|
||||
path.parentPath.isCallExpression()
|
||||
) {
|
||||
const call = path.parentPath.node;
|
||||
if (call.arguments.length === 3 && t.isLiteral(call.arguments[1])) return;
|
||||
if (call.arguments.length === 3 && t.isLiteral(call.arguments[1])) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(state.addImport(
|
||||
`${moduleName}/core-js/${methods[prop.name]}`,
|
||||
"default",
|
||||
`${obj.name}$${prop.name}`
|
||||
));
|
||||
path.replaceWith(
|
||||
state.addImport(
|
||||
`${moduleName}/core-js/${methods[prop.name]}`,
|
||||
"default",
|
||||
`${obj.name}$${prop.name}`,
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
exit(path, state) {
|
||||
@@ -144,15 +177,17 @@ export default function ({ types: t }) {
|
||||
if (path.scope.getBindingIdentifier(obj.name)) return;
|
||||
|
||||
const moduleName = getRuntimeModuleName(state.opts);
|
||||
path.replaceWith(t.memberExpression(
|
||||
state.addImport(
|
||||
`${moduleName}/core-js/${definitions.builtins[obj.name]}`,
|
||||
"default",
|
||||
obj.name
|
||||
path.replaceWith(
|
||||
t.memberExpression(
|
||||
state.addImport(
|
||||
`${moduleName}/core-js/${definitions.builtins[obj.name]}`,
|
||||
"default",
|
||||
obj.name,
|
||||
),
|
||||
node.property,
|
||||
node.computed,
|
||||
),
|
||||
node.property,
|
||||
node.computed
|
||||
));
|
||||
);
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user