Skip to content

Commit

Permalink
Add supports for polyfill computed methods (#10398)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyzx authored and nicolo-ribaudo committed Sep 5, 2019
1 parent 5c859b1 commit 8769903
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 19 deletions.
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']()

0 comments on commit 8769903

Please sign in to comment.