Skip to content

Commit af993b6

Browse files
authored
fix: unify typechecking and ast collection (#10449)
1 parent b1ab2b9 commit af993b6

5 files changed

Lines changed: 69 additions & 300 deletions

File tree

‎packages/vitest/src/node/ast-collect.ts‎

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface ParsedSuite extends Suite {
3434
dynamic: boolean
3535
}
3636

37-
interface LocalCallDefinition {
37+
export interface LocalCallDefinition {
3838
start: number
3939
end: number
4040
name: string
@@ -46,6 +46,24 @@ interface LocalCallDefinition {
4646
tags: string[]
4747
}
4848

49+
export interface FileInformation {
50+
file: File
51+
filepath: string
52+
parsed: string
53+
map: any
54+
definitions: LocalCallDefinition[]
55+
}
56+
57+
export interface AstCollectOptions {
58+
/**
59+
* Override the pool stored on the resulting File task. Required when
60+
* collecting typecheck files because the project's `config.pool` is the
61+
* user's runtime pool (e.g. `forks`), not the `typescript` pool that the
62+
* typecheck spec uses to compute its task id.
63+
*/
64+
pool?: string
65+
}
66+
4967
const debug = createDebugger('vitest:ast-collect-info')
5068
const verbose = createDebugger('vitest:ast-collect-verbose')
5169

@@ -287,15 +305,16 @@ function astParseFile(filepath: string, code: string) {
287305
}
288306
}
289307

290-
export function createFailedFileTask(project: TestProject, filepath: string, error: Error): File {
308+
export function createFailedFileTask(project: TestProject, filepath: string, error: Error, options?: AstCollectOptions): File {
291309
const config = project.serializedConfig
310+
const pool = options?.pool ?? config.pool
292311
const baseFile = createFileTaskOriginal(
293312
filepath,
294313
config.root,
295314
config.name,
296-
config.pool,
315+
pool,
297316
undefined,
298-
{ typecheck: config.pool === 'typescript', __vitest_label__: config.mergeReportsLabel },
317+
{ typecheck: pool === 'typescript', __vitest_label__: config.mergeReportsLabel },
299318
)
300319
const file: ParsedFile = {
301320
...baseFile,
@@ -340,16 +359,18 @@ function createFileTask(
340359
requestMap: any,
341360
filepath: string,
342361
fileTags: string[] | undefined,
362+
options?: AstCollectOptions,
343363
) {
344364
const { definitions, ast } = astParseFile(testFilepath, code)
345365
const config = project.serializedConfig
366+
const pool = options?.pool ?? config.pool
346367
const baseFile = createFileTaskOriginal(
347368
filepath,
348369
config.root,
349370
config.name,
350-
config.pool,
371+
pool,
351372
undefined,
352-
{ typecheck: config.pool === 'typescript', __vitest_label__: config.mergeReportsLabel },
373+
{ typecheck: pool === 'typescript', __vitest_label__: config.mergeReportsLabel },
353374
)
354375
const file: ParsedFile = {
355376
...baseFile,
@@ -484,31 +505,55 @@ function createFileTask(
484505
],
485506
}
486507
}
487-
return file
508+
return { file, definitions }
488509
}
489510

490511
export async function astCollectTests(
491512
project: TestProject,
492513
filepath: string,
493514
): Promise<File> {
515+
const information = await astCollectFileInformation(project, filepath)
516+
return information.file
517+
}
518+
519+
export async function astCollectFileInformation(
520+
project: TestProject,
521+
filepath: string,
522+
options?: AstCollectOptions,
523+
): Promise<FileInformation> {
494524
const request = await transformSSR(project, filepath)
495525
const testFilepath = relative(project.config.root, filepath)
496526
if (!request) {
497527
debug?.('Cannot parse', testFilepath, '(vite didn\'t return anything)')
498-
return createFailedFileTask(
499-
project,
528+
return {
529+
file: createFailedFileTask(
530+
project,
531+
filepath,
532+
new Error(`Failed to parse ${testFilepath}. Vite didn't return anything.`),
533+
options,
534+
),
500535
filepath,
501-
new Error(`Failed to parse ${testFilepath}. Vite didn't return anything.`),
502-
)
536+
parsed: '',
537+
map: null,
538+
definitions: [],
539+
}
503540
}
504-
return createFileTask(
541+
const { file, definitions } = createFileTask(
505542
project,
506543
testFilepath,
507544
request.code,
508545
request.map,
509546
filepath,
510547
request.fileTags,
548+
options,
511549
)
550+
return {
551+
file,
552+
filepath,
553+
parsed: request.code,
554+
map: request.map,
555+
definitions,
556+
}
512557
}
513558

514559
async function transformSSR(project: TestProject, filepath: string) {

0 commit comments

Comments
 (0)