TyrealHu / acorn-typescript

Alternative, TypeScript parser
https://www.npmjs.com/package/acorn-typescript?activeTab=readme
MIT License
145 stars 18 forks source link

SyntaxError when parsing React.ComponentProps<typeof RRLink> #46

Closed yadomi closed 1 year ago

yadomi commented 1 year ago

To reproduce:

const acorn = require('acorn');
const { tsPlugin } = require('acorn-typescript');

const parser = acorn.Parser.extend(tsPlugin());

const source = `
import * as React from 'react'
import { Link as RRLink } from 'react-router-dom';

const Link = (props: React.ComponentProps<typeof RRLink>) => null
`;

const ast = parser.parse(source, {
    sourceType: 'module',
    locations: true,
    ecmaVersion: 'latest',
}); // ERROR

This throw this error:

/Users/yadomi/workspace/node_modules/acorn/dist/acorn.js:3562
    throw err
    ^

SyntaxError: Unexpected token (5:56)
    at pp$4.raise (/Users/yadomi/workspace/node_modules/acorn/dist/acorn.js:3560:15)
    at p.raiseCommonCheck (/Users/yadomi/workspace/node_modules/acorn-typescript/lib/index.js:1:101091)
    at p.raise (/Users/yadomi/workspace/node_modules/acorn-typescript/lib/index.js:1:101245)
    at pp$9.unexpected (/Users/yadomi/workspace/node_modules/acorn/dist/acorn.js:768:10)
    at p.tsParseNonArrayType (/Users/yadomi/workspace/node_modules/acorn-typescript/lib/index.js:1:48305)
    at p.tsParseArrayTypeOrHigher (/Users/yadomi/workspace/node_modules/acorn-typescript/lib/index.js:1:48372)
    at /Users/yadomi/workspace/node_modules/acorn-typescript/lib/index.js:1:48964
    at p.tsInAllowConditionalTypesContext (/Users/yadomi/workspace/node_modules/acorn-typescript/lib/index.js:1:38740)
    at p.tsParseTypeOperatorOrHigher (/Users/yadomi/workspace/node_modules/acorn-typescript/lib/index.js:1:48911)
    at p.tsParseUnionOrIntersectionType (/Users/yadomi/workspace/node_modules/acorn-typescript/lib/index.js:1:41989) {
  pos: 140,
  loc: Position { line: 5, column: 56 },
  raisedAt: 141
}

Update:

However, this works:

type RRLinkType = typeof RRLink
const Link = (props: React.ComponentProps<RRLinkType>) => null
TyrealHu commented 1 year ago

try it in version 1.4.9. I rewrite the options to be true by default. In previous versions, you should enable jsx options like

const Parser = acorn.Parser.extend(tsPlugin({
  jsx: {
    allowNamespaces: true
  }
}))
yadomi commented 1 year ago

thanks for the reply,

however I have the same issue with acorn-typescript@1.4.9 (with or without jsx.allowNamespaces to true) with the snippet above.

I'm using acorn@8.10.0 (latest)

TyrealHu commented 1 year ago

Yeah, my bad. This has been fixed in acorn-typescript@1.4.10. Its a bug of left relational and right relational. Also I have added your case in UT.