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

[runtime-corejs3] Only polyfill instance methods when it might be needed #9754

Merged
merged 1 commit into from Apr 2, 2019
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
14 changes: 14 additions & 0 deletions packages/babel-plugin-transform-runtime/src/helpers.js
@@ -1,4 +1,5 @@
import semver from "semver";
import { types as t } from "@babel/core";

export function hasMinVersion(minVersion, runtimeVersion) {
// If the range is unavailable, we're running the script during Babel's
Expand Down Expand Up @@ -30,3 +31,16 @@ export function hasMinVersion(minVersion, runtimeVersion) {
!semver.intersects(`>=8.0.0`, runtimeVersion)
);
}

// Note: We can't use NodePath#couldBeBaseType because it doesn't support arrays.
// Even if we added support for arrays, this package needs to be compatible with
// ^7.0.0 so we can't rely on it.
export function typeAnnotationToString(node) {
switch (node.type) {
case "GenericTypeAnnotation":
if (t.isIdentifier(node.id, { name: "Array" })) return "array";
break;
case "StringTypeAnnotation":
return "string";
}
}
26 changes: 24 additions & 2 deletions packages/babel-plugin-transform-runtime/src/index.js
Expand Up @@ -6,6 +6,7 @@ import { types as t } from "@babel/core";

import getCoreJS2Definitions from "./runtime-corejs2-definitions";
import getCoreJS3Definitions from "./runtime-corejs3-definitions";
import { typeAnnotationToString } from "./helpers";

function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
try {
Expand Down Expand Up @@ -112,6 +113,16 @@ export default declare((api, options, dirname) => {
);
}

function maybeNeedsPolyfill(path, methods, name) {
if (!methods[name].types) return true;

const typeAnnotation = path.get("object").getTypeAnnotation();
const type = typeAnnotationToString(typeAnnotation);
if (!type) return true;

return methods[name].types.some(name => name === type);
}

if (has(options, "useBuiltIns")) {
if (options.useBuiltIns) {
throw new Error(
Expand Down Expand Up @@ -284,7 +295,14 @@ export default declare((api, options, dirname) => {

// transform calling instance methods like `something.includes()`
if (injectCoreJS3 && !hasStaticMapping(object.name, propertyName)) {
if (hasMapping(InstanceProperties, propertyName)) {
if (
hasMapping(InstanceProperties, propertyName) &&
maybeNeedsPolyfill(
path.get("callee"),
InstanceProperties,
propertyName,
)
) {
let context1, context2;
if (t.isIdentifier(object)) {
context1 = object;
Expand Down Expand Up @@ -381,7 +399,11 @@ export default declare((api, options, dirname) => {
!hasStaticMapping(objectName, propertyName)
) {
// transform getting of instance methods like `method = something.includes`
if (injectCoreJS3 && hasMapping(InstanceProperties, propertyName)) {
if (
injectCoreJS3 &&
hasMapping(InstanceProperties, propertyName) &&
maybeNeedsPolyfill(path, InstanceProperties, propertyName)
) {
path.replaceWith(
t.callExpression(
this.addDefaultImport(
Expand Down
Expand Up @@ -180,12 +180,16 @@ export default () => {
},
},

// NOTE: You can specify the object types whose method needs to be polyfilled.
// e.g. concat: { types: ["array"] }
// See ./helpers.js@typeAnnotationToString for the supported types

InstanceProperties: {
at: { stable: false, path: "at" },
bind: { stable: true, path: "bind" },
codePointAt: { stable: true, path: "code-point-at" },
codePoints: { stable: false, path: "code-points" },
concat: { stable: true, path: "concat" },
concat: { stable: true, path: "concat", types: ["array"] },
copyWithin: { stable: true, path: "copy-within" },
endsWith: { stable: true, path: "ends-with" },
entries: { stable: true, path: "entries" },
Expand Down
@@ -0,0 +1 @@
require(`./locale/${lan}`);
@@ -0,0 +1,6 @@
{
"plugins": [
["transform-runtime", { "corejs": 3 }],
"transform-template-literals"
]
}
@@ -0,0 +1 @@
require("./locale/".concat(lan));
@@ -0,0 +1,3 @@
"".concat(b);
[].concat(b);
a.concat(b);
danez marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
{
"plugins": [["transform-runtime", { "corejs": 3 }]]
}
@@ -0,0 +1,9 @@
var _concatInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/concat");

var _context;

"".concat(b);

_concatInstanceProperty(_context = []).call(_context, b);

_concatInstanceProperty(a).call(a, b);