acornjs / acorn-stage3

Support for stage 3 proposals in acorn

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to walk AST's parsed with stage3

brickpop opened this issue · comments

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

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?

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

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