@@ -5,7 +5,7 @@ import yn = require('yn')
55import arrify = require( 'arrify' )
66import { BaseError } from 'make-error'
77import * as util from 'util'
8- import * as ts from 'typescript'
8+ import * as _ts from 'typescript'
99
1010/**
1111 * @internal
@@ -26,6 +26,28 @@ const debugFn = shouldDebug ?
2626 } :
2727 < T , U > ( _ : string , fn : ( arg : T ) => U ) => fn
2828
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+
2951/**
3052 * Export the current version.
3153 */
@@ -48,7 +70,7 @@ export interface Options {
4870 ignoreDiagnostics ?: number | string | Array < number | string >
4971 readFile ?: ( path : string ) => string | undefined
5072 fileExists ?: ( path : string ) => boolean
51- transformers ?: ts . CustomTransformers
73+ transformers ?: _ts . CustomTransformers
5274}
5375
5476/**
@@ -142,7 +164,7 @@ export class TSError extends BaseError {
142164export interface Register {
143165 cwd : string
144166 extensions : string [ ]
145- ts : typeof ts
167+ ts : TSCommon
146168 compile ( code : string , fileName : string , lineOffset ?: number ) : string
147169 getTypeInfo ( code : string , fileName : string , position : number ) : TypeInfo
148170}
@@ -182,15 +204,17 @@ export function register (opts: Options = {}): Register {
182204 const cwd = process . cwd ( )
183205 const { compilerOptions, project, skipProject } = options
184206 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 )
185209 const transformers = options . transformers || undefined
186210 const readFile = options . readFile || ts . sys . readFile
187211 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 )
189213 const configDiagnosticList = filterDiagnostics ( config . errors , ignoreDiagnostics )
190214 const extensions = [ '.ts' , '.tsx' ]
191215 const fileNames = options . files ? config . fileNames : [ ]
192216
193- const diagnosticHost : ts . FormatDiagnosticsHost = {
217+ const diagnosticHost : _ts . FormatDiagnosticsHost = {
194218 getNewLine : ( ) => EOL ,
195219 getCurrentDirectory : ( ) => cwd ,
196220 getCanonicalFileName : ( path ) => path
@@ -200,7 +224,7 @@ export function register (opts: Options = {}): Register {
200224 ? ts . formatDiagnosticsWithColorAndContext
201225 : ts . formatDiagnostics
202226
203- function createTSError ( diagnostics : ReadonlyArray < ts . Diagnostic > ) {
227+ function createTSError ( diagnostics : ReadonlyArray < _ts . Diagnostic > ) {
204228 const diagnosticText = formatDiagnostics ( diagnostics , diagnosticHost )
205229 const diagnosticCodes = diagnostics . map ( x => x . code )
206230 return new TSError ( diagnosticText , diagnosticCodes )
@@ -398,7 +422,7 @@ function registerExtension (
398422/**
399423 * Do post-processing on config options to support `ts-node`.
400424 */
401- function fixConfig ( config : ts . ParsedCommandLine ) {
425+ function fixConfig ( ts : TSCommon , config : _ts . ParsedCommandLine ) {
402426 // Delete options that *should not* be passed through.
403427 delete config . options . out
404428 delete config . options . outFile
@@ -425,12 +449,13 @@ function fixConfig (config: ts.ParsedCommandLine) {
425449 */
426450function readConfig (
427451 cwd : string ,
452+ ts : TSCommon ,
428453 fileExists : ( path : string ) => boolean ,
429454 readFile : ( path : string ) => string | undefined ,
430455 compilerOptions ?: object ,
431456 project ?: string | null ,
432457 noProject ?: boolean | null
433- ) : ts . ParsedCommandLine {
458+ ) : _ts . ParsedCommandLine {
434459 let config : any = { compilerOptions : { } }
435460 let basePath = normalizeSlashes ( cwd )
436461 let configFileName : string | undefined = undefined
@@ -461,7 +486,7 @@ function readConfig (
461486 // Override default configuration options `ts-node` requires.
462487 config . compilerOptions = Object . assign ( { } , config . compilerOptions , compilerOptions , TS_NODE_COMPILER_OPTIONS )
463488
464- return fixConfig ( ts . parseJsonConfigFileContent ( config , ts . sys , basePath , undefined , configFileName ) )
489+ return fixConfig ( ts , ts . parseJsonConfigFileContent ( config , ts . sys , basePath , undefined , configFileName ) )
465490}
466491
467492/**
@@ -494,6 +519,6 @@ function updateSourceMap (sourceMapText: string, fileName: string) {
494519/**
495520 * Filter diagnostics.
496521 */
497- function filterDiagnostics ( diagnostics : ts . Diagnostic [ ] , ignore : number [ ] ) {
522+ function filterDiagnostics ( diagnostics : _ts . Diagnostic [ ] , ignore : number [ ] ) {
498523 return diagnostics . filter ( x => ignore . indexOf ( x . code ) === - 1 )
499524}
0 commit comments