-
Notifications
You must be signed in to change notification settings - Fork 891
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
[acorn-walk]: baseVisitor[type] is not a function
when walking with a plugin
#829
Comments
Sure, a PR that helps with this would be great. |
Ok great. So the follow-up question is: what should be the behavior of the walkers (after the PR) around the now-unguarded function calls like https://github.com/acornjs/acorn/blob/master/acorn-walk/src/index.js#L23
I think the option 2) makes the most sense but maybe I'm wrong. The downside of option 1), when parsing code that requires multiple new node type definitions, would be that the user would have to fix them one-by-one, where with 2) they'd see all warnings at once. |
Definitely strict, I think—otherwise the code will just go ahead in a broken form. |
Another alternative (or perhaps, addition) would be to set a Symbol on the returned AST with an array of objects that each introduce extra base node type definitions that are ultimately registered via the plugins themselves. This would allow e.g. Pseudocode: // in acorn internals
const pluginBaseTypesSymbol = Symbol('acorn plugin base types');
// in acorn-jsx, right before returning AST
if (!ast[pluginBaseTypesSymbol]) {
ast[pluginBaseTypesSymbol] = []
}
ast[pluginBaseTypesSymbol].push({
JSXElement: () => {}
});
// in acorn-walk, before performing the walk
const baseVisitor = Object.assign({}, walk.base, ...ast[pluginBaseTypesSymbol], userSuppliedBase);
// in user code
const JSXParser = ...;
const ast = JSXParser.parse('...');
acornWalk.walk(ast, {...}); // OK, no need to specify base types |
Just wanted to chime in and say I just bumped up into this issue as well, and thankfully found this thread and the linked solution worked for me. 🙇 🙏 walk.simple(parsedJsx, {}, {
...walk.base,
JSXElement: () => {}
}) I think it would be be great to get this added to the docs somewhere, and maybe even added (or linked to / from) the acorn-jsx README as well? I know this is work in progress but if stale, I don't mind trying to submit the relevant PRs. 👍 |
Just had the same error because I forgot to parse code before passing it to a |
Hello,
I tried to walk a JSX file using
acorn-walk
andacorn-jsx
, with the following code:and this throws an exception:
The uncaught error is not very friendly, although the code is simple so I figured out this can be fixed by providing the
base
param, for example like this:I think perhaps this deserves a small code change (checking if
baseVisitor[type]
exists and logging a friendly message if not), and a small note in the docs.WDYT? (I'm willing to send a PR)
Jakub
The text was updated successfully, but these errors were encountered: