forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule-resolution.spec.ts
More file actions
740 lines (648 loc) · 29.3 KB
/
Copy pathmodule-resolution.spec.ts
File metadata and controls
740 lines (648 loc) · 29.3 KB
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
import * as path from "path";
import * as tstl from "../../src";
import * as util from "../util";
import * as ts from "typescript";
import { BuildMode } from "../../src";
import { normalizeSlashes } from "../../src/utils";
import { pathsWithoutBaseUrl } from "../../src/transpilation/diagnostics";
describe("basic module resolution", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-node-modules");
const projectWithNodeModules = util
.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.ts"));
test("can resolve global dependencies with declarations", () => {
// Declarations in the node_modules directory
expect(projectWithNodeModules.getLuaExecutionResult().globalWithDeclarationsResults).toEqual({
foo: "foo from lua global with decls",
bar: "bar from lua global with decls: global with declarations!",
baz: "baz from lua global with decls",
});
});
test("can resolve global dependencies with hand-written declarations", () => {
// No declarations in the node_modules directory, but written by hand in project dir
expect(projectWithNodeModules.getLuaExecutionResult().globalWithoutDeclarationsResults).toEqual({
foo: "foo from lua global without decls",
bar: "bar from lua global without decls: global without declarations!",
baz: "baz from lua global without decls",
});
});
test("can resolve module dependencies with declarations", () => {
// Declarations in the node_modules directory
expect(projectWithNodeModules.getLuaExecutionResult().moduleWithDeclarationsResults).toEqual({
foo: "foo from lua module with decls",
bar: "bar from lua module with decls: module with declarations!",
baz: "baz from lua module with decls",
});
});
test("can resolve module dependencies with hand-written declarations", () => {
// Declarations in the node_modules directory
expect(projectWithNodeModules.getLuaExecutionResult().moduleWithoutDeclarationsResults).toEqual({
foo: "foo from lua module without decls",
bar: "bar from lua module without decls: module without declarations!",
baz: "baz from lua module without decls",
});
});
test("can resolve package depencency with a dependency on another package", () => {
// Declarations in the node_modules directory
expect(projectWithNodeModules.getLuaExecutionResult().moduleWithDependencyResult).toBe(
"Calling dependency: foo from lua module with decls"
);
});
test("resolved package dependency included in bundle", () => {
const mainFile = path.join(projectPath, "main.ts");
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(mainFile)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual({
globalWithDeclarationsResults: {
foo: "foo from lua global with decls",
bar: "bar from lua global with decls: global with declarations!",
baz: "baz from lua global with decls",
},
globalWithoutDeclarationsResults: {
foo: "foo from lua global without decls",
bar: "bar from lua global without decls: global without declarations!",
baz: "baz from lua global without decls",
},
moduleWithDeclarationsResults: {
foo: "foo from lua module with decls",
bar: "bar from lua module with decls: module with declarations!",
baz: "baz from lua module with decls",
},
moduleWithDependencyResult: "Calling dependency: foo from lua module with decls",
moduleWithoutDeclarationsResults: {
foo: "foo from lua module without decls",
bar: "bar from lua module without decls: module without declarations!",
baz: "baz from lua module without decls",
},
});
});
});
describe("module resolution with chained dependencies", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-dependency-chain");
const expectedResult = { result: "dependency3", result2: "someFunc from otherfile.lua" };
test("can resolve dependencies in chain", () => {
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.ts"))
.expectToEqual(expectedResult);
});
test("resolved package dependency included in bundle", () => {
const mainFile = path.join(projectPath, "main.ts");
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(mainFile)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual(expectedResult);
});
test("works with different module setting", () => {
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.ts"))
.setOptions({ module: ts.ModuleKind.ESNext })
.expectToEqual(expectedResult);
});
});
describe("module resolution with outDir", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-dependency-chain");
const expectedResult = { result: "dependency3", result2: "someFunc from otherfile.lua" };
test("emits files in outDir", () => {
const builder = util
.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.ts"))
.setOptions({ outDir: "tstl-out" })
.expectToEqual(expectedResult);
// Get the output paths relative to the project path
const outPaths = builder.getLuaResult().transpiledFiles.map(f => path.relative(projectPath, f.outPath));
expect(outPaths).toHaveLength(5);
expect(outPaths).toContain(path.join("tstl-out", "main.lua"));
// Note: outputs to lua_modules
expect(outPaths).toContain(path.join("tstl-out", "lua_modules", "dependency1", "index.lua"));
expect(outPaths).toContain(path.join("tstl-out", "lua_modules", "dependency1", "otherfile.lua"));
expect(outPaths).toContain(path.join("tstl-out", "lua_modules", "dependency2", "index.lua"));
expect(outPaths).toContain(path.join("tstl-out", "lua_modules", "dependency3", "index.lua"));
});
test("emits bundle in outDir", () => {
const mainFile = path.join(projectPath, "main.ts");
const builder = util
.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(mainFile)
.setOptions({ outDir: "tstl-out", luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual(expectedResult);
// Get the output paths relative to the project path
const outPaths = builder.getLuaResult().transpiledFiles.map(f => path.relative(projectPath, f.outPath));
expect(outPaths).toHaveLength(1);
expect(outPaths).toContain(path.join("tstl-out", "bundle.lua"));
});
});
describe("module resolution with sourceDir", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-sourceDir");
const expectedResult = {
result: "dependency3",
functionInSubDir: "non-node_modules import",
functionReExportedFromSubDir: "nested func result",
nestedFunctionInSubDirOfSubDir: "nested func result",
nestedFunctionUsingFunctionFromParentDir: "nested func: non-node_modules import 2",
};
test("can resolve dependencies with sourceDir", () => {
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "src", "main.ts"))
.setOptions({ outDir: "tstl-out" })
.expectToEqual(expectedResult);
});
test("can resolve dependencies and bundle files with sourceDir", () => {
const mainFile = path.join(projectPath, "src", "main.ts");
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(mainFile)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual(expectedResult);
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1394
test("can resolve files with non-standard extension (#1394)", () => {
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "src", "main.ts"))
.setOptions({ outDir: "tstl-out", extension: ".script" })
.expectToEqual(expectedResult);
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1394
test("can resolve files with non-standard extension without separator (#1394)", () => {
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "src", "main.ts"))
.setOptions({ outDir: "tstl-out", extension: "script" })
.expectToEqual(expectedResult);
});
});
describe("module resolution project with lua sources", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-lua-sources");
const expectedResult = {
funcFromLuaFile: "lua file in subdir",
funcFromSubDirLuaFile: "lua file in subdir",
};
test("can resolve lua dependencies", () => {
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.ts"))
.setOptions({ outDir: "tstl-out" })
.expectToEqual(expectedResult);
});
test("can resolve dependencies and bundle files with sourceDir", () => {
const mainFile = path.join(projectPath, "main.ts");
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(mainFile)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual(expectedResult);
});
});
describe("module resolution in library mode", () => {
test("result does not contain resolved paths", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-dependency-chain");
const { transpiledFiles } = util
.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.ts"))
.setOptions({ buildMode: tstl.BuildMode.Library })
.expectToHaveNoDiagnostics()
.getLuaResult();
for (const file of transpiledFiles) {
expect(file.lua).not.toContain('require("lua_modules');
}
});
test("project works in library mode because no external dependencies", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-lua-sources");
const { transpiledFiles } = util
.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.ts"))
.setOptions({ outDir: "tstl-out", buildMode: tstl.BuildMode.Library })
.expectToEqual({
funcFromLuaFile: "lua file in subdir",
funcFromSubDirLuaFile: "lua file in subdir",
})
.getLuaResult();
for (const file of transpiledFiles) {
expect(file.lua).not.toContain('require("lua_modules');
}
});
});
describe("module resolution project with dependencies built by tstl library mode", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-tstl-library-modules");
// First compile dependencies into node_modules. NOTE: Actually writing to disk, very slow
tstl.transpileProject(path.join(projectPath, "dependency1-ts", "tsconfig.json"));
tstl.transpileProject(path.join(projectPath, "dependency2-ts", "tsconfig.json"));
const expectedResult = {
dependency1IndexResult: "function in dependency 1 index: dependency1OtherFileFunc in dependency1/d1otherfile",
dependency1OtherFileFuncResult: "dependency1OtherFileFunc in dependency1/d1otherfile",
dependency2MainResult: "dependency 2 main",
dependency2OtherFileResult: "Dependency 2 func: my string argument",
};
test("can resolve lua dependencies", () => {
const transpileResult = util
.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.ts"))
.setOptions({ outDir: "tstl-out" })
.expectToEqual(expectedResult)
.getLuaResult();
// Assert node_modules file requires the correct lualib_bundle
const requiringLuaFile = path.join("lua_modules", "dependency1", "index.lua");
const lualibRequiringFile = transpileResult.transpiledFiles.find(f => f.outPath.endsWith(requiringLuaFile));
expect(lualibRequiringFile).toBeDefined();
expect(lualibRequiringFile?.lua).toContain('require("lualib_bundle")');
});
test("can resolve dependencies and bundle", () => {
const mainFile = path.join(projectPath, "main.ts");
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(mainFile)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual(expectedResult);
});
});
describe("module resolution project with dependencies built by tstl library mode and has exports field", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-tstl-library-has-exports-field");
// First compile dependencies into node_modules. NOTE: Actually writing to disk, very slow
const dependency1Path = path.join(projectPath, "node_modules", "dependency1");
tstl.transpileProject(path.join(dependency1Path, "tsconfig.json"));
const expectedResult = {
dependency1IndexResult: "function in dependency 1 index: dependency1OtherFileFunc in dependency1/d1otherfile",
dependency1OtherFileFuncResult: "dependency1OtherFileFunc in dependency1/d1otherfile",
};
test("can resolve lua dependencies", () => {
const transpileResult = util
.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.ts"))
.setOptions({
outDir: "tstl-out",
moduleResolution: ts.ModuleResolutionKind.Node16,
module: ts.ModuleKind.Node16,
})
.expectToEqual(expectedResult)
.getLuaResult();
// Assert node_modules file requires the correct lualib_bundle
const requiringLuaFile = path.join("lua_modules", "dependency1", "dist", "index.lua");
const lualibRequiringFile = transpileResult.transpiledFiles.find(f => f.outPath.endsWith(requiringLuaFile));
expect(lualibRequiringFile).toBeDefined();
expect(lualibRequiringFile?.lua).toContain('require("lualib_bundle")');
});
test("can resolve dependencies and bundle", () => {
const mainFile = path.join(projectPath, "main.ts");
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(mainFile)
.setOptions({
luaBundle: "bundle.lua",
luaBundleEntry: mainFile,
moduleResolution: ts.ModuleResolutionKind.Node16,
module: ts.ModuleKind.Node16,
})
.expectToEqual(expectedResult);
});
});
// Test fix for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1037
describe("module resolution with tsx", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-tsx");
test("project with tsx files", () => {
util.testProject(path.join(projectPath, "tsconfig.json"))
.setMainFileName(path.join(projectPath, "main.tsx"))
.expectToEqual({
result: "hello from other.tsx",
indexResult: "hello from dir/index.tsx",
});
});
});
describe("dependency with complicated inner structure", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-complicated-dependency");
const tsConfigPath = path.join(projectPath, "tsconfig.json");
const mainFilePath = path.join(projectPath, "main.ts");
const expectedResult = {
otherFileResult: "someFunc from otherfile.lua",
otherFileUtil: "util",
subsubresult: "result from subsub dir",
utilResult: "util",
subdirwithInitResult: "a",
};
// Test fix for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1055
test("bundle should not contain duplicate files", () => {
const mainFile = path.join(projectPath, "main.ts");
const { transpiledFiles } = util
.testProject(tsConfigPath)
.setMainFileName(mainFilePath)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual(expectedResult)
.getLuaResult();
expect(transpiledFiles).toHaveLength(1);
const lua = transpiledFiles[0].lua!;
// util is used in 2 places, but should only occur once in the bundle
const utilModuleOccurrences = (lua.match(/\["lua_modules\.dependency1\.util"\]/g) ?? []).length;
expect(utilModuleOccurrences).toBe(1);
});
// Test fix for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1054
test("should be able to resolve dependency files in subdirectories", () => {
util.testProject(tsConfigPath).setMainFileName(mainFilePath).expectToEqual(expectedResult);
});
});
test("module resolution should not try to resolve @noResolution annotation", () => {
util.testModule`
import * as json from "json";
const test = json.decode("{}");
`
.addExtraFile(
"json.d.ts",
`
/** @noResolution */
declare module "json" {
function encode(this: void, data: unknown): string;
function decode(this: void, data: string): unknown;
}
`
)
.expectToHaveNoDiagnostics();
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1101
test("module resolution inline require of environment library workaround", () => {
util.testModule`
declare function require(this: void, module: string): any;
const test = require("@NoResolution:luasource");
test.foo();
`.expectToHaveNoDiagnostics();
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1118
describe("module resolution should not try to resolve modules in noResolvePaths", () => {
test("as used in direct import", () => {
util.testModule`
import * as lua from "directimport";
lua.foo();
`
.addExtraFile(
"directimport.d.ts",
`declare module "directimport" {
export function foo(): void;
}`
)
.setOptions({ noResolvePaths: ["directimport"] })
.expectToHaveNoDiagnostics();
});
test("as used in imported lua sources", () => {
util.testModule`
import * as lua from "./luasource";
lua.foo();
`
.addExtraFile("luasource.d.ts", "export function foo(): void;")
.addExtraFile(
"luasource.lua",
`
require("dontResolveThis")
require("a.b.c.foo")
return { foo = function() return "bar" end }
`
)
.setOptions({ noResolvePaths: ["a.b.c.foo", "somethingExtra", "dontResolveThis"] })
.expectToHaveNoDiagnostics();
});
test("can ignore specific files with glob pattern", () => {
util.testModule`
// Pre-Load as to not error out at runtime
import "preload";
import "ignoreme";
import * as b from "./actualfile";
export const result = b.foo();
`
.addExtraFile("preload.lua", 'package.preload["ignoreme"] = function() return nil end')
.addExtraFile(
"actualfile.ts",
`export function foo()
{
return 'foo';
}`
)
.addExtraFile(
"ignoreme.d.ts",
`declare module "ignoreme" {
export function foo(): void;
}`
)
.setOptions({ noResolvePaths: ["ignore*"] })
.expectToHaveNoDiagnostics()
.expectToEqual({ result: "foo" });
});
test("can ignore all files with glob pattern in require", () => {
util.testModule`
declare function require(this: void, module: string): any;
const a = require("a")
const b = require("b/b")
const c = require("c/c/c")
const d = require("!:?somefile")
`
.setOptions({ noResolvePaths: ["**"] })
.expectToHaveNoDiagnostics();
});
test("can ignore all files with glob pattern as used in imported lua sources", () => {
util.testModule`
import * as lua from "./luasource";
lua.foo();
`
.addExtraFile("luasource.d.ts", "export function foo(): void;")
.addExtraFile(
"luasource.lua",
`
require("ignoreme!")
require("i.g.n.o.r.e")
`
)
.setOptions({ noResolvePaths: ["**"] })
.expectToHaveNoDiagnostics();
});
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1062
test("module resolution should not rewrite @NoResolution requires in library mode", () => {
const { transpiledFiles } = util.testModule`
import * as json from "json";
const test = json.decode("{}");
`
.addExtraFile(
"json.d.ts",
`
/** @noResolution */
declare module "json" {
function encode(this: void, data: unknown): string;
function decode(this: void, data: string): unknown;
}
`
)
.setOptions({ buildMode: BuildMode.Library })
.getLuaResult();
expect(transpiledFiles).toHaveLength(1);
expect(transpiledFiles[0].lua).toContain('require("@NoResolution:');
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1050
test("module resolution should not try to resolve require-like functions", () => {
util.testModule`
function custom_require(this: void, value: string) {
return value;
}
namespace ns {
export function require(this: void, value: string) {
return value;
}
}
class MyClass {
require(value: string) {
return value;
}
}
const inst = new MyClass();
export const result = [
custom_require("value 1"),
ns.require("value 2"),
inst.require("value 3")
];
`
.expectToHaveNoDiagnostics()
.expectToEqual({
result: ["value 1", "value 2", "value 3"],
});
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1050
test("module resolution uses baseURL to resolve imported files", () => {
util.testModule`
import { foo } from "dep1";
import { bar } from "dep2";
import { baz } from "luadep";
export const fooResult = foo();
export const barResult = bar();
export const bazResult = baz();
`
.addExtraFile(
"myproject/mydeps/dep1.ts",
`
export function foo() { return "foo"; }
`
)
.addExtraFile(
"myproject/mydeps/dep2.ts",
`
export function bar() { return "bar"; }
`
)
.addExtraFile(
"myproject/mydeps/luadep.d.ts",
`
export function baz(): string;
`
)
.addExtraFile(
"myproject/mydeps/luadep.lua",
`
return { baz = function() return "baz" end }
`
)
.setOptions({ baseUrl: "./myproject/mydeps" })
.expectToEqual({
fooResult: "foo",
barResult: "bar",
bazResult: "baz",
});
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1071
test("includes lualib_bundle when external lua requests it", () => {
util.testModule`
export { foo } from "./lualibuser";
`
.addExtraFile(
"lualibuser.d.ts",
`
export const foo: string[];
`
)
.addExtraFile(
"lualibuser.lua",
`
local ____lualib = require("lualib_bundle")
local __TS__ArrayPush = ____lualib.__TS__ArrayPush
local result = {}
__TS__ArrayPush(result, "foo")
__TS__ArrayPush(result, "bar")
return { foo = result }
`
)
.expectToEqual({
foo: ["foo", "bar"],
});
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1146
test("require matches correct pattern", () => {
util.testModule`
declare function require(this: void, module: string): any;
export const addResult = require("a").foo + require("b").foo;
export const callResult = require("c")("foo");
`
.addExtraFile("a.lua", "return { foo = 3 }")
.addExtraFile("b.lua", "return { foo = 5 }")
.addExtraFile("c.lua", "return function(self, a) return a end")
.expectToEqual({ addResult: 3 + 5, callResult: "foo" });
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1307
test("lualib_module with parent directory import (#1307)", () => {
const projectDir = path.join(__dirname, "module-resolution", "project-with-dependency-with-same-file-names");
const inputProject = path.join(projectDir, "tsconfig.json");
util.testProject(inputProject).setMainFileName(path.join(projectDir, "index.ts")).expectToEqual({
// eslint-disable-next-line @typescript-eslint/naming-convention
BASE_CONSTANT: 123,
// eslint-disable-next-line @typescript-eslint/naming-convention
FEATURE_CONSTANT: 456,
});
});
test("supports paths configuration", () => {
// Package root
const baseProjectPath = path.resolve(__dirname, "module-resolution", "paths-simple");
// myprogram package
const projectPath = path.join(baseProjectPath, "myprogram");
const projectTsConfig = path.join(projectPath, "tsconfig.json");
const mainFile = path.join(projectPath, "main.ts");
const luaResult = util
.testProject(projectTsConfig)
.setMainFileName(mainFile)
.expectToHaveNoDiagnostics()
.getLuaResult();
expect(snapshotPaths(luaResult.transpiledFiles)).toMatchSnapshot();
// Bundle to have all files required to execute and check result
util.testProject(projectTsConfig)
.setMainFileName(mainFile)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual({ foo: 314, bar: 271 });
});
test("supports complicated paths configuration", () => {
// Package root
const baseProjectPath = path.resolve(__dirname, "module-resolution", "paths-base-tsconfig");
// myprogram package
const projectPath = path.join(baseProjectPath, "packages", "myprogram");
const projectTsConfig = path.join(projectPath, "tsconfig.json");
const mainFile = path.join(projectPath, "src", "main.ts");
const luaResult = util
.testProject(projectTsConfig)
.setMainFileName(mainFile)
.expectToHaveNoDiagnostics()
.getLuaResult();
expect(snapshotPaths(luaResult.transpiledFiles)).toMatchSnapshot();
// Bundle to have all files required to execute
util.testProject(projectTsConfig)
.setMainFileName(mainFile)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual({ foo: 314, bar: 271 });
});
test("paths without baseUrl is error", () => {
util.testFunction``.setOptions({ paths: {} }).expectToHaveDiagnostics([pathsWithoutBaseUrl.code]);
});
test("module resolution using plugin", () => {
const baseProjectPath = path.resolve(__dirname, "module-resolution", "project-with-module-resolution-plugin");
const projectTsConfig = path.join(baseProjectPath, "tsconfig.json");
const mainFile = path.join(baseProjectPath, "main.ts");
const testBuilder = util
.testProject(projectTsConfig)
.setMainFileName(mainFile)
.setOptions({
luaPlugins: [
{
name: path.join(__dirname, "./plugins/moduleResolution.ts"),
},
],
})
.expectToHaveNoDiagnostics();
const luaResult = testBuilder.getLuaResult();
expect(luaResult.transpiledFiles).toHaveLength(3);
testBuilder.expectToEqual({ result: ["foo", "absolutefoo"] });
});
function snapshotPaths(files: tstl.TranspiledFile[]) {
return files.map(f => normalizeSlashes(f.outPath).split("module-resolution")[1]).sort();
}