@@ -11,7 +11,7 @@ import type { LanguageContext } from '../custom-language.ts'
1111import type { Generator , GeneratorResult } from './index.ts'
1212import type { SourceMapInput } from 'rolldown'
1313import type * as TsgoApiModule from 'typescript-next/async'
14- import type { API , Snapshot } from 'typescript-next/async'
14+ import type { API , Program , Snapshot } from 'typescript-next/async'
1515import type * as TsgoFsModule from 'typescript-next/fs'
1616import type { Diagnostic } from 'typescript-next/proto'
1717
@@ -29,6 +29,7 @@ export interface TsgoGeneratorOptions {
2929 cwd : string
3030 tsconfig : string
3131 vfs : boolean
32+ typecheck ?: boolean
3233 languageContext : LanguageContext
3334 loadSource ?: ( fileName : string ) => Promise < void >
3435}
@@ -45,6 +46,8 @@ export class TsgoGenerator implements Generator {
4546 private loadedSourceFiles = new Set < string > ( )
4647 private rootFileProjects = new Map < string , string > ( )
4748 private emitQueue : Promise < void > = Promise . resolve ( )
49+ // A new snapshot creates new programs, so changed code is checked again.
50+ private programErrors = new WeakMap < Program , Promise < string > > ( )
4851
4952 constructor ( options : TsgoGeneratorOptions ) {
5053 this . options = options
@@ -100,6 +103,8 @@ export class TsgoGenerator implements Generator {
100103 }
101104 }
102105
106+ if ( this . options . typecheck ) this . assertNoCheckDisabled ( )
107+
103108 // eslint-disable-next-line unicorn/prefer-early-return
104109 if ( this . options . languageContext . contentMapperExtensions . length ) {
105110 // Mapper setup and transform errors can otherwise produce empty declarations.
@@ -120,6 +125,20 @@ export class TsgoGenerator implements Generator {
120125 }
121126 }
122127
128+ private assertNoCheckDisabled ( ) : void {
129+ const configs = this . snapshot ! . getProjects ( )
130+ . filter (
131+ ( { configFileName, parsedCommandLine } ) =>
132+ configFileName && parsedCommandLine . options . noCheck ,
133+ )
134+ . map ( ( { configFileName } ) => configFileName )
135+ if ( configs . length ) {
136+ throw new Error (
137+ `The \`typecheck\` option conflicts with \`noCheck\` in ${ configs . join ( ', ' ) } . Remove \`noCheck\` from the tsconfig or disable \`typecheck\`.` ,
138+ )
139+ }
140+ }
141+
123142 addFile ( code : string , fileName : string ) : void {
124143 if ( ! this . options . vfs ) return
125144 this . pendingFiles . set ( this . toTsFilename ( fileName ) , code )
@@ -255,6 +274,31 @@ export class TsgoGenerator implements Generator {
255274 await this . replaceSnapshot ( nextSnapshot )
256275 }
257276
277+ private checkProgram ( program : Program ) : Promise < string > {
278+ let errors = this . programErrors . get ( program )
279+ if ( ! errors ) {
280+ debug ( 'type checking program %s' , program . id )
281+ errors = Promise . all ( [
282+ program . getConfigFileParsingDiagnostics ( ) ,
283+ program . getProgramDiagnostics ( ) ,
284+ program . getGlobalDiagnostics ( ) ,
285+ program . getSyntacticDiagnostics ( ) ,
286+ program . getSemanticDiagnostics ( ) ,
287+ program . getDeclarationDiagnostics ( ) ,
288+ ] ) . then ( ( diagnostics ) => {
289+ const errors = diagnostics
290+ . flat ( )
291+ . filter (
292+ ( { category } ) =>
293+ category === this . apiModule ! . DiagnosticCategory . Error ,
294+ )
295+ return errors . length ? this . formatDiagnostics ( errors ) : ''
296+ } )
297+ this . programErrors . set ( program , errors )
298+ }
299+ return errors
300+ }
301+
258302 private async emitFromSnapshot ( fileName : string ) : Promise < GeneratorResult > {
259303 if ( ! this . apiModule || ! this . api || ! this . snapshot ) {
260304 throw new Error ( 'TsgoGenerator has not been initialized.' )
@@ -307,6 +351,17 @@ export class TsgoGenerator implements Generator {
307351 }
308352 }
309353
354+ if ( this . options . typecheck ) {
355+ const error = await this . checkProgram ( project . program ) . catch (
356+ ( error : unknown ) => {
357+ throw new Error ( mapError ( error , tsFileName , fileName ) , {
358+ cause : error ,
359+ } )
360+ } ,
361+ )
362+ if ( error ) return { error : mapError ( error , tsFileName , fileName ) }
363+ }
364+
310365 const output = await project . program
311366 . getDeclarationEmit ( [ tsFileName ] )
312367 . catch ( ( error : unknown ) => {
0 commit comments