add some tests

This commit is contained in:
Henry Zhu 2016-12-02 17:58:31 -05:00
parent 3a7a1b9221
commit d06270498b
12 changed files with 71 additions and 16 deletions

View File

@ -2,14 +2,8 @@
// or if more than one is found
const polyfillSource = "babel-polyfill";
let numPolyfillImports = 0;
export default function ({ types: t }) {
function checkNumPolyfillImports() {
numPolyfillImports++;
return numPolyfillImports > 1;
}
function addImport(polyfill) {
let declar = t.importDeclaration([], t.stringLiteral(`core-js/modules/${polyfill}`));
declar._blockHoist = 3;
@ -41,7 +35,8 @@ export default function ({ types: t }) {
ImportDeclaration(path, state) {
if (path.node.specifiers.length === 0 &&
path.node.source.value === polyfillSource) {
if (checkNumPolyfillImports()) {
this.numPolyfillImports++;
if (this.numPolyfillImports > 1) {
path.remove();
return;
}
@ -53,19 +48,19 @@ export default function ({ types: t }) {
Program(path, state) {
if (!state.opts.polyfills) {
throw path.buildCodeFrameError(`
"polyfills" option not correctly passed
to the transform-polyfill-require plugin
in babel-preset-env
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, polyfillSource)) {
if (checkNumPolyfillImports()) {
this.numPolyfillImports++;
if (this.numPolyfillImports > 1) {
path.remove();
return;
}
let requires = state.opts.polyfills.map((p) => addRequire(p));
bodyPath.replaceWithMultiple(requires);
}
@ -75,6 +70,9 @@ in babel-preset-env
return {
name: "transform-polyfill-require",
visitor: isPolyfillImport
visitor: isPolyfillImport,
pre() {
this.numPolyfillImports = 0;
}
};
}

View File

@ -0,0 +1,2 @@
import "babel-polyfill";
1 ** 2;

View File

@ -0,0 +1,8 @@
import "core-js/modules/es6.typed.data-view";
import "core-js/modules/es6.reflect.apply";
import "core-js/modules/es6.reflect.own-keys";
import "core-js/modules/es6.symbol.iterator";
import "core-js/modules/es6.symbol.species";
import "core-js/modules/es6.array.from";
1 ** 2;

View File

@ -2,7 +2,7 @@
"presets": [
["../../../../lib", {
"targets": {
"chrome": 54
"chrome": 55
},
"modules": false,
"useBuiltIns": true

View File

@ -0,0 +1,3 @@
import "babel-polyfill";
import "babel-polyfill";
1 ** 2;

View File

@ -0,0 +1,8 @@
import "core-js/modules/es6.typed.data-view";
import "core-js/modules/es6.reflect.apply";
import "core-js/modules/es6.reflect.own-keys";
import "core-js/modules/es6.symbol.iterator";
import "core-js/modules/es6.symbol.species";
import "core-js/modules/es6.array.from";
1 ** 2;

View File

@ -0,0 +1,11 @@
{
"presets": [
["../../../../lib", {
"targets": {
"chrome": 55
},
"modules": false,
"useBuiltIns": true
}]
]
}

View File

@ -0,0 +1,3 @@
require("babel-polyfill");
1 ** 2;

View File

@ -0,0 +1,13 @@
require("core-js/modules/es6.typed.data-view");
require("core-js/modules/es6.reflect.apply");
require("core-js/modules/es6.reflect.own-keys");
require("core-js/modules/es6.symbol.iterator");
require("core-js/modules/es6.symbol.species");
require("core-js/modules/es6.array.from");
1 ** 2;

View File

@ -0,0 +1,11 @@
{
"presets": [
["../../../../lib", {
"targets": {
"chrome": 55
},
"modules": false,
"useBuiltIns": true
}]
]
}