Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to walk AST's parsed with stage3 #1

Open
brickpop opened this issue Jul 10, 2018 · 3 comments
Open

Unable to walk AST's parsed with stage3 #1

brickpop opened this issue Jul 10, 2018 · 3 comments

Comments

@brickpop
Copy link

I am trying to walk an AST parsed with the stage3 plugin, but both the injector and the decorated acorn instance throw an error.

The following code:

const acorn = require("acorn-stage3");
const walk = require("acorn/dist/walk");

const content = `
import("./module.js");
test("foo");
`;
const parsed = acorn.parse(content, { sourceType: 'module', plugins: { stage3: true } });

walk.simple(parsed, {
    CallExpression(node) { console.log(node) }
});

Will throw an error like:

TypeError: baseVisitor[type] is not a function
    at c (/private/tmp/prova/node_modules/acorn/dist/walk.js:29:22)
    at Object.skipThrough (/private/tmp/prova/node_modules/acorn/dist/walk.js:183:37)
    at c (/private/tmp/prova/node_modules/acorn/dist/walk.js:29:22)
    at Object.base.NewExpression.base.CallExpression (/private/tmp/prova/node_modules/acorn/dist/walk.js:374:3)
    at c (/private/tmp/prova/node_modules/acorn/dist/walk.js:29:22)
    at Object.skipThrough (/private/tmp/prova/node_modules/acorn/dist/walk.js:183:37)
    at c (/private/tmp/prova/node_modules/acorn/dist/walk.js:29:22)
    at Object.base.ExpressionStatement.base.ParenthesizedExpression (/private/tmp/prova/node_modules/acorn/dist/walk.js:201:35)
    at c (/private/tmp/prova/node_modules/acorn/dist/walk.js:29:22)
    at Object.skipThrough (/private/tmp/prova/node_modules/acorn/dist/walk.js:183:37)
> 

Using the injector will yield equal results:

const acorn = require("acorn");
const walk = require("acorn/dist/walk");
const injectAcornStage3 = require("acorn-stage3/inject");

const localAcorn = injectAcornStage3(acorn);

const content = `
import("./module.js");
test("foo");
`;
const parsed = localAcorn.parse(content, { sourceType: 'module', plugins: { stage3: true } });
console.log(parsed);

walk.simple(parsed, {
    CallExpression(node) { console.log(node) }
});

Same result.

Am I doing something wrong?
The same kind of problem also happens with "acorn-acorn-object-rest-spread"
Thank you

@brickpop
Copy link
Author

Digging a bit inside of walk.simple, I find that baseVisitor[type] tries to evaluate baseVisitor["Import"].

baseVisitor contains the list of base tokens, and it looks like "Import" is not among its keys.

Could it be that the AST walker is using the undecorated instance of acorn?

@adrianheine
Copy link
Member

acorn-stage3 emits AST nodes that acorn-walk cannot handle.

@caub
Copy link

caub commented Aug 23, 2019

@ledfusion you might want to use http://npm.im/@babel/traverse then, this ast-walker can handle stage3 nodes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants