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

Add supports for polyfill computed methods #10398

Merged
merged 1 commit into from Sep 5, 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
49 changes: 32 additions & 17 deletions packages/babel-plugin-transform-runtime/src/index.js
Expand Up @@ -130,6 +130,14 @@ export default declare((api, options, dirname) => {
return methods[name].types.some(name => name === type);
}

function resolvePropertyName(path, computed) {
const { node } = path;
if (!computed) return node.name;
if (path.isStringLiteral()) return node.value;
const result = path.evaluate();
return result.value;
}

if (has(options, "useBuiltIns")) {
if (options.useBuiltIns) {
throw new Error(
Expand Down Expand Up @@ -297,8 +305,11 @@ export default declare((api, options, dirname) => {

if (!t.isMemberExpression(callee)) return;

const { object, property } = callee;
const propertyName = property.name;
const { object } = callee;
const propertyName = resolvePropertyName(
path.get("callee.property"),
callee.computed,
);

// transform calling instance methods like `something.includes()`
if (injectCoreJS3 && !hasStaticMapping(object.name, propertyName)) {
Expand Down Expand Up @@ -375,29 +386,33 @@ export default declare((api, options, dirname) => {
if (!path.isReferenced()) return;

const { node } = path;
const { object, property } = node;
const { object } = node;

if (!t.isReferenced(object, node)) return;

if (node.computed) {
if (injectCoreJS2) return;
// transform `something[Symbol.iterator]` to calling `getIteratorMethod(something)` helper
if (path.get("property").matchesPattern("Symbol.iterator")) {
path.replaceWith(
t.callExpression(
this.addDefaultImport(
`${moduleName}/core-js/get-iterator-method`,
"getIteratorMethod",
),
[object],
// transform `something[Symbol.iterator]` to calling `getIteratorMethod(something)` helper
if (
!injectCoreJS2 &&
node.computed &&
path.get("property").matchesPattern("Symbol.iterator")
) {
path.replaceWith(
t.callExpression(
this.addDefaultImport(
`${moduleName}/core-js/get-iterator-method`,
"getIteratorMethod",
),
);
}
[object],
),
);
return;
}

const objectName = object.name;
const propertyName = property.name;
const propertyName = resolvePropertyName(
path.get("property"),
node.computed,
);
// doesn't reference the global
if (
path.scope.getBindingIdentifier(objectName) ||
Expand Down
@@ -0,0 +1,5 @@
var _isArray = "isArray";

Array["from"]; // polyfill
Array[_isArray]; // polyfill
Array[of]; // don't polyfill
@@ -0,0 +1,3 @@
{
"plugins": [["transform-runtime", { "corejs": 2 }]]
}
@@ -0,0 +1,10 @@
var _Array$isArray = require("@babel/runtime-corejs2/core-js/array/is-array");

var _Array$from = require("@babel/runtime-corejs2/core-js/array/from");

var _isArray = "isArray";
_Array$from; // polyfill

_Array$isArray; // polyfill

Array[of]; // don't polyfill
@@ -1 +1 @@
bar[filter]()
bar[filter]()
@@ -0,0 +1,9 @@
var _map = "map";

object["filter"]; // polyfill
object[_map]; // polyfill
object[find]; // don't polyfill

object["filter"](); // polyfill
object[_map](); // polyfill
object[find](); // don't polyfill
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-runtime", { "corejs": { "version": 3, "proposals": true } }]
]
}
@@ -0,0 +1,21 @@
var _mapInstanceProperty = require("@babel/runtime-corejs3/core-js/instance/map");

var _filterInstanceProperty = require("@babel/runtime-corejs3/core-js/instance/filter");

var _map = "map";

_filterInstanceProperty(object); // polyfill


_mapInstanceProperty(object); // polyfill


object[find]; // don't polyfill

_filterInstanceProperty(object).call(object); // polyfill


_mapInstanceProperty(object).call(object); // polyfill


object[find](); // don't polyfill
@@ -0,0 +1,5 @@
var _isArray = "isArray";

Array["from"]; // polyfill
Array[_isArray]; // polyfill
Array[of]; // don't polyfill
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-runtime", { "corejs": { "version": 3, "proposals": true } }]
]
}
@@ -0,0 +1,10 @@
var _Array$isArray = require("@babel/runtime-corejs3/core-js/array/is-array");

var _Array$from = require("@babel/runtime-corejs3/core-js/array/from");

var _isArray = "isArray";
_Array$from; // polyfill

_Array$isArray; // polyfill

Array[of]; // don't polyfill
@@ -1 +1 @@
bar[filter]()
bar['filter']()