-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathresolution.spec.ts
More file actions
223 lines (210 loc) · 7.22 KB
/
resolution.spec.ts
File metadata and controls
223 lines (210 loc) · 7.22 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
import * as path from "path";
import * as ts from "typescript";
import { couldNotResolveRequire, emitPathCollision } from "../../../src/transpilation/diagnostics";
import * as util from "../../util";
const requireRegex = /require\("(.*?)"\)/;
const expectToRequire =
(expected: string): util.TapCallback =>
builder => {
const [, requiredPath] = builder.getMainLuaCodeChunk().match(requireRegex) ?? [];
expect(requiredPath).toBe(expected);
};
test.each([
{
filePath: "main.ts",
usedPath: "./folder/Module",
expected: "folder.Module",
options: { rootDir: "." },
},
{
filePath: "main.ts",
usedPath: "./folder/Module",
expected: "folder.Module",
options: { rootDir: "./" },
},
{
filePath: "src/main.ts",
usedPath: "./folder/Module",
expected: "src.folder.Module",
options: { rootDir: "." },
},
{
filePath: "main.ts",
usedPath: "folder/Module",
expected: "folder.Module",
options: { rootDir: ".", baseUrl: "." },
},
{
filePath: "main.ts",
usedPath: "folder/Module",
expected: "folder.Module",
options: { rootDir: "./", baseUrl: "." },
},
{
filePath: "src/main.ts",
usedPath: "./folder/Module",
expected: "folder.Module",
options: { rootDir: "src" },
},
{
filePath: "src/main.ts",
usedPath: "./folder/Module",
expected: "folder.Module",
options: { rootDir: "./src" },
},
{
filePath: "src/dir/main.ts",
usedPath: "../Module",
expected: "Module",
options: { rootDir: "./src" },
},
{
filePath: "src/dir/dir/main.ts",
usedPath: "../../dir/Module",
expected: "dir.Module",
options: { rootDir: "./src" },
},
])("resolve paths with baseUrl or rootDir (%p)", ({ filePath, usedPath, expected, options }) => {
util.testModule`
import * as module from "${usedPath}";
module;
`
.setMainFileName(filePath)
.addExtraFile(`${usedPath}.ts`, "")
.setOptions(options)
.tap(expectToRequire(expected));
});
test("doesn't resolve paths out of root dir", () => {
util.testModule`
import * as module from "../module";
module;
`
.setMainFileName("src/main.ts")
.setOptions({ rootDir: "./src" })
.disableSemanticCheck()
.expectDiagnosticsToMatchSnapshot([couldNotResolveRequire.code]);
});
test("resolves non-standard requires", () => {
const { transpiledFiles } = util.testModule`
export * from "./externalLua";
`
.addExtraFile("externalLua.d.ts", "export const foo = 3;")
.addExtraFile(
"externalLua.lua",
`
require("requiredLuaFile1") -- standard
require('requiredLuaFile2') -- single quote
require'requiredLuaFile3' -- no parentheses
require"requiredLuaFile4" -- no parentheses double quote
require "requiredLuaFile5" -- no parentheses and space
require "requiredLua'File6" -- no parentheses and space
require 'requiredLua"File7' -- no parentheses and space
`
)
.addExtraFile("requiredLuaFile1.lua", "")
.addExtraFile("requiredLuaFile2.lua", "")
.addExtraFile("requiredLuaFile3.lua", "")
.addExtraFile("requiredLuaFile4.lua", "")
.addExtraFile("requiredLuaFile5.lua", "")
.addExtraFile("requiredLua'File6.lua", "")
.addExtraFile('requiredLua"File7.lua', "")
.expectToHaveNoDiagnostics()
.getLuaResult();
// Expect main.lua, externalLua.lua and all 7 required lua files in there
expect(transpiledFiles.map(f => f.outPath)).toHaveLength(9);
});
test.each([
{
declarationStatement: `
/** @noResolution */
declare module "fake" {}
`,
mainCode: 'import "fake";',
expectedPath: "fake",
},
{
declarationStatement: `
/** @noResolution */
declare module "fake" {}
`,
mainCode: 'import * as fake from "fake"; fake;',
expectedPath: "fake",
},
{
declarationStatement: `
/** @noResolution */
declare module "fake" {
export const x: number;
}
`,
mainCode: 'import { x } from "fake"; x;',
expectedPath: "fake",
},
{
declarationStatement: `
/** @noResolution */
declare module "fake" {
export const x: number;
}
declare module "fake" {
export const y: number;
}
`,
mainCode: 'import { y } from "fake"; y;',
expectedPath: "fake",
},
])("noResolution prevents any module path resolution behavior", ({ declarationStatement, mainCode, expectedPath }) => {
util.testModule(mainCode)
.setMainFileName("src/main.ts")
.addExtraFile("module.d.ts", declarationStatement)
.tap(expectToRequire(expectedPath));
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1445
// Can't test this via execution because the test harness uses package.preload
// instead of real filesystem resolution, so require() always finds the module
// regardless of output path. We check the output path directly instead.
test("dots in directory names are replaced with underscores in output", () => {
const { transpiledFiles } = util.testModule`
import { answer } from "./Foo.Bar";
export const result = answer;
`
.addExtraFile("Foo.Bar/index.ts", "export const answer = 42;")
.setOptions({ rootDir: "." })
.getLuaResult();
const dottedFile = transpiledFiles.find(f => f.lua?.includes("answer = 42"));
expect(dottedFile).toBeDefined();
expect(dottedFile!.outPath).toContain(path.join("Foo_Bar", "index.lua"));
expect(dottedFile!.outPath).not.toContain("Foo.Bar");
});
test("dots in file names are replaced with underscores in output", () => {
const { transpiledFiles } = util.testModule`
import { answer } from "./foo.test";
export const result = answer;
`
.addExtraFile("foo.test.ts", "export const answer = 42;")
.setOptions({ rootDir: "." })
.getLuaResult();
const dottedFile = transpiledFiles.find(f => f.lua?.includes("answer = 42"));
expect(dottedFile).toBeDefined();
expect(dottedFile!.outPath).toContain("foo_test.lua");
expect(dottedFile!.outPath).not.toContain("foo.test");
});
test("dots in paths that collide with existing paths produce a diagnostic", () => {
util.testModule`
import { a } from "./Foo.Bar";
import { b } from "./Foo_Bar";
export const result = a + b;
`
.addExtraFile("Foo.Bar/index.ts", "export const a = 1;")
.addExtraFile("Foo_Bar/index.ts", "export const b = 2;")
.setOptions({ rootDir: "." })
.expectToHaveDiagnostics([emitPathCollision.code]);
});
test("import = require", () => {
util.testModule`
import foo = require("./foo/bar");
foo;
`
.setOptions({ module: ts.ModuleKind.CommonJS })
.tap(expectToRequire("foo.bar"));
});