1+ import { execFileSync } from "node:child_process" ;
12import { realpathSync } from "node:fs" ;
23import { resolve } from "node:path" ;
3- import ts from "typescript" ;
44import { expect , test } from "vitest" ;
55
66// A consumer compiling with `skipLibCheck: false` type-checks this package's
@@ -14,60 +14,78 @@ import { expect, test } from "vitest";
1414// `exports` as a consumer would, and require the declarations to check clean.
1515// Real path: the compiler reports resolved files by it.
1616const packageDir = realpathSync ( resolve ( import . meta. dirname , ".." ) ) ;
17- const typesDir = resolve ( packageDir , "types" ) ;
1817const ENTRIES : Record < string , string > = {
1918 "solid-js" : "index.d.ts" ,
2019 "solid-js/internal" : "internal.d.ts" ,
2120 "solid-js/refresh" : "refresh/index.d.ts" ,
2221 "solid-js/attribution" : "attribution.d.ts"
2322} ;
2423
25- test ( "the published declarations type-check under skipLibCheck: false" , ( ) => {
26- const probe = resolve ( packageDir , "test/__declarations-probe__.ts" ) ;
27- const source = Object . keys ( ENTRIES )
28- . map ( ( specifier , i ) => `import * as e${ i } from "${ specifier } ";\nexport { e${ i } };` )
29- . join ( "\n" ) ;
30- const options : ts . CompilerOptions = {
31- strict : true ,
32- noEmit : true ,
33- skipLibCheck : false ,
34- module : ts . ModuleKind . ESNext ,
35- moduleResolution : ts . ModuleResolutionKind . Bundler ,
36- target : ts . ScriptTarget . ES2022 ,
37- lib : [ "lib.es2022.d.ts" , "lib.dom.d.ts" ] ,
38- types : [ ]
39- } ;
40- const host = ts . createCompilerHost ( options ) ;
41- const { getSourceFile, fileExists, readFile } = host ;
42- host . fileExists = file => file === probe || fileExists . call ( host , file ) ;
43- host . readFile = file => ( file === probe ? source : readFile . call ( host , file ) ) ;
44- host . getSourceFile = ( file , languageVersion , ...rest ) =>
45- file === probe
46- ? ts . createSourceFile ( file , source , languageVersion )
47- : getSourceFile . call ( host , file , languageVersion , ...rest ) ;
48- const program = ts . createProgram ( [ probe ] , options , host ) ;
24+ // The compiler runs in a child Node: V8 coverage instruments every script in
25+ // the worker's isolate (`coverage.include` only filters the report), which
26+ // multiplies the checker's cost several-fold under `vitest --coverage`.
27+ const CHECK = `
28+ import { resolve } from "node:path";
29+ import ts from "typescript";
4930
50- // Every entry must resolve to this package's generated declarations, or
51- // the check below would pass over files it never saw.
52- const resolved = Object . values ( ENTRIES ) . filter ( file =>
53- program . getSourceFile ( resolve ( typesDir , file ) )
54- ) ;
55- expect ( resolved ) . toEqual ( Object . values ( ENTRIES ) ) ;
31+ const packageDir = ${ JSON . stringify ( packageDir ) } ;
32+ const typesDir = resolve(packageDir, "types");
33+ const entries = ${ JSON . stringify ( ENTRIES ) } ;
34+ const probe = resolve(packageDir, "test/__declarations-probe__.ts");
35+ const source = Object.keys(entries)
36+ .map((specifier, i) => \`import * as e\${i} from "\${specifier}";\\nexport { e\${i} };\`)
37+ .join("\\n");
38+ const options = {
39+ strict: true,
40+ noEmit: true,
41+ skipLibCheck: false,
42+ module: ts.ModuleKind.ESNext,
43+ moduleResolution: ts.ModuleResolutionKind.Bundler,
44+ target: ts.ScriptTarget.ES2022,
45+ lib: ["lib.es2022.d.ts", "lib.dom.d.ts"],
46+ types: []
47+ };
48+ const host = ts.createCompilerHost(options);
49+ const { getSourceFile, fileExists, readFile } = host;
50+ host.fileExists = file => file === probe || fileExists.call(host, file);
51+ host.readFile = file => (file === probe ? source : readFile.call(host, file));
52+ host.getSourceFile = (file, languageVersion, ...rest) =>
53+ file === probe
54+ ? ts.createSourceFile(file, source, languageVersion)
55+ : getSourceFile.call(host, file, languageVersion, ...rest);
56+ const program = ts.createProgram([probe], options, host);
5657
57- const ours = program
58- . getSourceFiles ( )
59- . filter ( sf => sf . fileName === probe || resolve ( sf . fileName ) . startsWith ( typesDir + "/" ) ) ;
60- const diagnostics = ours . flatMap ( sf => [
61- ...program . getSyntacticDiagnostics ( sf ) ,
62- ...program . getSemanticDiagnostics ( sf )
63- ] ) ;
64- const report = diagnostics . map ( d => {
58+ const resolved = Object.values(entries).filter(file =>
59+ program.getSourceFile(resolve(typesDir, file))
60+ );
61+ const ours = program
62+ .getSourceFiles()
63+ .filter(sf => sf.fileName === probe || resolve(sf.fileName).startsWith(typesDir + "/"));
64+ const report = ours
65+ .flatMap(sf => [...program.getSyntacticDiagnostics(sf), ...program.getSemanticDiagnostics(sf)])
66+ .map(d => {
6567 const where = d.file
66- ? ` ${ d . file . fileName . slice ( packageDir . length + 1 ) } :${
67- d . file . getLineAndCharacterOfPosition ( d . start ! ) . line + 1
68- } `
68+ ? \`\ ${d.file.fileName.slice(packageDir.length + 1)}:\ ${
69+ d.file.getLineAndCharacterOfPosition(d.start).line + 1
70+ }\ `
6971 : "(global)";
70- return ` ${ where } TS${ d . code } : ${ ts . flattenDiagnosticMessageText ( d . messageText , " " ) } `;
72+ return \`\ ${where} TS\ ${d.code}: \ ${ts.flattenDiagnosticMessageText(d.messageText, " ")}\ `;
7173 });
74+ process.stdout.write(JSON.stringify({ resolved, report }));
75+ ` ;
76+
77+ // The check is a few hundred milliseconds alone but several seconds when
78+ // turbo runs every package's suite at once, past vitest's 5 s default.
79+ test ( "the published declarations type-check under skipLibCheck: false" , ( ) => {
80+ const { resolved, report } = JSON . parse (
81+ execFileSync ( process . execPath , [ "--input-type=module" , "-e" , CHECK ] , {
82+ cwd : packageDir ,
83+ encoding : "utf8"
84+ } )
85+ ) ;
86+
87+ // Every entry must resolve to this package's generated declarations, or
88+ // the check would pass over files it never saw.
89+ expect ( resolved ) . toEqual ( Object . values ( ENTRIES ) ) ;
7290 expect ( report ) . toEqual ( [ ] ) ;
73- } ) ;
91+ } , 30000 ) ;
0 commit comments