Add supports for polyfill computed methods (#10398)
This commit is contained in:
parent
5c859b1117
commit
8769903284
@ -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(
|
||||
@ -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)) {
|
||||
@ -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) ||
|
||||
|
||||
@ -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']()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user