Skip to content

Commit

Permalink
fix: pass File to helper traverser
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Oct 18, 2019
1 parent 125dfd0 commit 0c80844
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/babel-helpers/package.json
Expand Up @@ -17,5 +17,8 @@
},
"devDependencies": {
"@babel/helper-plugin-test-runner": "^7.0.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
}
}
27 changes: 19 additions & 8 deletions packages/babel-helpers/src/index.js
@@ -1,3 +1,4 @@
import { File } from "@babel/core";
import traverse from "@babel/traverse";
import * as t from "@babel/types";
import helpers from "./helpers";
Expand Down Expand Up @@ -29,7 +30,7 @@ function getHelperMetadata(file) {
const importPaths = [];
const importBindingsReferences = [];

traverse(file, {
const dependencyVisitor = {
ImportDeclaration(child) {
const name = child.node.source.value;
if (!helpers[name]) {
Expand Down Expand Up @@ -72,9 +73,9 @@ function getHelperMetadata(file) {

child.skip();
},
});
};

traverse(file, {
const referenceVisitor = {
Program(path) {
const bindings = path.scope.getAllBindings();

Expand Down Expand Up @@ -111,7 +112,10 @@ function getHelperMetadata(file) {
exportBindingAssignments.push(makePath(child));
}
},
});
};

traverse(file.ast, dependencyVisitor, file.scope);
traverse(file.ast, referenceVisitor, file.scope);

if (!exportPath) throw new Error("Helpers must default-export something.");

Expand Down Expand Up @@ -170,7 +174,7 @@ function permuteHelperAST(file, metadata, id, localBindings, getDependency) {
toRename[exportName] = id.name;
}

traverse(file, {
const visitor = {
Program(path) {
// We need to compute these in advance because removing nodes would
// invalidate the paths.
Expand Down Expand Up @@ -223,7 +227,8 @@ function permuteHelperAST(file, metadata, id, localBindings, getDependency) {
// actually doing the traversal.
path.stop();
},
});
};
traverse(file.ast, visitor, file.scope);
}

const helperData = Object.create(null);
Expand All @@ -238,7 +243,13 @@ function loadHelper(name) {
}

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

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

return {
nodes: file.program.body,
nodes: file.path.node.body,
globals: metadata.globals,
};
},
Expand Down

0 comments on commit 0c80844

Please sign in to comment.