@@ -38,25 +38,25 @@ namespace ts {
38
38
39
39
function trace ( message : DiagnosticMessage , ...args : any [ ] ) : void ;
40
40
function trace ( message : DiagnosticMessage ) : void {
41
- // this will be bound to the trace method on the host
42
- this . trace ( createCompilerDiagnostic . apply ( undefined , arguments ) ) ;
41
+ // ' this' will be bound to the trace method on the host
42
+ this . trace ( ( < Diagnostic > createCompilerDiagnostic . apply ( undefined , arguments ) ) . messageText ) ;
43
43
}
44
44
45
45
function noTrace ( message : DiagnosticMessage , ...args : any [ ] ) : void ;
46
46
function noTrace ( message : DiagnosticMessage ) : void {
47
47
}
48
48
49
- type Trace = typeof trace ;
49
+ type ModuleResolutionTracer = typeof trace ;
50
50
51
- function getTrace ( compilerOptions : CompilerOptions , host : ModuleResolutionHost ) : Trace {
51
+ function getTrace ( compilerOptions : CompilerOptions , host : ModuleResolutionHost ) : ModuleResolutionTracer {
52
52
return compilerOptions . traceModuleResolution && host . trace ? trace . bind ( host ) : noTrace ;
53
53
}
54
54
55
55
export function resolveModuleName ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost ) : ResolvedModuleWithFailedLookupLocations {
56
56
const traceWorker = getTrace ( compilerOptions , host ) ;
57
-
57
+
58
58
traceWorker ( Diagnostics . Resolving_module_0_from_1 , moduleName , containingFile ) ;
59
-
59
+
60
60
let moduleResolution = compilerOptions . moduleResolution ;
61
61
if ( moduleResolution === undefined ) {
62
62
if ( compilerOptions . module === ModuleKind . CommonJS ) {
@@ -76,13 +76,13 @@ namespace ts {
76
76
77
77
let result : ResolvedModuleWithFailedLookupLocations ;
78
78
switch ( moduleResolution ) {
79
- case ModuleResolutionKind . NodeJs :
80
- result = nodeModuleNameResolver ( moduleName , containingFile , host ) ;
79
+ case ModuleResolutionKind . NodeJs :
80
+ result = nodeModuleNameResolver ( moduleName , containingFile , compilerOptions , host ) ;
81
81
break ;
82
- case ModuleResolutionKind . Classic :
82
+ case ModuleResolutionKind . Classic :
83
83
result = classicNameResolver ( moduleName , containingFile , compilerOptions , host ) ;
84
84
break ;
85
- case ModuleResolutionKind . BaseUrl :
85
+ case ModuleResolutionKind . BaseUrl :
86
86
result = baseUrlModuleNameResolver ( moduleName , containingFile , compilerOptions , host ) ;
87
87
break ;
88
88
}
@@ -177,8 +177,10 @@ namespace ts {
177
177
const traceWorker = getTrace ( options , host ) ;
178
178
179
179
if ( isRootedDiskPath ( moduleName ) ) {
180
- let failedLookupLocations : string [ ] = [ ] ;
181
- const resolvedFileName = loadModuleFromFile ( supportedExtensions , moduleName , failedLookupLocations , host ) ;
180
+ traceWorker ( Diagnostics . Resolving_rooted_module_name_0_use_it_as_a_candidate_location , moduleName ) ;
181
+
182
+ const failedLookupLocations : string [ ] = [ ] ;
183
+ const resolvedFileName = loadModuleFromFile ( supportedExtensions , moduleName , failedLookupLocations , host , traceWorker ) ;
182
184
return {
183
185
resolvedModule : resolvedFileName ? { resolvedFileName } : undefined ,
184
186
failedLookupLocations
@@ -187,21 +189,27 @@ namespace ts {
187
189
188
190
if ( nameStartsWithDotSlashOrDotDotSlash ( moduleName ) ) {
189
191
// relative name
192
+ traceWorker ( Diagnostics . Resolving_relative_module_name_0 , moduleName ) ;
190
193
return baseUrlResolveRelativeModuleName ( moduleName , containingFile , baseUrl , options , host , traceWorker ) ;
191
194
}
192
195
else {
193
196
// non-relative name
194
- return baseUrlResolveNonRelativeModuleName ( moduleName , baseUrl , options , host ) ;
197
+ traceWorker ( Diagnostics . Resolving_non_relative_module_name_0 , moduleName ) ;
198
+ return baseUrlResolveNonRelativeModuleName ( moduleName , baseUrl , options , host , traceWorker ) ;
195
199
}
196
200
}
197
201
198
- function baseUrlResolveRelativeModuleName ( moduleName : string , containingFile : string , baseUrl : string , options : CompilerOptions , host : ModuleResolutionHost , traceWorker : Trace ) : ResolvedModuleWithFailedLookupLocations {
202
+ function baseUrlResolveRelativeModuleName ( moduleName : string , containingFile : string , baseUrl : string , options : CompilerOptions , host : ModuleResolutionHost , traceWorker : ModuleResolutionTracer ) : ResolvedModuleWithFailedLookupLocations {
199
203
const failedLookupLocations : string [ ] = [ ] ;
200
204
201
205
const containingDirectory = getDirectoryPath ( containingFile ) ;
202
206
const candidate = normalizePath ( combinePaths ( containingDirectory , moduleName ) ) ;
203
207
208
+ traceWorker ( Diagnostics . Converting_relative_module_name_0_to_absolute_using_1_as_a_base_directory_2 , moduleName , containingDirectory , candidate ) ;
209
+
204
210
if ( options . rootDirs ) {
211
+ traceWorker ( Diagnostics . rootDirs_option_is_specified_searching_for_a_longest_matching_prefix ) ;
212
+
205
213
let matchedPrefix : string ;
206
214
for ( const rootDir of options . rootDirs ) {
207
215
let normalizedRoot = getNormalizedAbsolutePath ( rootDir , baseUrl ) ;
@@ -214,26 +222,32 @@ namespace ts {
214
222
}
215
223
if ( matchedPrefix ) {
216
224
const suffix = candidate . substr ( matchedPrefix . length ) ;
217
- return baseUrlResolveNonRelativeModuleName ( suffix , baseUrl , options , host ) ;
225
+ traceWorker ( Diagnostics . Found_longest_matching_rootDir_0_converted_relative_path_1_to_non_relative_path_2 , matchedPrefix , moduleName , suffix ) ;
226
+
227
+ return baseUrlResolveNonRelativeModuleName ( suffix , baseUrl , options , host , traceWorker ) ;
218
228
}
219
229
return { resolvedModule : undefined , failedLookupLocations } ;
220
230
}
221
231
else {
222
- const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host ) ;
232
+ traceWorker ( Diagnostics . rootDirs_option_is_not_specified_using_0_as_candidate_location , candidate ) ;
233
+
234
+ const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host , traceWorker ) ;
223
235
return {
224
236
resolvedModule : resolvedFileName ? { resolvedFileName } : undefined ,
225
237
failedLookupLocations
226
238
} ;
227
239
}
228
240
}
229
241
230
- function baseUrlResolveNonRelativeModuleName ( moduleName : string , baseUrl : string , options : CompilerOptions , host : ModuleResolutionHost ) : ResolvedModuleWithFailedLookupLocations {
242
+ function baseUrlResolveNonRelativeModuleName ( moduleName : string , baseUrl : string , options : CompilerOptions , host : ModuleResolutionHost , traceWorker : ModuleResolutionTracer ) : ResolvedModuleWithFailedLookupLocations {
231
243
let longestMatchPrefixLength = - 1 ;
232
244
let matchedPattern : string ;
233
245
let matchedStar : string ;
234
246
235
247
const failedLookupLocations : string [ ] = [ ] ;
236
248
if ( options . paths ) {
249
+ traceWorker ( Diagnostics . paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0 , moduleName ) ;
250
+
237
251
for ( const key in options . paths ) {
238
252
const pattern : string = key ;
239
253
const indexOfStar = pattern . indexOf ( "*" ) ;
@@ -262,10 +276,15 @@ namespace ts {
262
276
}
263
277
264
278
if ( matchedPattern ) {
279
+ traceWorker ( Diagnostics . Module_name_0_matched_pattern_1 , moduleName , matchedPattern ) ;
280
+
265
281
for ( const subst of options . paths [ matchedPattern ] ) {
266
282
const path = matchedStar ? subst . replace ( "\*" , matchedStar ) : subst ;
267
283
const candidate = normalizePath ( combinePaths ( baseUrl , path ) ) ;
268
- const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host ) ;
284
+
285
+ traceWorker ( Diagnostics . Trying_substitution_0_candidate_module_location_Colon_1 , subst , path ) ;
286
+
287
+ const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host , traceWorker ) ;
269
288
if ( resolvedFileName ) {
270
289
return { resolvedModule : { resolvedFileName } , failedLookupLocations } ;
271
290
}
@@ -275,7 +294,10 @@ namespace ts {
275
294
}
276
295
else {
277
296
const candidate = normalizePath ( combinePaths ( baseUrl , moduleName ) ) ;
278
- const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host ) ;
297
+
298
+ traceWorker ( Diagnostics . Resolving_module_name_0_relative_to_base_url_1_2 , moduleName , baseUrl , candidate ) ;
299
+
300
+ const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host , traceWorker ) ;
279
301
return {
280
302
resolvedModule : resolvedFileName ? { resolvedFileName } : undefined ,
281
303
failedLookupLocations
@@ -292,46 +314,57 @@ namespace ts {
292
314
return str . indexOf ( suffix , expectedPos ) === expectedPos ;
293
315
}
294
316
295
- export function nodeModuleNameResolver ( moduleName : string , containingFile : string , host : ModuleResolutionHost ) : ResolvedModuleWithFailedLookupLocations {
317
+ export function nodeModuleNameResolver ( moduleName : string , containingFile : string , options : CompilerOptions , host : ModuleResolutionHost ) : ResolvedModuleWithFailedLookupLocations {
296
318
const containingDirectory = getDirectoryPath ( containingFile ) ;
297
319
320
+ const traceWorker = getTrace ( options , host ) ;
321
+
298
322
if ( isRootedDiskPath ( moduleName ) || nameStartsWithDotSlashOrDotDotSlash ( moduleName ) ) {
299
323
const failedLookupLocations : string [ ] = [ ] ;
300
324
const candidate = normalizePath ( combinePaths ( containingDirectory , moduleName ) ) ;
301
- let resolvedFileName = loadModuleFromFile ( supportedJsExtensions , candidate , failedLookupLocations , host ) ;
325
+
326
+ traceWorker ( Diagnostics . Loading_module_0_as_file_Slash_folder_candidate_module_location_1 , moduleName , candidate ) ;
327
+
328
+ let resolvedFileName = loadModuleFromFile ( supportedJsExtensions , candidate , failedLookupLocations , host , traceWorker ) ;
302
329
303
330
if ( resolvedFileName ) {
304
331
return { resolvedModule : { resolvedFileName } , failedLookupLocations } ;
305
332
}
306
333
307
- resolvedFileName = loadNodeModuleFromDirectory ( supportedJsExtensions , candidate , failedLookupLocations , host ) ;
334
+ resolvedFileName = loadNodeModuleFromDirectory ( supportedJsExtensions , candidate , failedLookupLocations , host , traceWorker ) ;
308
335
return resolvedFileName
309
336
? { resolvedModule : { resolvedFileName } , failedLookupLocations }
310
337
: { resolvedModule : undefined , failedLookupLocations } ;
311
338
}
312
339
else {
313
- return loadModuleFromNodeModules ( moduleName , containingDirectory , host ) ;
340
+ traceWorker ( Diagnostics . Loading_module_0_from_node_modules_folder , moduleName ) ;
341
+
342
+ return loadModuleFromNodeModules ( moduleName , containingDirectory , host , traceWorker ) ;
314
343
}
315
344
}
316
345
317
- function loadModuleFromFile ( extensions : string [ ] , candidate : string , failedLookupLocation : string [ ] , host : ModuleResolutionHost ) : string {
346
+ function loadModuleFromFile ( extensions : string [ ] , candidate : string , failedLookupLocation : string [ ] , host : ModuleResolutionHost , traceWorker : ModuleResolutionTracer ) : string {
318
347
return forEach ( extensions , tryLoad ) ;
319
348
320
349
function tryLoad ( ext : string ) : string {
321
350
const fileName = fileExtensionIs ( candidate , ext ) ? candidate : candidate + ext ;
351
+
322
352
if ( host . fileExists ( fileName ) ) {
353
+ traceWorker ( Diagnostics . File_0_exist_use_it_as_a_module_resolution_result , fileName ) ;
323
354
return fileName ;
324
355
}
325
356
else {
357
+ traceWorker ( Diagnostics . File_0_does_not_exist , fileName ) ;
326
358
failedLookupLocation . push ( fileName ) ;
327
359
return undefined ;
328
360
}
329
361
}
330
362
}
331
363
332
- function loadNodeModuleFromDirectory ( extensions : string [ ] , candidate : string , failedLookupLocation : string [ ] , host : ModuleResolutionHost ) : string {
364
+ function loadNodeModuleFromDirectory ( extensions : string [ ] , candidate : string , failedLookupLocation : string [ ] , host : ModuleResolutionHost , traceWorker : ModuleResolutionTracer ) : string {
333
365
const packageJsonPath = combinePaths ( candidate , "package.json" ) ;
334
366
if ( host . fileExists ( packageJsonPath ) ) {
367
+ traceWorker ( Diagnostics . Found_package_json_at_0 , packageJsonPath ) ;
335
368
336
369
let jsonContent : { typings ?: string } ;
337
370
@@ -345,34 +378,40 @@ namespace ts {
345
378
}
346
379
347
380
if ( jsonContent . typings ) {
348
- const result = loadModuleFromFile ( extensions , normalizePath ( combinePaths ( candidate , jsonContent . typings ) ) , failedLookupLocation , host ) ;
381
+ const typingsFile = normalizePath ( combinePaths ( candidate , jsonContent . typings ) ) ;
382
+ traceWorker ( Diagnostics . package_json_has_typings_field_0_that_references_1 , jsonContent . typings , typingsFile ) ;
383
+ const result = loadModuleFromFile ( extensions , typingsFile , failedLookupLocation , host , traceWorker ) ;
349
384
if ( result ) {
350
385
return result ;
351
386
}
352
387
}
388
+ else {
389
+ traceWorker ( Diagnostics . package_json_does_not_have_typings_field ) ;
390
+ }
353
391
}
354
392
else {
393
+ traceWorker ( Diagnostics . package_json_does_not_have_typings_field ) ;
355
394
// record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results
356
395
failedLookupLocation . push ( packageJsonPath ) ;
357
396
}
358
397
359
- return loadModuleFromFile ( extensions , combinePaths ( candidate , "index" ) , failedLookupLocation , host ) ;
398
+ return loadModuleFromFile ( extensions , combinePaths ( candidate , "index" ) , failedLookupLocation , host , traceWorker ) ;
360
399
}
361
400
362
- function loadModuleFromNodeModules ( moduleName : string , directory : string , host : ModuleResolutionHost ) : ResolvedModuleWithFailedLookupLocations {
401
+ function loadModuleFromNodeModules ( moduleName : string , directory : string , host : ModuleResolutionHost , traceWorker : ModuleResolutionTracer ) : ResolvedModuleWithFailedLookupLocations {
363
402
const failedLookupLocations : string [ ] = [ ] ;
364
403
directory = normalizeSlashes ( directory ) ;
365
404
while ( true ) {
366
405
const baseName = getBaseFileName ( directory ) ;
367
406
if ( baseName !== "node_modules" ) {
368
407
const nodeModulesFolder = combinePaths ( directory , "node_modules" ) ;
369
408
const candidate = normalizePath ( combinePaths ( nodeModulesFolder , moduleName ) ) ;
370
- let result = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host ) ;
409
+ let result = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host , traceWorker ) ;
371
410
if ( result ) {
372
411
return { resolvedModule : { resolvedFileName : result , isExternalLibraryImport : true } , failedLookupLocations } ;
373
412
}
374
413
375
- result = loadNodeModuleFromDirectory ( supportedExtensions , candidate , failedLookupLocations , host ) ;
414
+ result = loadNodeModuleFromDirectory ( supportedExtensions , candidate , failedLookupLocations , host , traceWorker ) ;
376
415
if ( result ) {
377
416
return { resolvedModule : { resolvedFileName : result , isExternalLibraryImport : true } , failedLookupLocations } ;
378
417
}
@@ -412,8 +451,11 @@ namespace ts {
412
451
413
452
export function classicNameResolver ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost ) : ResolvedModuleWithFailedLookupLocations {
414
453
454
+ const traceWorker = getTrace ( compilerOptions , host ) ;
455
+
415
456
// module names that contain '!' are used to reference resources and are not resolved to actual files on disk
416
457
if ( moduleName . indexOf ( "!" ) != - 1 ) {
458
+ traceWorker ( Diagnostics . Module_name_0_contains_character , moduleName ) ;
417
459
return { resolvedModule : undefined , failedLookupLocations : [ ] } ;
418
460
}
419
461
@@ -435,9 +477,11 @@ namespace ts {
435
477
436
478
const candidate = searchName + extension ;
437
479
if ( host . fileExists ( candidate ) ) {
480
+ traceWorker ( Diagnostics . File_0_exist_use_it_as_a_module_resolution_result , candidate ) ;
438
481
return candidate ;
439
482
}
440
483
else {
484
+ traceWorker ( Diagnostics . File_0_does_not_exist , candidate ) ;
441
485
failedLookupLocations . push ( candidate ) ;
442
486
}
443
487
} ) ;
0 commit comments