|
1 | | -import { type ecmaVersion, Parser, type Program } from 'acorn'; |
| 1 | +import { Parser, type Program } from 'acorn'; |
2 | 2 | import { build, type BuildOptions } from 'esbuild'; |
3 | 3 | import { isArray } from 'remeda'; |
4 | 4 |
|
5 | | -import type { |
6 | | - GeneratorMutatorParsingInfo, |
7 | | - Tsconfig, |
8 | | - TsConfigTarget, |
9 | | -} from '../types'; |
| 5 | +import type { GeneratorMutatorParsingInfo, Tsconfig } from '../types'; |
10 | 6 |
|
11 | 7 | export async function getMutatorInfo( |
12 | 8 | filePath: string, |
@@ -34,11 +30,7 @@ export async function getMutatorInfo( |
34 | 30 | tsconfig?.compilerOptions, |
35 | 31 | ); |
36 | 32 |
|
37 | | - return parseFile( |
38 | | - code, |
39 | | - namedExport, |
40 | | - getEcmaVersion(tsconfig?.compilerOptions?.target), |
41 | | - ); |
| 33 | + return parseFile(code, namedExport); |
42 | 34 | } |
43 | 35 |
|
44 | 36 | async function bundleFile( |
@@ -74,10 +66,17 @@ async function bundleFile( |
74 | 66 | function parseFile( |
75 | 67 | file: string, |
76 | 68 | name: string, |
77 | | - ecmaVersion: ecmaVersion = 6, |
78 | 69 | ): GeneratorMutatorParsingInfo | undefined { |
79 | 70 | try { |
80 | | - const ast = Parser.parse(file, { ecmaVersion, sourceType: 'module' }); |
| 71 | + // `file` is esbuild's bundled output, not the user's source. esbuild may |
| 72 | + // emit any modern syntax (notably dynamic `import()`, which it preserves |
| 73 | + // even when targeting es6 in ESM mode), so we parse with the latest |
| 74 | + // ecmaVersion to avoid spurious SyntaxErrors that would mask the export |
| 75 | + // we are looking for. See https://github.com/orval-labs/orval/issues/1634. |
| 76 | + const ast = Parser.parse(file, { |
| 77 | + ecmaVersion: 'latest', |
| 78 | + sourceType: 'module', |
| 79 | + }); |
81 | 80 |
|
82 | 81 | const foundSpecifier = ast.body |
83 | 82 | .filter((x) => x.type === 'ExportNamedDeclaration') |
@@ -201,19 +200,3 @@ function parseFunction( |
201 | 200 | } |
202 | 201 | } |
203 | 202 | } |
204 | | - |
205 | | -function getEcmaVersion(target?: TsConfigTarget): ecmaVersion | undefined { |
206 | | - if (!target) { |
207 | | - return; |
208 | | - } |
209 | | - |
210 | | - if (target.toLowerCase() === 'esnext') { |
211 | | - return 'latest'; |
212 | | - } |
213 | | - |
214 | | - try { |
215 | | - return Number(target.toLowerCase().replace('es', '')) as ecmaVersion; |
216 | | - } catch { |
217 | | - return; |
218 | | - } |
219 | | -} |
0 commit comments