@@ -5,7 +5,7 @@ import yn = require('yn')
5
5
import arrify = require( 'arrify' )
6
6
import { BaseError } from 'make-error'
7
7
import * as util from 'util'
8
- import * as ts from 'typescript'
8
+ import * as _ts from 'typescript'
9
9
10
10
/**
11
11
* @internal
@@ -26,6 +26,28 @@ const debugFn = shouldDebug ?
26
26
} :
27
27
< T , U > ( _ : string , fn : ( arg : T ) => U ) => fn
28
28
29
+ /**
30
+ * Common TypeScript interfaces between versions.
31
+ */
32
+ export interface TSCommon {
33
+ version : typeof _ts . version
34
+ sys : typeof _ts . sys
35
+ ScriptSnapshot : typeof _ts . ScriptSnapshot
36
+ displayPartsToString : typeof _ts . displayPartsToString
37
+ createLanguageService : typeof _ts . createLanguageService
38
+ getDefaultLibFilePath : typeof _ts . getDefaultLibFilePath
39
+ getPreEmitDiagnostics : typeof _ts . getPreEmitDiagnostics
40
+ flattenDiagnosticMessageText : typeof _ts . flattenDiagnosticMessageText
41
+ transpileModule : typeof _ts . transpileModule
42
+ ModuleKind : typeof _ts . ModuleKind
43
+ ScriptTarget : typeof _ts . ScriptTarget
44
+ findConfigFile : typeof _ts . findConfigFile
45
+ readConfigFile : typeof _ts . readConfigFile
46
+ parseJsonConfigFileContent : typeof _ts . parseJsonConfigFileContent
47
+ formatDiagnostics : typeof _ts . formatDiagnostics
48
+ formatDiagnosticsWithColorAndContext : typeof _ts . formatDiagnosticsWithColorAndContext
49
+ }
50
+
29
51
/**
30
52
* Export the current version.
31
53
*/
@@ -48,7 +70,7 @@ export interface Options {
48
70
ignoreDiagnostics ?: number | string | Array < number | string >
49
71
readFile ?: ( path : string ) => string | undefined
50
72
fileExists ?: ( path : string ) => boolean
51
- transformers ?: ts . CustomTransformers
73
+ transformers ?: _ts . CustomTransformers
52
74
}
53
75
54
76
/**
@@ -142,7 +164,7 @@ export class TSError extends BaseError {
142
164
export interface Register {
143
165
cwd : string
144
166
extensions : string [ ]
145
- ts : typeof ts
167
+ ts : TSCommon
146
168
compile ( code : string , fileName : string , lineOffset ?: number ) : string
147
169
getTypeInfo ( code : string , fileName : string , position : number ) : TypeInfo
148
170
}
@@ -182,15 +204,17 @@ export function register (opts: Options = {}): Register {
182
204
const cwd = process . cwd ( )
183
205
const { compilerOptions, project, skipProject } = options
184
206
const typeCheck = options . typeCheck === true || options . transpileOnly !== true
207
+ const compiler = require . resolve ( options . compiler || 'typescript' , { paths : [ cwd ] } )
208
+ const ts : typeof _ts = require ( compiler )
185
209
const transformers = options . transformers || undefined
186
210
const readFile = options . readFile || ts . sys . readFile
187
211
const fileExists = options . fileExists || ts . sys . fileExists
188
- const config = readConfig ( cwd , fileExists , readFile , compilerOptions , project , skipProject )
212
+ const config = readConfig ( cwd , ts , fileExists , readFile , compilerOptions , project , skipProject )
189
213
const configDiagnosticList = filterDiagnostics ( config . errors , ignoreDiagnostics )
190
214
const extensions = [ '.ts' , '.tsx' ]
191
215
const fileNames = options . files ? config . fileNames : [ ]
192
216
193
- const diagnosticHost : ts . FormatDiagnosticsHost = {
217
+ const diagnosticHost : _ts . FormatDiagnosticsHost = {
194
218
getNewLine : ( ) => EOL ,
195
219
getCurrentDirectory : ( ) => cwd ,
196
220
getCanonicalFileName : ( path ) => path
@@ -200,7 +224,7 @@ export function register (opts: Options = {}): Register {
200
224
? ts . formatDiagnosticsWithColorAndContext
201
225
: ts . formatDiagnostics
202
226
203
- function createTSError ( diagnostics : ReadonlyArray < ts . Diagnostic > ) {
227
+ function createTSError ( diagnostics : ReadonlyArray < _ts . Diagnostic > ) {
204
228
const diagnosticText = formatDiagnostics ( diagnostics , diagnosticHost )
205
229
const diagnosticCodes = diagnostics . map ( x => x . code )
206
230
return new TSError ( diagnosticText , diagnosticCodes )
@@ -398,7 +422,7 @@ function registerExtension (
398
422
/**
399
423
* Do post-processing on config options to support `ts-node`.
400
424
*/
401
- function fixConfig ( config : ts . ParsedCommandLine ) {
425
+ function fixConfig ( ts : TSCommon , config : _ts . ParsedCommandLine ) {
402
426
// Delete options that *should not* be passed through.
403
427
delete config . options . out
404
428
delete config . options . outFile
@@ -425,12 +449,13 @@ function fixConfig (config: ts.ParsedCommandLine) {
425
449
*/
426
450
function readConfig (
427
451
cwd : string ,
452
+ ts : TSCommon ,
428
453
fileExists : ( path : string ) => boolean ,
429
454
readFile : ( path : string ) => string | undefined ,
430
455
compilerOptions ?: object ,
431
456
project ?: string | null ,
432
457
noProject ?: boolean | null
433
- ) : ts . ParsedCommandLine {
458
+ ) : _ts . ParsedCommandLine {
434
459
let config : any = { compilerOptions : { } }
435
460
let basePath = normalizeSlashes ( cwd )
436
461
let configFileName : string | undefined = undefined
@@ -461,7 +486,7 @@ function readConfig (
461
486
// Override default configuration options `ts-node` requires.
462
487
config . compilerOptions = Object . assign ( { } , config . compilerOptions , compilerOptions , TS_NODE_COMPILER_OPTIONS )
463
488
464
- return fixConfig ( ts . parseJsonConfigFileContent ( config , ts . sys , basePath , undefined , configFileName ) )
489
+ return fixConfig ( ts , ts . parseJsonConfigFileContent ( config , ts . sys , basePath , undefined , configFileName ) )
465
490
}
466
491
467
492
/**
@@ -494,6 +519,6 @@ function updateSourceMap (sourceMapText: string, fileName: string) {
494
519
/**
495
520
* Filter diagnostics.
496
521
*/
497
- function filterDiagnostics ( diagnostics : ts . Diagnostic [ ] , ignore : number [ ] ) {
522
+ function filterDiagnostics ( diagnostics : _ts . Diagnostic [ ] , ignore : number [ ] ) {
498
523
return diagnostics . filter ( x => ignore . indexOf ( x . code ) === - 1 )
499
524
}
0 commit comments