benjamn / recast

JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator
MIT License
4.91k stars 347 forks source link

TSX support #1382

Closed sergeyzwezdin closed 4 months ago

sergeyzwezdin commented 7 months ago

Trying to parse React (tsx) code, but recast fails with error.

import * as recast from 'recast';

recast.parse(`import {FC} from 'react';

export const Button: FC = () => {
    return <div>button</div>;
}`, {
    parser: require('recast/parsers/typescript')
})

Unterminated regular expression. (4:24) SyntaxError: Unterminated regular expression. (4:24) at constructor (/node_modules/@babel/parser/src/parse-error.ts:81:19) at V8IntrinsicMixin.toParseError [as raise]

Does recast support React/TSX code parsing?

eventualbuddha commented 4 months ago

Try the babel-ts parser instead?

import * as recast from "recast";
import * as ts from "recast/parsers/babel-ts";

console.log(
  recast.parse(
    `import {FC} from 'react';

export const Button: FC = () => {
    return <div>button</div>;
}`,
    {
      parser: ts,
    },
  ),
);