forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoduleResolution.ts
1132 lines (1049 loc) · 54.7 KB
/
moduleResolution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as Harness from "../_namespaces/Harness.js";
import * as ts from "../_namespaces/ts.js";
import { jsonToReadableText } from "./helpers.js";
interface File {
name: string;
content?: string;
symlinks?: string[];
}
function createModuleResolutionHost(baselines: string[], hasDirectoryExists: boolean, ...files: File[]): ts.ModuleResolutionHost {
const map = new Map<string, File>();
for (const file of files) {
map.set(file.name, file);
baselines.push(`//// [${file.name}]\n${file.content || ""}`, "");
if (file.symlinks) {
for (const symlink of file.symlinks) {
map.set(symlink, file);
baselines.push(`//// [${symlink}] symlink(${file.name})`, "");
}
}
}
if (hasDirectoryExists) {
const directories = new Map<string, string>();
for (const f of files) {
let name = ts.getDirectoryPath(f.name);
while (true) {
directories.set(name, name);
const baseName = ts.getDirectoryPath(name);
if (baseName === name) {
break;
}
name = baseName;
}
}
return {
readFile,
realpath,
directoryExists: path => directories.has(path),
fileExists: path => {
assert.isTrue(directories.has(ts.getDirectoryPath(path)), `'fileExists' '${path}' request in non-existing directory`);
return map.has(path);
},
useCaseSensitiveFileNames: true,
};
}
else {
return { readFile, realpath, fileExists: path => map.has(path), useCaseSensitiveFileNames: true };
}
function readFile(path: string): string | undefined {
const file = map.get(path);
return file && file.content;
}
function realpath(path: string): string {
return map.get(path)!.name;
}
}
function runBaseline(scenario: string, baselines: readonly string[]) {
Harness.Baseline.runBaseline(`moduleResolution/${scenario.split(" ").join("-")}.js`, baselines.join("\n"));
}
describe("unittests:: moduleResolution:: Node module resolution - relative paths", () => {
// node module resolution does _not_ implicitly append these extensions to an extensionless path (though will still attempt to load them if explicitly)
const nonImplicitExtensions = [ts.Extension.Mts, ts.Extension.Dmts, ts.Extension.Mjs, ts.Extension.Cts, ts.Extension.Dcts, ts.Extension.Cjs];
const autoExtensions = ts.filter(ts.supportedTSExtensionsFlat, e => !nonImplicitExtensions.includes(e));
it("load as file", () => {
const baselines: string[] = [];
testLoadAsFile("load as file with relative name in current directory", "/foo/bar/baz.ts", "/foo/bar/foo", "./foo");
testLoadAsFile("load as file with relative name in parent directory", "/foo/bar/baz.ts", "/foo/foo", "../foo");
testLoadAsFile("load as file with name starting with directory seperator", "/foo/bar/baz.ts", "/foo", "/foo");
testLoadAsFile("load as file with name starting with window root", "c:/foo/bar/baz.ts", "c:/foo", "c:/foo");
runBaseline("relative module name as file", baselines);
function testLoadAsFile(scenario: string, containingFileName: string, moduleFileNameNoExt: string, moduleName: string): void {
baselines.push(scenario);
for (const ext of autoExtensions) {
test(ext, /*hasDirectoryExists*/ true);
test(ext, /*hasDirectoryExists*/ false);
}
baselines.push("");
function test(ext: string, hasDirectoryExists: boolean) {
const containingFile = { name: containingFileName };
const moduleFile = { name: moduleFileNameNoExt + ext };
baselines.push(`Resolving "${moduleName}" from ${containingFile.name} when module has extension: ${ext}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const resolution = ts.nodeModuleNameResolver(moduleName, containingFile.name, {}, createModuleResolutionHost(baselines, hasDirectoryExists, containingFile, moduleFile));
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
}
}
});
it("module name as directory - load from 'typings'", () => {
const baselines: string[] = [];
testLoadingFromPackageJson("/a/b/c/d.ts", "/a/b/c/bar/package.json", "c/d/e.d.ts", "/a/b/c/bar/c/d/e.d.ts", "./bar");
testLoadingFromPackageJson("/a/b/c/d.ts", "/a/bar/package.json", "e.d.ts", "/a/bar/e.d.ts", "../../bar");
testLoadingFromPackageJson("/a/b/c/d.ts", "/bar/package.json", "e.d.ts", "/bar/e.d.ts", "/bar");
testLoadingFromPackageJson("c:/a/b/c/d.ts", "c:/bar/package.json", "e.d.ts", "c:/bar/e.d.ts", "c:/bar");
runBaseline("relative module name as directory", baselines);
function testLoadingFromPackageJson(containingFileName: string, packageJsonFileName: string, fieldRef: string, moduleFileName: string, moduleName: string): void {
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
function test(hasDirectoryExists: boolean) {
const containingFile = { name: containingFileName };
const packageJson = { name: packageJsonFileName, content: jsonToReadableText({ typings: fieldRef }) };
const moduleFile = { name: moduleFileName };
baselines.push(`Resolving "${moduleName}" from ${containingFile.name} with typings: ${fieldRef}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const resolution = ts.nodeModuleNameResolver(moduleName, containingFile.name, {}, createModuleResolutionHost(baselines, hasDirectoryExists, containingFile, packageJson, moduleFile));
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
}
}
});
it("module name as directory - handle invalid 'typings'", () => {
const baselines: string[] = [];
testTypingsIgnored(["a", "b"]);
testTypingsIgnored({ a: "b" });
testTypingsIgnored(/*typings*/ true);
testTypingsIgnored(/*typings*/ null); // eslint-disable-line no-restricted-syntax
testTypingsIgnored(/*typings*/ undefined);
runBaseline("relative module name as directory with invalid typings", baselines);
function testTypingsIgnored(typings: any): void {
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
function test(hasDirectoryExists: boolean) {
const containingFile = { name: "/a/b.ts" };
const packageJson = { name: "/node_modules/b/package.json", content: jsonToReadableText({ typings }) };
const moduleFile = { name: "/a/b.d.ts" };
const indexPath = "/node_modules/b/index.d.ts";
const indexFile = { name: indexPath };
baselines.push(`Resolving "b" from ${containingFile.name} with typings: ${jsonToReadableText(typings)}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const resolution = ts.nodeModuleNameResolver("b", containingFile.name, {}, createModuleResolutionHost(baselines, hasDirectoryExists, containingFile, packageJson, moduleFile, indexFile));
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
}
}
});
it("module name as directory - load index.d.ts", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("relative module name as directory load index", baselines);
function test(hasDirectoryExists: boolean) {
const containingFile = { name: "/a/b/c.ts" };
const packageJson = { name: "/a/b/foo/package.json", content: jsonToReadableText({ main: "/c/d" }) };
const indexFile = { name: "/a/b/foo/index.d.ts" };
baselines.push(`Resolving "./foo" from ${containingFile.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const resolution = ts.nodeModuleNameResolver("./foo", containingFile.name, {}, createModuleResolutionHost(baselines, hasDirectoryExists, containingFile, packageJson, indexFile));
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
}
});
});
describe("unittests:: moduleResolution:: Node module resolution - non-relative paths", () => {
it("computes correct commonPrefix for moduleName cache", () => {
const resolutionCache = ts.createModuleResolutionCache("/", f => f);
let cache = resolutionCache.getOrCreateCacheForNonRelativeName("a", /*mode*/ undefined);
cache.set("/sub", {
resolvedModule: {
originalPath: undefined,
resolvedFileName: "/sub/node_modules/a/index.ts",
isExternalLibraryImport: true,
extension: ts.Extension.Ts,
},
failedLookupLocations: [],
affectingLocations: [],
resolutionDiagnostics: [],
});
assert.isDefined(cache.get("/sub"));
assert.isUndefined(cache.get("/"));
cache = resolutionCache.getOrCreateCacheForNonRelativeName("b", /*mode*/ undefined);
cache.set("/sub/dir/foo", {
resolvedModule: {
originalPath: undefined,
resolvedFileName: "/sub/directory/node_modules/b/index.ts",
isExternalLibraryImport: true,
extension: ts.Extension.Ts,
},
failedLookupLocations: [],
affectingLocations: [],
resolutionDiagnostics: [],
});
assert.isDefined(cache.get("/sub/dir/foo"));
assert.isDefined(cache.get("/sub/dir"));
assert.isDefined(cache.get("/sub"));
assert.isUndefined(cache.get("/"));
cache = resolutionCache.getOrCreateCacheForNonRelativeName("c", /*mode*/ undefined);
cache.set("/foo/bar", {
resolvedModule: {
originalPath: undefined,
resolvedFileName: "/bar/node_modules/c/index.ts",
isExternalLibraryImport: true,
extension: ts.Extension.Ts,
},
failedLookupLocations: [],
affectingLocations: [],
resolutionDiagnostics: [],
});
assert.isDefined(cache.get("/foo/bar"));
assert.isDefined(cache.get("/foo"));
assert.isDefined(cache.get("/"));
cache = resolutionCache.getOrCreateCacheForNonRelativeName("d", /*mode*/ undefined);
cache.set("/foo", {
resolvedModule: {
originalPath: undefined,
resolvedFileName: "/foo/index.ts",
isExternalLibraryImport: true,
extension: ts.Extension.Ts,
},
failedLookupLocations: [],
affectingLocations: [],
resolutionDiagnostics: [],
});
assert.isDefined(cache.get("/foo"));
assert.isUndefined(cache.get("/"));
cache = resolutionCache.getOrCreateCacheForNonRelativeName("e", /*mode*/ undefined);
cache.set("c:/foo", {
resolvedModule: {
originalPath: undefined,
resolvedFileName: "d:/bar/node_modules/e/index.ts",
isExternalLibraryImport: true,
extension: ts.Extension.Ts,
},
failedLookupLocations: [],
affectingLocations: [],
resolutionDiagnostics: [],
});
assert.isDefined(cache.get("c:/foo"));
assert.isDefined(cache.get("c:/"));
assert.isUndefined(cache.get("d:/"));
cache = resolutionCache.getOrCreateCacheForNonRelativeName("f", /*mode*/ undefined);
cache.set("/foo/bar/baz", {
resolvedModule: undefined,
failedLookupLocations: [],
affectingLocations: [],
resolutionDiagnostics: [],
});
assert.isDefined(cache.get("/foo/bar/baz"));
assert.isDefined(cache.get("/foo/bar"));
assert.isDefined(cache.get("/foo"));
assert.isDefined(cache.get("/"));
});
it("load module as file - ts files not loaded", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("non relative module name as file ts files not loaded", baselines);
function test(hasDirectoryExists: boolean) {
const containingFile = { name: "/a/b/c/d/e.ts" };
const moduleFile = { name: "/a/b/node_modules/foo.ts" };
baselines.push(`Resolving "foo" from ${containingFile.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const resolution = ts.nodeModuleNameResolver("foo", containingFile.name, {}, createModuleResolutionHost(baselines, hasDirectoryExists, containingFile, moduleFile));
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
}
});
it("load module as file", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("non relative module name as file", baselines);
function test(hasDirectoryExists: boolean) {
const containingFile = { name: "/a/b/c/d/e.ts" };
const moduleFile = { name: "/a/b/node_modules/foo.d.ts" };
baselines.push(`Resolving "foo" from ${containingFile.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const resolution = ts.nodeModuleNameResolver("foo", containingFile.name, {}, createModuleResolutionHost(baselines, hasDirectoryExists, containingFile, moduleFile));
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
}
});
it("load module as directory", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("non relative module name as directory", baselines);
function test(hasDirectoryExists: boolean) {
const containingFile: File = { name: "/a/node_modules/b/c/node_modules/d/e.ts" };
const moduleFile: File = { name: "/a/node_modules/foo/index.d.ts" };
baselines.push(`Resolving "foo" from ${containingFile.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const resolution = ts.nodeModuleNameResolver("foo", containingFile.name, {}, createModuleResolutionHost(baselines, hasDirectoryExists, containingFile, moduleFile));
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
}
});
it("preserveSymlinks", () => {
const baselines: string[] = [];
testPreserveSymlinks(/*preserveSymlinks*/ false);
testPreserveSymlinks(/*preserveSymlinks*/ true);
runBaseline("non relative preserveSymlinks", baselines);
function testPreserveSymlinks(preserveSymlinks: boolean) {
const realFileName = "/linked/index.d.ts";
const symlinkFileName = "/app/node_modules/linked/index.d.ts";
const host = createModuleResolutionHost(
baselines,
/*hasDirectoryExists*/ true,
{ name: realFileName, symlinks: [symlinkFileName] },
{ name: "/app/node_modules/linked/package.json", content: jsonToReadableText({ version: "0.0.0", main: "./index" }) },
);
baselines.push(`Resolving "linked" from /app/app.ts when preserveSymlinks is ${preserveSymlinks}`);
const resolution = ts.nodeModuleNameResolver("linked", "/app/app.ts", { preserveSymlinks }, host);
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
}
});
it("uses originalPath for caching", () => {
const baselines: string[] = [];
const host = createModuleResolutionHost(
baselines,
/*hasDirectoryExists*/ true,
{
name: "/modules/a.ts",
symlinks: ["/sub/node_modules/a/index.ts"],
},
{
name: "/sub/node_modules/a/package.json",
content: jsonToReadableText({ version: "0.0.0", main: "./index" }),
},
);
const compilerOptions: ts.CompilerOptions = { moduleResolution: ts.ModuleResolutionKind.Node10 };
const cache = ts.createModuleResolutionCache("/", f => f);
baselines.push(`Resolving "a" from /sub/dir/foo.ts`);
let resolution = ts.resolveModuleName("a", "/sub/dir/foo.ts", compilerOptions, host, cache);
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
baselines.push(`Resolving "a" from /sub/foo.ts`);
resolution = ts.resolveModuleName("a", "/sub/foo.ts", compilerOptions, host, cache);
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
baselines.push(`Resolving "a" from /foo.ts`);
resolution = ts.resolveModuleName("a", "/foo.ts", compilerOptions, host, cache);
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
runBaseline("non relative uses originalPath for caching", baselines);
});
it("preserves originalPath on cache hit", () => {
const baselines: string[] = [];
const host = createModuleResolutionHost(
baselines,
/*hasDirectoryExists*/ true,
{ name: "/linked/index.d.ts", symlinks: ["/app/node_modules/linked/index.d.ts"] },
{ name: "/app/node_modules/linked/package.json", content: jsonToReadableText({ version: "0.0.0", main: "./index" }) },
);
const cache = ts.createModuleResolutionCache("/", f => f);
const compilerOptions: ts.CompilerOptions = { moduleResolution: ts.ModuleResolutionKind.Node10 };
baselineResolution("/app/src/app.ts");
baselineResolution("/app/lib/main.ts");
runBaseline("non relative preserves originalPath on cache hit", baselines);
function baselineResolution(containingFile: string) {
baselines.push(`Resolving "linked" from ${containingFile}`);
const resolution = ts.resolveModuleName("linked", containingFile, compilerOptions, host, cache);
baselines.push(`Resolution:: ${jsonToReadableText(resolution)}`);
baselines.push("");
}
});
});
describe("unittests:: moduleResolution:: Relative imports", () => {
function test(scenario: string, filesMapLike: ts.MapLike<string>, currentDirectory: string, rootFiles: string[], relativeNamesToCheck: string[]) {
it(scenario, () => {
const files = new Map(Object.entries(filesMapLike));
const baselines: string[] = [];
files.forEach((content, file) => baselines.push(`//// [${file}]\n${content}`, ""));
const options: ts.CompilerOptions = { module: ts.ModuleKind.CommonJS };
const host: ts.CompilerHost = {
getSourceFile: (fileName: string, languageVersion: ts.ScriptTarget) => {
const path = ts.normalizePath(ts.combinePaths(currentDirectory, fileName));
const file = files.get(path);
return file ? ts.createSourceFile(fileName, file, languageVersion) : undefined;
},
getDefaultLibFileName: () => "lib.d.ts",
writeFile: ts.notImplemented,
getCurrentDirectory: () => currentDirectory,
getDirectories: () => [],
getCanonicalFileName: fileName => fileName.toLowerCase(),
getNewLine: () => "\r\n",
useCaseSensitiveFileNames: () => false,
fileExists: fileName => {
const path = ts.normalizePath(ts.combinePaths(currentDirectory, fileName));
return files.has(path);
},
readFile: ts.notImplemented,
};
const program = ts.createProgram(rootFiles, options, host);
baselines.push("Program files::");
program.getSourceFiles().forEach(file => baselines.push(file.fileName));
baselines.push("", "Syntactic Diagnostics::");
baselines.push(ts.formatDiagnostics(program.getSyntacticDiagnostics(), host), "");
baselines.push("Semantic Diagnostics::");
baselines.push(ts.formatDiagnostics(program.getSemanticDiagnostics(), host), "");
// try to get file using a relative name
for (const relativeFileName of relativeNamesToCheck) {
baselines.push(`getSourceFile by ${relativeFileName}: ${program.getSourceFile(relativeFileName)?.fileName}`);
}
runBaseline(scenario, baselines);
});
}
test(
"should file all modules",
{
"/a/b/c/first/shared.ts": `
class A {}
export = A`,
"/a/b/c/first/second/class_a.ts": `
import Shared = require('../shared');
import C = require('../../third/class_c');
class B {}
export = B;`,
"/a/b/c/third/class_c.ts": `
import Shared = require('../first/shared');
class C {}
export = C;
`,
},
"/a/b/c/first/second",
["class_a.ts"],
["../../../c/third/class_c.ts"],
);
test(
"should find modules in node_modules",
{
"/parent/node_modules/mod/index.d.ts": "export var x",
"/parent/app/myapp.ts": `import {x} from "mod"`,
},
"/parent/app",
["myapp.ts"],
[],
);
test(
"should find file referenced via absolute and relative names",
{
"/a/b/c.ts": `/// <reference path="b.ts"/>`,
"/a/b/b.ts": "var x",
},
"/a/b",
["c.ts", "/a/b/b.ts"],
[],
);
});
describe("unittests:: moduleResolution:: Files with different casing with forceConsistentCasingInFileNames", () => {
let library: ts.SourceFile;
function test(
scenario: string,
filesMapLike: ts.MapLike<string>,
options: ts.CompilerOptions,
currentDirectory: string,
useCaseSensitiveFileNames: boolean,
rootFiles: string[],
): void {
it(scenario, () => {
const getCanonicalFileName = ts.createGetCanonicalFileName(useCaseSensitiveFileNames);
let files = new Map(Object.entries(filesMapLike));
if (!useCaseSensitiveFileNames) {
const oldFiles = files;
files = new Map<string, string>();
oldFiles.forEach((file, fileName) => {
files.set(getCanonicalFileName(fileName), file);
});
}
const baselines: string[] = [];
files.forEach((content, file) => baselines.push(`//// [${file}]\n${content}`, ""));
const host: ts.CompilerHost = {
getSourceFile: (fileName: string, languageVersion: ts.ScriptTarget) => {
if (fileName === "lib.d.ts") {
if (!library) {
library = ts.createSourceFile("lib.d.ts", "", ts.ScriptTarget.ES5);
}
return library;
}
const path = getCanonicalFileName(ts.normalizePath(ts.combinePaths(currentDirectory, fileName)));
const file = files.get(path);
return file ? ts.createSourceFile(fileName, file, languageVersion) : undefined;
},
getDefaultLibFileName: () => "lib.d.ts",
writeFile: ts.notImplemented,
getCurrentDirectory: () => currentDirectory,
getDirectories: () => [],
getCanonicalFileName,
getNewLine: () => "\r\n",
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
fileExists: fileName => {
const path = getCanonicalFileName(ts.normalizePath(ts.combinePaths(currentDirectory, fileName)));
return files.has(path);
},
readFile: ts.notImplemented,
};
const program = ts.createProgram(rootFiles, options, host);
const diagnostics = ts.sortAndDeduplicateDiagnostics([...program.getSemanticDiagnostics(), ...program.getOptionsDiagnostics()]);
baselines.push("Diagnostics::");
baselines.push(ts.formatDiagnostics(diagnostics, host), "");
runBaseline(scenario, baselines);
});
}
test(
"same file is referenced using absolute and relative names",
{
"/a/b/c.ts": `/// <reference path="d.ts"/>`,
"/a/b/d.ts": "var x",
},
{ module: ts.ModuleKind.AMD },
"/a/b",
/*useCaseSensitiveFileNames*/ false,
["c.ts", "/a/b/d.ts"],
);
test(
"two files used in program differ only in casing (tripleslash references)",
{
"/a/b/c.ts": `/// <reference path="D.ts"/>`,
"/a/b/d.ts": "var x",
},
{ module: ts.ModuleKind.AMD, forceConsistentCasingInFileNames: true },
"/a/b",
/*useCaseSensitiveFileNames*/ false,
["c.ts", "d.ts"],
);
test(
"two files used in program differ only in casing (imports)",
{
"/a/b/c.ts": `import {x} from "D"`,
"/a/b/d.ts": "export var x",
},
{ module: ts.ModuleKind.AMD, forceConsistentCasingInFileNames: true },
"/a/b",
/*useCaseSensitiveFileNames*/ false,
["c.ts", "d.ts"],
);
test(
"two files used in program differ only in casing (imports, relative module names)",
{
"moduleA.ts": `import {x} from "./ModuleB"`,
"moduleB.ts": "export var x",
},
{ module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true },
"",
/*useCaseSensitiveFileNames*/ false,
["moduleA.ts", "moduleB.ts"],
);
test(
"two files exist on disk that differs only in casing",
{
"/a/b/c.ts": `import {x} from "D"`,
"/a/b/D.ts": "export var x",
"/a/b/d.ts": "export var y",
},
{ module: ts.ModuleKind.AMD },
"/a/b",
/*useCaseSensitiveFileNames*/ true,
["c.ts", "d.ts"],
);
test(
"module name in require calls has inconsistent casing",
{
"moduleA.ts": `import a = require("./ModuleC")`,
"moduleB.ts": `import a = require("./moduleC")`,
"moduleC.ts": "export var x",
},
{ module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true },
"",
/*useCaseSensitiveFileNames*/ false,
["moduleA.ts", "moduleB.ts", "moduleC.ts"],
);
test(
"module names in require calls has inconsistent casing and current directory has uppercase chars",
{
"/a/B/c/moduleA.ts": `import a = require("./ModuleC")`,
"/a/B/c/moduleB.ts": `import a = require("./moduleC")`,
"/a/B/c/moduleC.ts": "export var x",
"/a/B/c/moduleD.ts": `
import a = require("./moduleA");
import b = require("./moduleB");
`,
},
{ module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true },
"/a/B/c",
/*useCaseSensitiveFileNames*/ false,
["moduleD.ts"],
);
test(
"module names in require calls has consistent casing and current directory has uppercase chars",
{
"/a/B/c/moduleA.ts": `import a = require("./moduleC")`,
"/a/B/c/moduleB.ts": `import a = require("./moduleC")`,
"/a/B/c/moduleC.ts": "export var x",
"/a/B/c/moduleD.ts": `
import a = require("./moduleA");
import b = require("./moduleB");
`,
},
{ module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true },
"/a/B/c",
/*useCaseSensitiveFileNames*/ false,
["moduleD.ts"],
);
test(
"two files in program differ only in drive letter in their names",
{
"d:/someFolder/moduleA.ts": `import a = require("D:/someFolder/moduleC")`,
"d:/someFolder/moduleB.ts": `import a = require("./moduleC")`,
"D:/someFolder/moduleC.ts": "export const x = 10",
},
{ module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true },
"d:/someFolder",
/*useCaseSensitiveFileNames*/ false,
["d:/someFolder/moduleA.ts", "d:/someFolder/moduleB.ts"],
);
});
describe("unittests:: moduleResolution:: baseUrl augmented module resolution", () => {
it("module resolution without path mappings/rootDirs", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("baseUrl without path mappings or rootDirs", baselines);
function test(hasDirectoryExists: boolean) {
const file1: File = { name: "/root/folder1/file1.ts" };
const file2: File = { name: "/root/folder2/file2.ts" };
const file3: File = { name: "/root/folder2/file3.ts" };
const host = createModuleResolutionHost(baselines, hasDirectoryExists, file1, file2, file3);
for (const moduleResolution of [ts.ModuleResolutionKind.Node10, ts.ModuleResolutionKind.Classic]) {
const options: ts.CompilerOptions = { moduleResolution, baseUrl: "/root" };
{
baselines.push(`Resolving "folder2/file2" from ${file1.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName("folder2/file2", file1.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
{
baselines.push(`Resolving "./file3" from ${file2.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName("./file3", file2.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
{
baselines.push(`Resolving "/root/folder1/file1" from ${file2.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName("/root/folder1/file1", file2.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
}
}
// add failure tests
});
it("node + baseUrl", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("node baseUrl", baselines);
function test(hasDirectoryExists: boolean) {
const main: File = { name: "/root/a/b/main.ts" };
const m1: File = { name: "/root/m1.ts" }; // load file as module
const m2: File = { name: "/root/m2/index.d.ts" }; // load folder as module
const m3: File = { name: "/root/m3/package.json", content: jsonToReadableText({ typings: "dist/typings.d.ts" }) };
const m3Typings: File = { name: "/root/m3/dist/typings.d.ts" };
const m4: File = { name: "/root/node_modules/m4.ts" }; // fallback to node
const options: ts.CompilerOptions = { moduleResolution: ts.ModuleResolutionKind.Node10, baseUrl: "/root" };
const host = createModuleResolutionHost(baselines, hasDirectoryExists, main, m1, m2, m3, m3Typings, m4);
check("m1", main);
check("m2", main);
check("m3", main);
check("m4", main);
function check(name: string, caller: File) {
baselines.push(`Resolving "${name}" from ${caller.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName(name, caller.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
}
});
it("classic + baseUrl", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("classic baseUrl", baselines);
function test(hasDirectoryExists: boolean) {
const main: File = { name: "/root/a/b/main.ts" };
const m1: File = { name: "/root/x/m1.ts" }; // load from base url
const m2: File = { name: "/m2.ts" }; // fallback to classic
const options: ts.CompilerOptions = { moduleResolution: ts.ModuleResolutionKind.Classic, baseUrl: "/root/x", jsx: ts.JsxEmit.React };
const host = createModuleResolutionHost(baselines, hasDirectoryExists, main, m1, m2);
check("m1", main);
check("m2", main);
function check(name: string, caller: File) {
baselines.push(`Resolving "${name}" from ${caller.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName(name, caller.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
}
});
it("node + baseUrl + path mappings", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("node baseUrl path mappings", baselines);
function test(hasDirectoryExists: boolean) {
const main: File = { name: "/root/folder1/main.ts" };
const file1: File = { name: "/root/folder1/file1.ts" };
const file2: File = { name: "/root/generated/folder1/file2.ts" }; // load remapped file as module
const file3: File = { name: "/root/generated/folder2/file3/index.d.ts" }; // load folder a module
const file4Typings: File = { name: "/root/generated/folder2/file4/package.json", content: jsonToReadableText({ typings: "dist/types.d.ts" }) };
const file4: File = { name: "/root/generated/folder2/file4/dist/types.d.ts" }; // load file pointed by typings
const file5: File = { name: "/root/someanotherfolder/file5/index.d.ts" }; // load remapped module from folder
const file6: File = { name: "/root/node_modules/file6.ts" }; // fallback to node
const host = createModuleResolutionHost(baselines, hasDirectoryExists, file1, file2, file3, file4, file4Typings, file5, file6);
const options: ts.CompilerOptions = {
moduleResolution: ts.ModuleResolutionKind.Node10,
baseUrl: "/root",
jsx: ts.JsxEmit.React,
paths: {
"*": [
"*",
"generated/*",
],
"somefolder/*": [
"someanotherfolder/*",
],
"/rooted/*": [
"generated/*",
],
},
};
check("folder1/file1");
check("folder1/file2");
check("/rooted/folder1/file2");
check("folder2/file3");
check("folder2/file4");
check("somefolder/file5");
check("file6");
function check(name: string) {
baselines.push(`Resolving "${name}" from ${main.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName(name, main.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
}
});
it("classic + baseUrl + path mappings", () => {
const baselines: string[] = [];
// classic mode does not use directoryExists
test(/*hasDirectoryExists*/ false);
runBaseline("classic baseUrl path mappings", baselines);
function test(hasDirectoryExists: boolean) {
const main: File = { name: "/root/folder1/main.ts" };
const file1: File = { name: "/root/folder1/file1.ts" };
const file2: File = { name: "/root/generated/folder1/file2.ts" };
const file3: File = { name: "/folder1/file3.ts" }; // fallback to classic
const host = createModuleResolutionHost(baselines, hasDirectoryExists, file1, file2, file3);
const options: ts.CompilerOptions = {
moduleResolution: ts.ModuleResolutionKind.Classic,
baseUrl: "/root",
jsx: ts.JsxEmit.React,
paths: {
"*": [
"*",
"generated/*",
],
"somefolder/*": [
"someanotherfolder/*",
],
"/rooted/*": [
"generated/*",
],
},
};
check("folder1/file1");
check("folder1/file2");
check("/rooted/folder1/file2");
check("folder1/file3");
function check(name: string) {
baselines.push(`Resolving "${name}" from ${main.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName(name, main.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
}
});
it("node + rootDirs", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("node rootDirs", baselines);
function test(hasDirectoryExists: boolean) {
const file1: File = { name: "/root/folder1/file1.ts" };
const file1_1: File = { name: "/root/folder1/file1_1/index.d.ts" }; // eslint-disable-line @typescript-eslint/naming-convention
const file2: File = { name: "/root/generated/folder1/file2.ts" };
const file3: File = { name: "/root/generated/folder2/file3.ts" };
const host = createModuleResolutionHost(baselines, hasDirectoryExists, file1, file1_1, file2, file3);
const options: ts.CompilerOptions = {
moduleResolution: ts.ModuleResolutionKind.Node10,
rootDirs: [
"/root",
"/root/generated/",
],
};
check("./file2", file1);
check("../folder1/file1", file3);
check("../folder1/file1_1", file3);
function check(name: string, container: File) {
baselines.push(`Resolving "${name}" from ${container.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName(name, container.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
}
});
it("classic + rootDirs", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ false);
runBaseline("classic rootDirs", baselines);
function test(hasDirectoryExists: boolean) {
const file1: File = { name: "/root/folder1/file1.ts" };
const file2: File = { name: "/root/generated/folder1/file2.ts" };
const file3: File = { name: "/root/generated/folder2/file3.ts" };
const file4: File = { name: "/folder1/file1_1.ts" };
const host = createModuleResolutionHost(baselines, hasDirectoryExists, file1, file2, file3, file4);
const options: ts.CompilerOptions = {
moduleResolution: ts.ModuleResolutionKind.Classic,
jsx: ts.JsxEmit.React,
rootDirs: [
"/root",
"/root/generated/",
],
};
check("./file2", file1);
check("../folder1/file1", file3);
check("folder1/file1_1", file3);
function check(name: string, container: File) {
baselines.push(`Resolving "${name}" from ${container.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName(name, container.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
}
});
it("nested node module", () => {
const baselines: string[] = [];
test(/*hasDirectoryExists*/ true);
test(/*hasDirectoryExists*/ false);
runBaseline("nested node module", baselines);
function test(hasDirectoryExists: boolean) {
const app: File = { name: "/root/src/app.ts" };
const libsPackage: File = { name: "/root/src/libs/guid/package.json", content: jsonToReadableText({ typings: "dist/guid.d.ts" }) };
const libsTypings: File = { name: "/root/src/libs/guid/dist/guid.d.ts" };
const host = createModuleResolutionHost(baselines, hasDirectoryExists, app, libsPackage, libsTypings);
const options: ts.CompilerOptions = {
moduleResolution: ts.ModuleResolutionKind.Node10,
baseUrl: "/root",
paths: {
"libs/guid": ["src/libs/guid"],
},
};
baselines.push(`Resolving "libs/guid" from ${app.name}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveModuleName("libs/guid", app.name, options, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
});
});
describe("unittests:: moduleResolution:: ModuleResolutionHost.directoryExists", () => {
it("No 'fileExists' calls if containing directory is missing", () => {
const host: ts.ModuleResolutionHost = {
readFile: ts.notImplemented,
fileExists: ts.notImplemented,
directoryExists: _ => false,
};
const result = ts.resolveModuleName("someName", "/a/b/c/d", { moduleResolution: ts.ModuleResolutionKind.Node10 }, host);
assert(!result.resolvedModule);
});
});
describe("unittests:: moduleResolution:: Type reference directive resolution: ", () => {
function testWorker(baselines: string[], hasDirectoryExists: boolean, typesRoot: string | undefined, typeDirective: string, initialFile: File, targetFile: File, ...otherFiles: File[]) {
const host = createModuleResolutionHost(baselines, hasDirectoryExists, ...[initialFile, targetFile].concat(...otherFiles));
baselines.push(`Resolving "${typeDirective}" from ${initialFile.name} typesRoots: ${typesRoot ? `[${typesRoot}]` : undefined}${hasDirectoryExists ? "" : " with host that doesnt have directoryExists"}`);
const result = ts.resolveTypeReferenceDirective(typeDirective, initialFile.name, typesRoot ? { typeRoots: [typesRoot] } : {}, host);
baselines.push(`Resolution:: ${jsonToReadableText(result)}`);
baselines.push("");
}
function test(baselines: string[], typesRoot: string, typeDirective: string, initialFile: File, targetFile: File, ...otherFiles: File[]) {
testWorker(baselines, /*hasDirectoryExists*/ false, typesRoot, typeDirective, initialFile, targetFile, ...otherFiles);
}
it("Can be resolved from primary location", () => {
const baselines: string[] = [];
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/types/lib/index.d.ts" };
test(baselines, /*typesRoot*/ "/root/src/types", /* typeDirective */ "lib", f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/types/lib/typings/lib.d.ts" };
const packageFile = { name: "/root/src/types/lib/package.json", content: jsonToReadableText({ types: "typings/lib.d.ts" }) };
test(baselines, /*typesRoot*/ "/root/src/types", /* typeDirective */ "lib", f1, f2, packageFile);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/node_modules/lib/index.d.ts" };
test(baselines, /*typesRoot*/ "/root/src/types", /* typeDirective */ "lib", f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/node_modules/lib/typings/lib.d.ts" };
const packageFile = { name: "/root/src/node_modules/lib/package.json", content: jsonToReadableText({ types: "typings/lib.d.ts" }) };
test(baselines, /*typesRoot*/ "/root/src/types", /* typeDirective */ "lib", f1, f2, packageFile);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/node_modules/@types/lib/index.d.ts" };
test(baselines, /*typesRoot*/ "/root/src/types", /* typeDirective */ "lib", f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/node_modules/@types/lib/typings/lib.d.ts" };
const packageFile = { name: "/root/src/node_modules/@types/lib/package.json", content: jsonToReadableText({ types: "typings/lib.d.ts" }) };
test(baselines, /*typesRoot*/ "/root/src/types", /* typeDirective */ "lib", f1, f2, packageFile);
}
runBaseline("type reference from primary location", baselines);
});
it("Can be resolved from secondary location", () => {
const baselines: string[] = [];
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/node_modules/lib.d.ts" };
test(baselines, /*typesRoot*/ "/root/src/types", /* typeDirective */ "lib", f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/node_modules/lib/index.d.ts" };
test(baselines, /*typesRoot*/ "/root/src/types", /* typeDirective */ "lib", f1, f2);
}
{