Skip to content

Commit c98b177

Browse files
committed
fix: disable JSX parsing for TypeScript files
1 parent ab6a653 commit c98b177

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

packages/react-docgen/src/__tests__/babel8-test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,20 @@ test('parses TypeScript function props with Babel 8', () => {
3636
},
3737
});
3838
});
39+
40+
test('parses generic arrow functions in TypeScript files with Babel 8', () => {
41+
const result = parse(
42+
`import React from 'react';
43+
44+
export const mockDomain = <Entity>(
45+
entities: Record<string, Entity> = {},
46+
) => ({ entities });
47+
48+
export function Button() {
49+
return React.createElement('button');
50+
}`,
51+
{ filename: 'store.ts' },
52+
);
53+
54+
expect(result).toHaveLength(1);
55+
});

packages/react-docgen/src/babelParser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ const IS_BABEL_8 = Number.parseInt(version, 10) >= 8;
1616
function getDefaultPlugins(
1717
options: TransformOptions,
1818
): NonNullable<ParserOptions['plugins']> {
19+
const extension = options.filename ? extname(options.filename) : '';
20+
const isTypeScript = TYPESCRIPT_EXTS.has(extension);
1921
const plugins: NonNullable<ParserOptions['plugins']> = [
20-
'jsx',
21-
options.filename && TYPESCRIPT_EXTS.has(extname(options.filename))
22-
? 'typescript'
23-
: 'flow',
22+
...(isTypeScript && extension !== '.tsx' ? [] : ['jsx' as const]),
23+
isTypeScript ? 'typescript' : 'flow',
2424
'asyncDoExpressions',
2525
];
2626

0 commit comments

Comments
 (0)