Add supports for polyfill computed methods (#10398)

This commit is contained in:
Bin Xin 2019-09-06 07:35:03 +08:00 committed by Nicolò Ribaudo
parent 5c859b1117
commit 8769903284
12 changed files with 107 additions and 19 deletions

View File

@ -130,6 +130,14 @@ export default declare((api, options, dirname) => {
return methods[name].types.some(name => name === type); 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 (has(options, "useBuiltIns")) {
if (options.useBuiltIns) { if (options.useBuiltIns) {
throw new Error( throw new Error(
@ -297,8 +305,11 @@ export default declare((api, options, dirname) => {
if (!t.isMemberExpression(callee)) return; if (!t.isMemberExpression(callee)) return;
const { object, property } = callee; const { object } = callee;
const propertyName = property.name; const propertyName = resolvePropertyName(
path.get("callee.property"),
callee.computed,
);
// transform calling instance methods like `something.includes()` // transform calling instance methods like `something.includes()`
if (injectCoreJS3 && !hasStaticMapping(object.name, propertyName)) { if (injectCoreJS3 && !hasStaticMapping(object.name, propertyName)) {
@ -375,29 +386,33 @@ export default declare((api, options, dirname) => {
if (!path.isReferenced()) return; if (!path.isReferenced()) return;
const { node } = path; const { node } = path;
const { object, property } = node; const { object } = node;
if (!t.isReferenced(object, node)) return; if (!t.isReferenced(object, node)) return;
if (node.computed) { // transform `something[Symbol.iterator]` to calling `getIteratorMethod(something)` helper
if (injectCoreJS2) return; if (
// transform `something[Symbol.iterator]` to calling `getIteratorMethod(something)` helper !injectCoreJS2 &&
if (path.get("property").matchesPattern("Symbol.iterator")) { node.computed &&
path.replaceWith( path.get("property").matchesPattern("Symbol.iterator")
t.callExpression( ) {
this.addDefaultImport( path.replaceWith(
`${moduleName}/core-js/get-iterator-method`, t.callExpression(
"getIteratorMethod", this.addDefaultImport(
), `${moduleName}/core-js/get-iterator-method`,
[object], "getIteratorMethod",
), ),
); [object],
} ),
);
return; return;
} }
const objectName = object.name; const objectName = object.name;
const propertyName = property.name; const propertyName = resolvePropertyName(
path.get("property"),
node.computed,
);
// doesn't reference the global // doesn't reference the global
if ( if (
path.scope.getBindingIdentifier(objectName) || path.scope.getBindingIdentifier(objectName) ||

View File

@ -0,0 +1,5 @@
var _isArray = "isArray";
Array["from"]; // polyfill
Array[_isArray]; // polyfill
Array[of]; // don't polyfill

View File

@ -0,0 +1,3 @@
{
"plugins": [["transform-runtime", { "corejs": 2 }]]
}

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,5 @@
{
"plugins": [
["transform-runtime", { "corejs": { "version": 3, "proposals": true } }]
]
}

View File

@ -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

View File

@ -0,0 +1,5 @@
var _isArray = "isArray";
Array["from"]; // polyfill
Array[_isArray]; // polyfill
Array[of]; // don't polyfill

View File

@ -0,0 +1,5 @@
{
"plugins": [
["transform-runtime", { "corejs": { "version": 3, "proposals": true } }]
]
}

View File

@ -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

View File

@ -1 +1 @@
bar[filter]() bar['filter']()