acornjs / acorn-jsx

Alternative, faster React.js JSX parser
MIT License
648 stars 72 forks source link

Cannot read properties of undefined (reading 'allowHashBang') #134

Closed kewp closed 1 year ago

kewp commented 1 year ago

Not sure what I'm doing wrong.

I just put the first example into a file:

var acorn = require("acorn");
var jsx = require("acorn-jsx");
acorn.Parser.extend(jsx()).parse("my(<jsx/>, 'code');");

then when i run it

$ node test.js

i get the error

Since Acorn 8.0.0, options.ecmaVersion is required.
Defaulting to 2020, but this will stop working in the future.
/Users/karl/auto-layout/node_modules/.pnpm/acorn@8.8.0/node_modules/acorn/dist/acorn.js:444
    if (opts.allowHashBang == null)
             ^

TypeError: Cannot read properties of undefined (reading 'allowHashBang')
    at getOptions (/Users/karl/auto-layout/node_modules/.pnpm/acorn@8.8.0/node_modules/acorn/dist/acorn.js:444:14)
    at new Parser (/Users/karl/auto-layout/node_modules/.pnpm/acorn@8.8.0/node_modules/acorn/dist/acorn.js:500:30)
    at new <anonymous> (/Users/karl/auto-layout/node_modules/.pnpm/acorn-jsx@5.3.2_acorn@8.8.0/node_modules/acorn-jsx/index.js:106:10)
    at Function.parse (/Users/karl/auto-layout/node_modules/.pnpm/acorn@8.8.0/node_modules/acorn/dist/acorn.js:639:12)
    at Object.<anonymous> (/Users/karl/auto-layout/test.js:3:28)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)

do i have to put in options?

using node 16.15.0

donfour commented 1 year ago

Doc might be out of date, adding an empty options object as the 2nd argument fixed the issue for me:

acorn.Parser.extend(jsx()).parse("my(<jsx/>, 'code');", {});
RReverser commented 1 year ago

Sounds like a bug upstream in acorn itself (considering that the error is coming from there).