Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: re-run typescript without jsx on babel parsing error #849

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 44 additions & 31 deletions src/parser/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,51 @@ import { getContent } from '../utils/file';

export default async function parseTypescript(filename) {
const content = await getContent(filename);
const plugins = [
'typescript',
'jsx',
'asyncGenerators',
'bigInt',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
// ['decorators', { decoratorsBeforeExport: true }],
'decorators-legacy',
'doExpressions',
'dynamicImport',
'exportDefaultFrom',
'exportNamespaceFrom',
'functionBind',
'functionSent',
'importMeta',
'logicalAssignment',
'nullishCoalescingOperator',
'numericSeparator',
'objectRestSpread',
'optionalCatchBinding',
'optionalChaining',
['pipelineOperator', { proposal: 'minimal' }],
'throwExpressions',
'importAssertions',
'explicitResourceManagement',
];

// Enable all known compatible @babel/parser plugins at the time of writing.
// Because we only parse them, not evaluate any code, it is safe to do so.
// note that babel/parser 7+ does not support *, due to plugin incompatibilities
return parse(content, {
sourceType: 'module',
plugins: [
'typescript',
'jsx',
'asyncGenerators',
'bigInt',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
// ['decorators', { decoratorsBeforeExport: true }],
'decorators-legacy',
'doExpressions',
'dynamicImport',
'exportDefaultFrom',
'exportNamespaceFrom',
'functionBind',
'functionSent',
'importMeta',
'logicalAssignment',
'nullishCoalescingOperator',
'numericSeparator',
'objectRestSpread',
'optionalCatchBinding',
'optionalChaining',
['pipelineOperator', { proposal: 'minimal' }],
'throwExpressions',
'importAssertions',
'explicitResourceManagement',
],
});
try {
return await parse(content, {
sourceType: 'module',
plugins,
});
} catch (err) {
if (err.code === 'BABEL_PARSER_SYNTAX_ERROR') {
// re-run it without jsx, as this causes sometimes parsing errors
return parse(content, {
sourceType: 'module',
plugins: plugins.filter((plugin) => plugin !== 'jsx'),
});
}
throw err;
}
}