Skip to content

Commit

Permalink
pass babel.File to helpers.ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Oct 21, 2019
1 parent 0c80844 commit d7f76e3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/babel-core/src/tools/build-external-helpers.js
Expand Up @@ -2,6 +2,7 @@ import * as helpers from "@babel/helpers";
import generator from "@babel/generator";
import template from "@babel/template";
import * as t from "@babel/types";
import File from "..";

// Wrapped to avoid wasting time parsing this when almost no-one uses
// build-external-helpers.
Expand Down Expand Up @@ -136,6 +137,7 @@ function buildHelpers(body, namespace, whitelist) {

const ref = (refs[name] = getHelperReference(name));

helpers.ensure(name, File);
const { nodes } = helpers.get(name, getHelperReference, ref);

body.push(...nodes);
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/transformation/file/file.js
Expand Up @@ -204,7 +204,7 @@ export default class File {
}

// make sure that the helper exists
helpers.ensure(name);
helpers.ensure(name, File);

const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
name,
Expand Down
3 changes: 0 additions & 3 deletions packages/babel-helpers/package.json
Expand Up @@ -17,8 +17,5 @@
},
"devDependencies": {
"@babel/helper-plugin-test-runner": "^7.0.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
}
}
28 changes: 18 additions & 10 deletions packages/babel-helpers/src/index.js
@@ -1,4 +1,3 @@
import { File } from "@babel/core";
import traverse from "@babel/traverse";
import * as t from "@babel/types";
import helpers from "./helpers";
Expand All @@ -14,6 +13,7 @@ function makePath(path) {
return parts.reverse().join(".");
}

let fileClass = undefined;
/**
* Given a file AST for a given helper, get a bunch of metadata about it so that Babel can quickly render
* the helper is whatever context it is needed in.
Expand Down Expand Up @@ -243,13 +243,16 @@ function loadHelper(name) {
}

const fn = () => {
const ast = t.file(helper.ast());
return new File(
{
filename: `babel-helper://${name}`,
},
{ ast },
);
const file = { ast: t.file(helper.ast()) };
if (fileClass) {
return new fileClass(
{
filename: `babel-helper://${name}`,
},
file,
);
}
return file;
};

const metadata = getHelperMetadata(fn());
Expand All @@ -260,7 +263,7 @@ function loadHelper(name) {
permuteHelperAST(file, metadata, id, localBindings, getDependency);

return {
nodes: file.path.node.body,
nodes: file.ast.program.body,
globals: metadata.globals,
};
},
Expand Down Expand Up @@ -291,7 +294,12 @@ export function getDependencies(name: string): $ReadOnlyArray<string> {
return Array.from(loadHelper(name).dependencies.values());
}

export function ensure(name: string) {
export function ensure(name: string, newFileClass?) {
if (!fileClass) {
// optional fileClass used to wrap helper snippets into File instance,
// offering `path.hub` support during traversal
fileClass = newFileClass;
}
loadHelper(name);
}

Expand Down
Expand Up @@ -133,6 +133,7 @@ function buildHelper(

if (!esm) {
bindings = [];
helpers.ensure(helperName, babel.File);
for (const dep of helpers.getDependencies(helperName)) {
const id = (dependencies[dep] = t.identifier(t.toIdentifier(dep)));
tree.body.push(template.statement.ast`
Expand Down

0 comments on commit d7f76e3

Please sign in to comment.