Skip to content

Commit

Permalink
Remove babel polyfill from fixture test runner (#12130)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
JLHwung and nicolo-ribaudo committed Dec 22, 2020
1 parent 14534e6 commit 814212f
Show file tree
Hide file tree
Showing 27 changed files with 561 additions and 158 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ build-no-bundle: clean clean-lib
BABEL_ENV=development $(YARN) gulp build-dev
# Ensure that build artifacts for types are created during local
# development too.
# Babel-transform-fixture-test-runner requires minified polyfill for performance
$(MAKE) build-flow-typings build-polyfill-dist
$(MAKE) build-flow-typings

watch: build-no-bundle
BABEL_ENV=development $(YARN) gulp watch
Expand Down
68 changes: 67 additions & 1 deletion packages/babel-helper-fixtures/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,67 @@ function pushTask(taskName, taskDir, suite, suiteName) {
delete test.options.ignoreOutput;
}

function wrapPackagesArray(type, names, optionsDir) {
return names.map(function (val) {
if (typeof val === "string") val = [val];

// relative path (outside of monorepo)
if (val[0][0] === ".") {
if (!optionsDir) {
throw new Error(
"Please provide an options.json in test dir when using a " +
"relative plugin path.",
);
}

val[0] = path.resolve(optionsDir, val[0]);
} else {
const monorepoPath = __dirname + "/../../babel-" + type + "-" + val[0];

if (fs.existsSync(monorepoPath)) {
val[0] = monorepoPath;
}
}

return val;
});
}

/**
* Resolve plugins/presets defined in options.json
*
* @export
* @param {{}} options the imported options.json
* @param {string} optionsDir the direcotry where options.json is placed
* @returns {{}} options whose plugins/presets are resolved
*/
export function resolveOptionPluginOrPreset(
options: {},
optionsDir: string,
): {} {
if (options.plugins) {
options.plugins = wrapPackagesArray("plugin", options.plugins, optionsDir);
}
if (options.presets) {
options.presets = wrapPackagesArray(
"preset",
options.presets,
optionsDir,
).map(function (val) {
if (val.length > 3) {
throw new Error(
"Unexpected extra options " +
JSON.stringify(val.slice(3)) +
" passed to preset.",
);
}

return val;
});
}
return options;
}

export default function get(entryLoc): Array<Suite> {
const suites = [];

Expand All @@ -277,7 +338,12 @@ export default function get(entryLoc): Array<Suite> {
suites.push(suite);

const suiteOptsLoc = tryResolve(suite.filename + "/options");
if (suiteOptsLoc) suite.options = require(suiteOptsLoc);
if (suiteOptsLoc) {
suite.options = resolveOptionPluginOrPreset(
require(suiteOptsLoc),
suite.filename,
);
}

for (const taskName of fs.readdirSync(suite.filename)) {
pushTask(taskName, suite.filename + "/" + taskName, suite, suiteName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"@babel/code-frame": "workspace:^7.10.4",
"@babel/core": "workspace:^7.12.10",
"@babel/helper-fixtures": "workspace:^7.12.10",
"@babel/polyfill": "workspace:^7.12.1",
"babel-check-duplicated-nodes": "^1.0.0",
"jest-diff": "^24.8.0",
"lodash": "^4.17.19",
"quick-lru": "5.1.0",
"regenerator-runtime": "^0.13.7",
"source-map": "^0.5.0"
}
}
52 changes: 6 additions & 46 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-env jest */
import * as babel from "@babel/core";
import { buildExternalHelpers } from "@babel/core";
import getFixtures from "@babel/helper-fixtures";
import {
default as getFixtures,
resolveOptionPluginOrPreset,
} from "@babel/helper-fixtures";
import sourceMap from "source-map";
import { codeFrameColumns } from "@babel/code-frame";
import escapeRegExp from "lodash/escapeRegExp";
Expand Down Expand Up @@ -37,7 +40,7 @@ function createContext() {
// Initialize the test context with the polyfill, and then freeze the global to prevent implicit
// global creation in tests, which could cause things to bleed between tests.
runModuleInTestContext(
"@babel/polyfill/dist/polyfill.min.js",
"regenerator-runtime",
__filename,
context,
moduleCache,
Expand Down Expand Up @@ -167,32 +170,6 @@ export function runCodeInTestContext(
}
}

function wrapPackagesArray(type, names, optionsDir) {
return (names || []).map(function (val) {
if (typeof val === "string") val = [val];

// relative path (outside of monorepo)
if (val[0][0] === ".") {
if (!optionsDir) {
throw new Error(
"Please provide an options.json in test dir when using a " +
"relative plugin path.",
);
}

val[0] = path.resolve(optionsDir, val[0]);
} else {
const monorepoPath = __dirname + "/../../babel-" + type + "-" + val[0];

if (fs.existsSync(monorepoPath)) {
val[0] = monorepoPath;
}
}

return val;
});
}

function run(task) {
const {
actual,
Expand Down Expand Up @@ -221,24 +198,7 @@ function run(task) {
opts,
);

newOpts.plugins = wrapPackagesArray("plugin", newOpts.plugins, optionsDir);
newOpts.presets = wrapPackagesArray(
"preset",
newOpts.presets,
optionsDir,
).map(function (val) {
if (val.length > 3) {
throw new Error(
"Unexpected extra options " +
JSON.stringify(val.slice(3)) +
" passed to preset.",
);
}

return val;
});

return newOpts;
return resolveOptionPluginOrPreset(newOpts, optionsDir);
}

let execCode = exec.code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
},
"devDependencies": {
"@babel/core": "workspace:*",
"@babel/helper-plugin-test-runner": "workspace:*"
"@babel/helper-plugin-test-runner": "workspace:*",
"babel-plugin-polyfill-corejs3": "0.0.10",
"core-js-pure": "^3.8.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const log = [];

async function* func1() {
log.push(1);
yield "a";
log.push(2);
}

async function* func2() {
yield* func1();
log.push(3);
}

return (async () => {
const iterator = func2();
await iterator.next();
await iterator.return();

expect(log).toEqual([1]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"parserOpts": {
"allowReturnOutsideFunction": true
},
"plugins": [
"transform-async-to-generator",
"proposal-async-generator-functions",
["babel-plugin-polyfill-corejs3", { "method": "usage-pure", "targets": {
"node": "6.17"
}}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const log = [];

async function* inner() {
try {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
} finally {
log.push(4);
yield "c";
log.push(5);
}
}

async function* outer() {
log.push(6);
yield* inner();
log.push(7);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([6, 1]);

const [res1, res2] = await Promise.all([ iterator.return("x"), iterator.return("y") ]);
expect(res1).toEqual({ value: "c", done: false });
expect(res2).toEqual({ value: "y", done: true });
expect(log).toEqual([6, 1, 4]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const log = [];

async function* inner() {
try {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
} finally {
log.push(4);
yield "c";
log.push(5);
}
}

async function* outer() {
log.push(6);
yield* inner();
log.push(7);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([6, 1]);

res = await iterator.return("x");
expect(res).toEqual({ value: "c", done: false });
expect(log).toEqual([6, 1, 4]);

res = await iterator.return("y");
expect(res).toEqual({ value: "y", done: true });
expect(log).toEqual([6, 1, 4]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const log = [];

async function* inner() {
try {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
} finally {
log.push(4);
yield "c";
log.push(5);
}
}

async function* outer() {
log.push(6);
yield* inner();
log.push(7);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([6, 1]);

res = await iterator.return();
expect(res).toEqual({ value: "c", done: false });
expect(log).toEqual([6, 1, 4]);

res = await iterator.next();
expect(res).toEqual({ value: undefined, done: true });
expect(log).toEqual([6, 1, 4, 5, 7]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const log = [];

async function* inner() {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
}

async function* outer() {
log.push(4);
yield* inner();
log.push(5);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([4, 1]);

res = await iterator.return();
expect(res).toEqual({ value: undefined, done: true });
expect(log).toEqual([4, 1]);

res = await iterator.next();
expect(res).toEqual({ value: undefined, done: true });
expect(log).toEqual([4, 1]);
})();

0 comments on commit 814212f

Please sign in to comment.