Skip to content

[acorn-walk]: baseVisitor[type] is not a function when walking with a pluginΒ #829

Open
@jakub-g

Description

@jakub-g

Hello,

I tried to walk a JSX file using acorn-walk and acorn-jsx, with the following code:

let acorn = require("acorn")
let walk = require("acorn-walk")
let acornJsx = require("acorn-jsx")

let acornJsxParser = acorn.Parser.extend(acornJsx())
let parsedJsx = acornJsxParser.parse('const foo = () => ( <div></div> )', {sourceType: 'module'})

walk.simple(parsedJsx, {
  ImportDeclaration(node) {
    console.log('ImportDeclaration')
  }
})

and this throws an exception:

C:\git\acorn-walk-test\node_modules\acorn-walk\dist\walk.js:29
    baseVisitor[type](node, st, c);
                     ^

TypeError: baseVisitor[type] is not a function
    at c (C:\git\acorn-walk-test\node_modules\acorn-walk\dist\walk.js:29:22)

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:

walk.simple(parsedJsx, {
  ImportDeclaration(node) {
    console.log('ImportDeclaration')
  }
}, {
  ...walk.base,
  JSXElement: () => {}
})

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

    "acorn": "^6.1.1",
    "acorn-jsx": "^5.0.1",
    "acorn-walk": "^6.1.1"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions