/
forceConsistentCasingInFileNames.ts
361 lines (341 loc) · 16.2 KB
/
forceConsistentCasingInFileNames.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
namespace ts.tscWatch {
describe("unittests:: tsc-watch:: forceConsistentCasingInFileNames", () => {
const loggerFile: File = {
path: `${projectRoot}/logger.ts`,
content: `export class logger { }`
};
const anotherFile: File = {
path: `${projectRoot}/another.ts`,
content: `import { logger } from "./logger"; new logger();`
};
const tsconfig: File = {
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { forceConsistentCasingInFileNames: true }
})
};
function verifyConsistentFileNames({ subScenario, changes }: { subScenario: string; changes: TscWatchCompileChange[]; }) {
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario,
commandLineArgs: ["--w", "--p", tsconfig.path],
sys: () => createWatchedSystem([loggerFile, anotherFile, tsconfig, libFile]),
changes
});
}
verifyConsistentFileNames({
subScenario: "when changing module name with different casing",
changes: [
{
caption: "Change module name from logger to Logger",
change: sys => sys.writeFile(anotherFile.path, anotherFile.content.replace("./logger", "./Logger")),
timeouts: runQueuedTimeoutCallbacks,
}
]
});
verifyConsistentFileNames({
subScenario: "when renaming file with different casing",
changes: [
{
caption: "Change name of file from logger to Logger",
change: sys => sys.renameFile(loggerFile.path, `${projectRoot}/Logger.ts`),
timeouts: runQueuedTimeoutCallbacks,
}
]
});
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario: "when relative information file location changes",
commandLineArgs: ["--w", "--p", ".", "--explainFiles"],
sys: () => {
const moduleA: File = {
path: `${projectRoot}/moduleA.ts`,
content: `import a = require("./ModuleC")`
};
const moduleB: File = {
path: `${projectRoot}/moduleB.ts`,
content: `import a = require("./moduleC")`
};
const moduleC: File = {
path: `${projectRoot}/moduleC.ts`,
content: `export const x = 10;`
};
const tsconfig: File = {
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true } })
};
return createWatchedSystem([moduleA, moduleB, moduleC, libFile, tsconfig], { currentDirectory: projectRoot });
},
changes: [
{
caption: "Prepend a line to moduleA",
change: sys => sys.prependFile(`${projectRoot}/moduleA.ts`, `// some comment
`),
timeouts: runQueuedTimeoutCallbacks,
}
],
});
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario: "jsxImportSource option changed",
commandLineArgs: ["--w", "--p", ".", "--explainFiles"],
sys: () => createWatchedSystem([
libFile,
{
path: `${projectRoot}/node_modules/react/Jsx-runtime/index.d.ts`,
content: `export namespace JSX {
interface Element {}
interface IntrinsicElements {
div: {
propA?: boolean;
};
}
}
export function jsx(...args: any[]): void;
export function jsxs(...args: any[]): void;
export const Fragment: unique symbol;
`,
},
{
path: `${projectRoot}/node_modules/react/package.json`,
content: JSON.stringify({ name: "react", version: "0.0.1" })
},
{
path: `${projectRoot}/index.tsx`,
content: `export const App = () => <div propA={true}></div>;`
},
{
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { jsx: "react-jsx", jsxImportSource: "react", forceConsistentCasingInFileNames: true },
files: ["node_modules/react/Jsx-Runtime/index.d.ts", "index.tsx"]
})
}
], { currentDirectory: projectRoot }),
changes: emptyArray,
});
function verifyWindowsStyleRoot(subScenario: string, windowsStyleRoot: string, projectRootRelative: string) {
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario,
commandLineArgs: ["--w", "--p", `${windowsStyleRoot}/${projectRootRelative}`, "--explainFiles"],
sys: () => {
const moduleA: File = {
path: `${windowsStyleRoot}/${projectRootRelative}/a.ts`,
content: `
export const a = 1;
export const b = 2;
`
};
const moduleB: File = {
path: `${windowsStyleRoot}/${projectRootRelative}/b.ts`,
content: `
import { a } from "${windowsStyleRoot.toLocaleUpperCase()}/${projectRootRelative}/a"
import { b } from "${windowsStyleRoot.toLocaleLowerCase()}/${projectRootRelative}/a"
a;b;
`
};
const tsconfig: File = {
path: `${windowsStyleRoot}/${projectRootRelative}/tsconfig.json`,
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true } })
};
return createWatchedSystem([moduleA, moduleB, libFile, tsconfig], { windowsStyleRoot, useCaseSensitiveFileNames: false });
},
changes: [
{
caption: "Prepend a line to moduleA",
change: sys => sys.prependFile(`${windowsStyleRoot}/${projectRootRelative}/a.ts`, `// some comment
`),
timeouts: runQueuedTimeoutCallbacks,
}
],
});
}
verifyWindowsStyleRoot("when Windows-style drive root is lowercase", "c:/", "project");
verifyWindowsStyleRoot("when Windows-style drive root is uppercase", "C:/", "project");
function verifyFileSymlink(subScenario: string, diskPath: string, targetPath: string, importedPath: string) {
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario,
commandLineArgs: ["--w", "--p", ".", "--explainFiles"],
sys: () => {
const moduleA: File = {
path: diskPath,
content: `
export const a = 1;
export const b = 2;
`
};
const symlinkA: SymLink = {
path: `${projectRoot}/link.ts`,
symLink: targetPath,
};
const moduleB: File = {
path: `${projectRoot}/b.ts`,
content: `
import { a } from "${importedPath}";
import { b } from "./link";
a;b;
`
};
const tsconfig: File = {
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true } })
};
return createWatchedSystem([moduleA, symlinkA, moduleB, libFile, tsconfig], { currentDirectory: projectRoot });
},
changes: [
{
caption: "Prepend a line to moduleA",
change: sys => sys.prependFile(diskPath, `// some comment
`),
timeouts: runQueuedTimeoutCallbacks,
}
],
});
}
verifyFileSymlink("when both file symlink target and import match disk", `${projectRoot}/XY.ts`, `${projectRoot}/XY.ts`, `./XY`);
verifyFileSymlink("when file symlink target matches disk but import does not", `${projectRoot}/XY.ts`, `${projectRoot}/Xy.ts`, `./XY`);
verifyFileSymlink("when import matches disk but file symlink target does not", `${projectRoot}/XY.ts`, `${projectRoot}/XY.ts`, `./Xy`);
verifyFileSymlink("when import and file symlink target agree but do not match disk", `${projectRoot}/XY.ts`, `${projectRoot}/Xy.ts`, `./Xy`);
verifyFileSymlink("when import, file symlink target, and disk are all different", `${projectRoot}/XY.ts`, `${projectRoot}/Xy.ts`, `./yX`);
function verifyDirSymlink(subScenario: string, diskPath: string, targetPath: string, importedPath: string) {
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario,
commandLineArgs: ["--w", "--p", ".", "--explainFiles"],
sys: () => {
const moduleA: File = {
path: `${diskPath}/a.ts`,
content: `
export const a = 1;
export const b = 2;
`
};
const symlinkA: SymLink = {
path: `${projectRoot}/link`,
symLink: targetPath,
};
const moduleB: File = {
path: `${projectRoot}/b.ts`,
content: `
import { a } from "${importedPath}/a";
import { b } from "./link/a";
a;b;
`
};
const tsconfig: File = {
path: `${projectRoot}/tsconfig.json`,
// Use outFile because otherwise the real and linked files will have the same output path
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true, outFile: "out.js", module: "system" } })
};
return createWatchedSystem([moduleA, symlinkA, moduleB, libFile, tsconfig], { currentDirectory: projectRoot });
},
changes: [
{
caption: "Prepend a line to moduleA",
change: sys => sys.prependFile(`${diskPath}/a.ts`, `// some comment
`),
timeouts: runQueuedTimeoutCallbacks,
}
],
});
}
verifyDirSymlink("when both directory symlink target and import match disk", `${projectRoot}/XY`, `${projectRoot}/XY`, `./XY`);
verifyDirSymlink("when directory symlink target matches disk but import does not", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./XY`);
verifyDirSymlink("when import matches disk but directory symlink target does not", `${projectRoot}/XY`, `${projectRoot}/XY`, `./Xy`);
verifyDirSymlink("when import and directory symlink target agree but do not match disk", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./Xy`);
verifyDirSymlink("when import, directory symlink target, and disk are all different", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./yX`);
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario: "with nodeNext resolution",
commandLineArgs: ["--w", "--explainFiles"],
sys: () => createWatchedSystem({
"/Users/name/projects/web/src/bin.ts": `import { foo } from "yargs";`,
"/Users/name/projects/web/node_modules/@types/yargs/index.d.ts": "export function foo(): void;",
"/Users/name/projects/web/node_modules/@types/yargs/index.d.mts": "export function foo(): void;",
"/Users/name/projects/web/node_modules/@types/yargs/package.json": JSON.stringify({
name: "yargs",
version: "17.0.12",
exports: {
".": {
types: {
import: "./index.d.mts",
default: "./index.d.ts"
}
},
}
}),
"/Users/name/projects/web/tsconfig.json": JSON.stringify({
compilerOptions: {
moduleResolution: "nodenext",
forceConsistentCasingInFileNames: true,
traceResolution: true,
}
}),
[libFile.path]: libFile.content,
}, { currentDirectory: "/Users/name/projects/web" }),
changes: emptyArray,
});
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario: "self name package reference",
commandLineArgs: ["-w", "--explainFiles"],
sys: () => createWatchedSystem({
"/Users/name/projects/web/package.json": JSON.stringify({
name: "@this/package",
type: "module",
exports: {
".": "./dist/index.js"
}
}),
"/Users/name/projects/web/index.ts": Utils.dedent`
import * as me from "@this/package";
me.thing();
export function thing(): void {}
`,
"/Users/name/projects/web/tsconfig.json": JSON.stringify({
compilerOptions: {
module: "nodenext",
outDir: "./dist",
declarationDir: "./types",
composite: true,
forceConsistentCasingInFileNames: true,
traceResolution: true,
}
}),
"/a/lib/lib.esnext.full.d.ts": libFile.content,
}, { currentDirectory: "/Users/name/projects/web" }),
changes: emptyArray,
});
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario: "package json is looked up for file",
commandLineArgs: ["-w", "--explainFiles"],
sys: () => createWatchedSystem({
"/Users/name/projects/lib-boilerplate/package.json": JSON.stringify({
name: "lib-boilerplate",
version: "0.0.2",
type: "module",
exports: "./src/index.ts",
}),
"/Users/name/projects/lib-boilerplate/src/index.ts": Utils.dedent`
export function thing(): void {}
`,
"/Users/name/projects/lib-boilerplate/test/basic.spec.ts": Utils.dedent`
import { thing } from 'lib-boilerplate'
`,
"/Users/name/projects/lib-boilerplate/tsconfig.json": JSON.stringify({
compilerOptions: {
module: "node16",
target: "es2021",
forceConsistentCasingInFileNames: true,
traceResolution: true,
}
}),
"/a/lib/lib.es2021.full.d.ts": libFile.content,
}, { currentDirectory: "/Users/name/projects/lib-boilerplate" }),
changes: emptyArray,
});
});
}