Skip to content

Commit

Permalink
account for web.iterable (babel#283)
Browse files Browse the repository at this point in the history
* account for web.iterable

* extra test, remove unncessary warning
  • Loading branch information
hzoo authored and ramasilveyra committed Sep 30, 2017
1 parent 57d41df commit c9c5b21
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 28 deletions.
9 changes: 0 additions & 9 deletions packages/babel-preset-env/src/use-built-ins-entry-plugin.js
Expand Up @@ -64,15 +64,6 @@ export default function({ types: t }) {
}
},
Program(path, state) {
if (!state.opts.polyfills) {
throw path.buildCodeFrameError(
`
There was an issue in "babel-preset-env" such that
the "polyfills" option was not correctly passed
to the "transform-polyfill-require" plugin
`,
);
}
path.get("body").forEach(bodyPath => {
if (isRequire(bodyPath)) {
bodyPath.replaceWithMultiple(
Expand Down
66 changes: 53 additions & 13 deletions packages/babel-preset-env/src/use-built-ins-plugin.js
Expand Up @@ -78,16 +78,7 @@ Please remove the "import 'babel-polyfill'" call or use "useBuiltIns: 'entry'" i
}
},
Program: {
enter(path, state) {
if (!state.opts.polyfills) {
throw path.buildCodeFrameError(
`
There was an issue in "babel-preset-env" such that
the "polyfills" option was not correctly passed
to the "transform-polyfill-require" plugin
`,
);
}
enter(path) {
path.get("body").forEach(bodyPath => {
if (isRequire(bodyPath)) {
console.warn(
Expand All @@ -102,8 +93,8 @@ Please remove the "require('babel-polyfill')" call or use "useBuiltIns: 'entry'"
},
},

// Symbol() -> _core.Symbol();
// new Promise -> new _core.Promise
// Symbol()
// new Promise
ReferencedIdentifier(path, state) {
const { node, parent, scope } = path;

Expand All @@ -115,7 +106,49 @@ Please remove the "require('babel-polyfill')" call or use "useBuiltIns: 'entry'"
addUnsupported(path, state.opts.polyfills, builtIn, this.builtIns);
},

// Array.from -> _core.Array.from
// arr[Symbol.iterator]()
CallExpression(path) {
// we can't compile this
if (path.node.arguments.length) return;

const callee = path.node.callee;
if (!t.isMemberExpression(callee)) return;
if (!callee.computed) return;
if (!path.get("callee.property").matchesPattern("Symbol.iterator")) {
return;
}

addImport(
path,
"babel-polyfill/lib/core-js/modules/web.dom.iterable",
this.builtIns,
);
},

// Symbol.iterator in arr
BinaryExpression(path) {
if (path.node.operator !== "in") return;
if (!path.get("left").matchesPattern("Symbol.iterator")) return;

addImport(
path,
"babel-polyfill/lib/core-js/modules/web.dom.iterable",
this.builtIns,
);
},

// yield*
YieldExpression(path) {
if (!path.node.delegate) return;

addImport(
path,
"babel-polyfill/lib/core-js/modules/web.dom.iterable",
this.builtIns,
);
},

// Array.from
MemberExpression: {
enter(path, state) {
if (!path.isReferenced()) return;
Expand All @@ -134,6 +167,13 @@ Please remove the "require('babel-polyfill')" call or use "useBuiltIns: 'entry'"
if (has(staticMethods, prop.name)) {
const builtIn = staticMethods[prop.name];
addUnsupported(path, state.opts.polyfills, builtIn, this.builtIns);
// if (obj.name === "Array" && prop.name === "from") {
// addImport(
// path,
// "babel-polyfill/lib/core-js/modules/web.dom.iterable",
// this.builtIns,
// );
// }
}
}

Expand Down
@@ -0,0 +1 @@
Symbol.iterator in arr
@@ -0,0 +1,3 @@
import "babel-polyfill/lib/core-js/modules/es6.symbol";
import "babel-polyfill/lib/core-js/modules/web.dom.iterable";
Symbol.iterator in arr;
@@ -0,0 +1,8 @@
{
"presets": [
["../../../../lib", {
"useBuiltIns": true,
"modules": false
}]
]
}
@@ -0,0 +1 @@
arr[Symbol.iterator]()
@@ -0,0 +1,3 @@
import "babel-polyfill/lib/core-js/modules/es6.symbol";
import "babel-polyfill/lib/core-js/modules/web.dom.iterable";
arr[Symbol.iterator]();
@@ -0,0 +1,8 @@
{
"presets": [
["../../../../lib", {
"useBuiltIns": true,
"modules": false
}]
]
}
@@ -0,0 +1,3 @@
function* a() {
yield 1;
}
@@ -0,0 +1,3 @@
function* a() {
yield 1;
}
@@ -0,0 +1,11 @@
{
"presets": [
["../../../../lib", {
"useBuiltIns": true,
"targets": {
"chrome": 55
},
"modules": false
}]
]
}
@@ -0,0 +1,3 @@
function* a() {
yield* 1;
}
@@ -0,0 +1,4 @@
import "babel-polyfill/lib/core-js/modules/web.dom.iterable";
function* a() {
yield* 1;
}
@@ -0,0 +1,11 @@
{
"presets": [
["../../../../lib", {
"useBuiltIns": true,
"targets": {
"chrome": 55
},
"modules": false
}]
]
}
Expand Up @@ -9,4 +9,4 @@ d.fill.bind(); //.bind
e.padStart.apply(); // .apply
f.padEnd.call(); // .call
String.prototype.startsWith.call; // prototype.call
var { codePointAt, endsWith } = k; // destructuring
var { codePointAt, endsWith } = k; // destructuring
Expand Up @@ -36,4 +36,4 @@ i[asdf]; // computed with identifier
j["search"]; // computed with template
k[asdf3]; // computed with concat strings
var _k2 = k,
_a = _k2[asdf2]; // computed
_a = _k2[asdf2]; // computed
Expand Up @@ -2,7 +2,6 @@ Array.from; // static method
Map; // built-in
new Promise(); // new builtin
Symbol.match; // as member expression
_arr[Symbol.iterator](); // Symbol.iterator

// no import
Array.asdf;
Expand Down
Expand Up @@ -2,7 +2,6 @@ Array.from; // static method
Map; // built-in
new Promise(); // new builtin
Symbol.match; // as member expression
_arr[Symbol.iterator](); // Symbol.iterator

// no import
Array.asdf;
Expand All @@ -23,4 +22,4 @@ function H(WeakMap) {
var asdf = 'copyWithin';
i[asdf]; // computed with identifier
j[`copyWithin`]; // computed with template
var { [asdf]: _a } = k; // computed
var { [asdf]: _a } = k; // computed
@@ -1,3 +1,4 @@
import "babel-polyfill/lib/core-js/modules/web.dom.iterable";
import "babel-polyfill/lib/core-js/modules/es6.symbol";
import "babel-polyfill/lib/core-js/modules/es6.regexp.match";
import "babel-polyfill/lib/core-js/modules/es6.promise";
Expand All @@ -22,4 +23,4 @@ _arr9[Symbol.iterator2]();
G.assign; // static method
function H(WeakMap) {
var blah = new WeakMap();
} // shadowed
} // shadowed

0 comments on commit c9c5b21

Please sign in to comment.