@@ -71,7 +71,7 @@ export interface Options {
71
71
ignoreDiagnostics ?: Array < number | string >
72
72
readFile ?: ( path : string ) => string | undefined
73
73
fileExists ?: ( path : string ) => boolean
74
- transformers ?: _ts . CustomTransformers
74
+ transformers ?: _ts . CustomTransformers | ( ( p : _ts . Program ) => _ts . CustomTransformers )
75
75
}
76
76
77
77
/**
@@ -274,32 +274,23 @@ export function register (opts: Options = {}): Register {
274
274
/**
275
275
* Create the basic required function using transpile mode.
276
276
*/
277
- let getOutput = function ( code : string , fileName : string , lineOffset = 0 ) : SourceOutput {
278
- const result = ts . transpileModule ( code , {
279
- fileName,
280
- transformers,
281
- compilerOptions : config . options ,
282
- reportDiagnostics : true
283
- } )
284
-
285
- const diagnosticList = result . diagnostics ?
286
- filterDiagnostics ( result . diagnostics , ignoreDiagnostics ) :
287
- [ ]
288
-
289
- if ( diagnosticList . length ) reportTSError ( configDiagnosticList )
290
-
291
- return [ result . outputText , result . sourceMapText as string ]
292
- }
293
-
294
- let getTypeInfo = function ( _code : string , _fileName : string , _position : number ) : TypeInfo {
295
- throw new TypeError ( `Type information is unavailable without "--type-check"` )
296
- }
277
+ let getOutput : ( code : string , fileName : string , lineOffset : number ) => SourceOutput
278
+ let getTypeInfo : ( _code : string , _fileName : string , _position : number ) => TypeInfo
297
279
298
280
// Use full language services when the fast option is disabled.
299
281
if ( typeCheck ) {
300
282
const memoryCache = new MemoryCache ( config . fileNames )
301
283
const cachedReadFile = cachedLookup ( debugFn ( 'readFile' , readFile ) )
302
284
285
+ const getCustomTransformers = ( ) => {
286
+ if ( typeof transformers === 'function' ) {
287
+ const program = service . getProgram ( )
288
+ return program ? transformers ( program ) : undefined
289
+ }
290
+
291
+ return transformers
292
+ }
293
+
303
294
// Create the compiler host for type checking.
304
295
const serviceHost : _ts . LanguageServiceHost = {
305
296
getScriptFileNames : ( ) => Array . from ( memoryCache . fileVersions . keys ( ) ) ,
@@ -331,14 +322,14 @@ export function register (opts: Options = {}): Register {
331
322
getCurrentDirectory : ( ) => cwd ,
332
323
getCompilationSettings : ( ) => config . options ,
333
324
getDefaultLibFileName : ( ) => ts . getDefaultLibFilePath ( config . options ) ,
334
- getCustomTransformers : ( ) => transformers
325
+ getCustomTransformers : getCustomTransformers
335
326
}
336
327
337
328
const registry = ts . createDocumentRegistry ( ts . sys . useCaseSensitiveFileNames , cwd )
338
329
const service = ts . createLanguageService ( serviceHost , registry )
339
330
340
331
// Set the file contents into cache manually.
341
- const updateMemoryCache = function ( contents : string , fileName : string ) {
332
+ const updateMemoryCache = ( contents : string , fileName : string ) => {
342
333
const fileVersion = memoryCache . fileVersions . get ( fileName ) || 0
343
334
344
335
// Avoid incrementing cache when nothing has changed.
@@ -348,7 +339,7 @@ export function register (opts: Options = {}): Register {
348
339
memoryCache . fileContents . set ( fileName , contents )
349
340
}
350
341
351
- getOutput = function ( code : string , fileName : string , lineOffset : number = 0 ) {
342
+ getOutput = ( code : string , fileName : string ) => {
352
343
updateMemoryCache ( code , fileName )
353
344
354
345
const output = service . getEmitOutput ( fileName )
@@ -379,7 +370,7 @@ export function register (opts: Options = {}): Register {
379
370
return [ output . outputFiles [ 1 ] . text , output . outputFiles [ 0 ] . text ]
380
371
}
381
372
382
- getTypeInfo = function ( code : string , fileName : string , position : number ) {
373
+ getTypeInfo = ( code : string , fileName : string , position : number ) => {
383
374
updateMemoryCache ( code , fileName )
384
375
385
376
const info = service . getQuickInfoAtPosition ( fileName , position )
@@ -388,10 +379,35 @@ export function register (opts: Options = {}): Register {
388
379
389
380
return { name, comment }
390
381
}
382
+ } else {
383
+ if ( typeof transformers === 'function' ) {
384
+ throw new TypeError ( 'Transformers function is unavailable in "--transpile-only"' )
385
+ }
386
+
387
+ getOutput = ( code : string , fileName : string ) : SourceOutput => {
388
+ const result = ts . transpileModule ( code , {
389
+ fileName,
390
+ transformers,
391
+ compilerOptions : config . options ,
392
+ reportDiagnostics : true
393
+ } )
394
+
395
+ const diagnosticList = result . diagnostics ?
396
+ filterDiagnostics ( result . diagnostics , ignoreDiagnostics ) :
397
+ [ ]
398
+
399
+ if ( diagnosticList . length ) reportTSError ( configDiagnosticList )
400
+
401
+ return [ result . outputText , result . sourceMapText as string ]
402
+ }
403
+
404
+ getTypeInfo = ( ) => {
405
+ throw new TypeError ( 'Type information is unavailable in "--transpile-only"' )
406
+ }
391
407
}
392
408
393
409
// Create a simple TypeScript compiler proxy.
394
- function compile ( code : string , fileName : string , lineOffset ?: number ) {
410
+ function compile ( code : string , fileName : string , lineOffset = 0 ) {
395
411
const [ value , sourceMap ] = getOutput ( code , fileName , lineOffset )
396
412
const output = updateOutput ( value , fileName , sourceMap , getExtension )
397
413
outputCache . set ( fileName , output )
0 commit comments