|
1 | 1 | import { generate } from '@babel/generator' |
2 | 2 | import { isIdentifierName } from '@babel/helper-validator-identifier' |
3 | | -import { parse } from '@babel/parser' |
| 3 | +import { parse, type ParseResult } from '@babel/parser' |
4 | 4 | import * as t from '@babel/types' |
5 | 5 | import { |
6 | 6 | isDeclarationType, |
@@ -142,12 +142,21 @@ export function createFakeJsPlugin({ |
142 | 142 | code: string, |
143 | 143 | id: string, |
144 | 144 | ): TransformResult { |
145 | | - const file = parse(code, { |
146 | | - plugins: [['typescript', { dts: true }], 'decoratorAutoAccessors'], |
147 | | - sourceType: 'module', |
148 | | - errorRecovery: true, |
149 | | - createParenthesizedExpressions: true, |
150 | | - }) |
| 145 | + let file: ParseResult |
| 146 | + try { |
| 147 | + file = parse(code, { |
| 148 | + plugins: [['typescript', { dts: true }], 'decoratorAutoAccessors'], |
| 149 | + sourceType: 'module', |
| 150 | + errorRecovery: true, |
| 151 | + createParenthesizedExpressions: true, |
| 152 | + }) |
| 153 | + } catch (error) { |
| 154 | + throw new Error( |
| 155 | + `Failed to parse ${id}. This may be caused by a syntax error in the declaration file or a bug in the plugin. Please report this issue to https://github.com/sxzz/rolldown-plugin-dts\n${error}`, |
| 156 | + { cause: error }, |
| 157 | + ) |
| 158 | + } |
| 159 | + |
151 | 160 | const { program, comments } = file |
152 | 161 | const typeOnlyIds: string[] = [] |
153 | 162 | const identifierMap: Record<string, number> = Object.create(null) |
@@ -369,11 +378,17 @@ export function createFakeJsPlugin({ |
369 | 378 | if (ids) typeOnlyIds.push(...ids) |
370 | 379 | } |
371 | 380 |
|
372 | | - const file = parse(code, { |
373 | | - sourceType: 'module', |
374 | | - }) |
375 | | - const { program } = file |
| 381 | + let file: ParseResult |
| 382 | + try { |
| 383 | + file = parse(code, { sourceType: 'module' }) |
| 384 | + } catch (error) { |
| 385 | + throw new Error( |
| 386 | + `Failed to parse generated code for chunk ${chunk.fileName}. This may be caused by a bug in the plugin. Please report this issue to https://github.com/sxzz/rolldown-plugin-dts\n${error}`, |
| 387 | + { cause: error }, |
| 388 | + ) |
| 389 | + } |
376 | 390 |
|
| 391 | + const { program } = file |
377 | 392 | program.body = patchTsNamespace(program.body) |
378 | 393 | program.body = patchReExport(program.body) |
379 | 394 |
|
|
0 commit comments