acornjs / acorn

A small, fast, JavaScript-based JavaScript parser
10.58k stars 883 forks source link

Uncaught SyntaxError SyntaxError: Unexpected character '@' (25:0) #1311

Closed YSHIDM closed 2 months ago

YSHIDM commented 2 months ago

my code:

import { parse } from 'acorn'
async function genAST(filePath, sourceType = 'module') {
  console.log('filePath :>>', filePath)
  const jsString = await readFile(filePath)
  return JSON.stringify(parse(jsString.toString(), { ecmaVersion: 2020, sourceType }))
}

source code:

import * as bull from '@midwayjs/bull';
import * as bullBoard from '@midwayjs/bull-board';
……

error message:

Uncaught SyntaxError SyntaxError: Unexpected character '@' (25:0)
    at pp$4.raise (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:3580:13)
    at pp.getTokenFromCode (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:5561:8)
    at pp.readToken (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:5248:15)
    at pp.nextToken (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:5239:15)
    at pp.next (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:5200:8)
    at pp$5.parseLiteral (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:3006:8)
    at pp$5.parseExprAtom (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:2887:17)
    at pp$5.parseExprSubscripts (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:2722:19)
    at pp$5.parseMaybeUnary (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:2680:17)
    at pp$5.parseExprOps (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:2608:19)
    at pp$5.parseMaybeConditional (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:2591:19)
    at pp$5.parseMaybeAssign (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:2558:19)
    at pp$5.parseMaybeAssign (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:2576:23)
    at pp$5.parseExpression (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:2521:19)
    at pp$8.parseStatement (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:956:45)
    at pp$8.parseTopLevel (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:823:21)
    at parse (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:595:15)
    at parse (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:645:35)
    at parse (d:\project\code-flow\node_modules\.pnpm\acorn@8.12.1\node_modules\acorn\dist\acorn.mjs:6018:17)
YSHIDM commented 2 months ago

nodejs version: v22.4.0 acorn version: "^8.12.1"

RReverser commented 2 months ago

Error message complains about something on line 25, but you only showed the first 2 lines in your sample.

YSHIDM commented 2 months ago

source code is ts file

YSHIDM commented 1 month ago

Error message complains about something on line 25, but you only showed the first 2 lines in your sample.

})0VW{GNWP VZ2 G9QM9OC5

code:

@Configuration({ // line 25
  imports: [
    koa,
    bull,
    bullBoard,
    redis,
    socketio,
    // sequelize,
    cache,
    jwt,
    info,
    crossDomain,
    // rabbitmq,
    upload,
    orm,
    ws,
  ],
  importConfigs: [join(__dirname, './config')],
})
marijnh commented 1 month ago

Acorn does not parse decorators (they are not yet a stable part of the language).

YSHIDM commented 1 month ago

Acorn does not parse decorators (they are not yet a stable part of the language).

yes, it's typescript