/
showConfig.ts
193 lines (175 loc) · 8.29 KB
/
showConfig.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
namespace ts {
describe("unittests:: config:: showConfig", () => {
function showTSConfigCorrectly(name: string, commandLinesArgs: string[], configJson?: object) {
describe(name, () => {
const outputFileName = `config/showConfig/${name.replace(/[^a-z0-9\-./ ]/ig, "")}/tsconfig.json`;
it(`Correct output for ${outputFileName}`, () => {
const cwd = `/${name}`;
const configPath = combinePaths(cwd, "tsconfig.json");
const configContents = configJson ? JSON.stringify(configJson) : undefined;
const configParseHost: ParseConfigFileHost = {
fileExists: path =>
comparePaths(getNormalizedAbsolutePath(path, cwd), configPath) === Comparison.EqualTo ? true : false,
getCurrentDirectory() { return cwd; },
useCaseSensitiveFileNames: true,
onUnRecoverableConfigFileDiagnostic: d => {
throw new Error(flattenDiagnosticMessageText(d.messageText, "\n"));
},
readDirectory() { return []; },
readFile: path =>
comparePaths(getNormalizedAbsolutePath(path, cwd), configPath) === Comparison.EqualTo ? configContents : undefined,
};
let commandLine = parseCommandLine(commandLinesArgs);
if (commandLine.options.project) {
const result = getParsedCommandLineOfConfigFile(commandLine.options.project, commandLine.options, configParseHost);
if (result) {
commandLine = result;
}
}
const initResult = convertToTSConfig(commandLine, configPath, configParseHost);
// eslint-disable-next-line no-null/no-null
Harness.Baseline.runBaseline(outputFileName, JSON.stringify(initResult, null, 4) + "\n");
});
});
}
showTSConfigCorrectly("Default initialized TSConfig", ["--showConfig"]);
showTSConfigCorrectly("Show TSConfig with files options", ["--showConfig", "file0.ts", "file1.ts", "file2.ts"]);
showTSConfigCorrectly("Show TSConfig with boolean value compiler options", ["--showConfig", "--noUnusedLocals"]);
showTSConfigCorrectly("Show TSConfig with enum value compiler options", ["--showConfig", "--target", "es5", "--jsx", "react"]);
showTSConfigCorrectly("Show TSConfig with list compiler options", ["--showConfig", "--types", "jquery,mocha"]);
showTSConfigCorrectly("Show TSConfig with list compiler options with enum value", ["--showConfig", "--lib", "es5,es2015.core"]);
showTSConfigCorrectly("Show TSConfig with incorrect compiler option", ["--showConfig", "--someNonExistOption"]);
showTSConfigCorrectly("Show TSConfig with incorrect compiler option value", ["--showConfig", "--lib", "nonExistLib,es5,es2015.promise"]);
showTSConfigCorrectly("Show TSConfig with advanced options", ["--showConfig", "--declaration", "--declarationDir", "lib", "--skipLibCheck", "--noErrorTruncation"]);
showTSConfigCorrectly("Show TSConfig with compileOnSave and more", ["-p", "tsconfig.json"], {
compilerOptions: {
esModuleInterop: true,
target: "es5",
module: "commonjs",
strict: true,
},
compileOnSave: true,
exclude: [
"dist"
],
files: [],
include: [
"src/*"
],
references: [
{ path: "./test" }
],
});
// Regression test for https://github.com/Microsoft/TypeScript/issues/28836
showTSConfigCorrectly("Show TSConfig with paths and more", ["-p", "tsconfig.json"], {
compilerOptions: {
allowJs: true,
outDir: "./lib",
esModuleInterop: true,
module: "commonjs",
moduleResolution: "node",
target: "ES2017",
sourceMap: true,
baseUrl: ".",
paths: {
"@root/*": ["./*"],
"@configs/*": ["src/configs/*"],
"@common/*": ["src/common/*"],
"*": [
"node_modules/*",
"src/types/*"
]
},
experimentalDecorators: true,
emitDecoratorMetadata: true,
resolveJsonModule: true
},
include: [
"./src/**/*"
]
});
showTSConfigCorrectly("Show TSConfig with watch options", ["-p", "tsconfig.json"], {
watchOptions: {
watchFile: "DynamicPriorityPolling"
},
include: [
"./src/**/*"
]
});
// Bulk validation of all option declarations
for (const option of optionDeclarations) {
baselineOption(option, /*isCompilerOptions*/ true);
}
for (const option of optionsForWatch) {
baselineOption(option, /*isCompilerOptions*/ false);
}
function baselineOption(option: CommandLineOption, isCompilerOptions: boolean) {
if (option.name === "project") return;
let args: string[];
let optionValue: object | undefined;
switch (option.type) {
case "boolean": {
if (option.isTSConfigOnly) {
args = ["-p", "tsconfig.json"];
optionValue = { [option.name]: true };
}
else {
args = [`--${option.name}`];
}
break;
}
case "list": {
if (option.isTSConfigOnly) {
args = ["-p", "tsconfig.json"];
optionValue = { [option.name]: [] };
}
else {
args = [`--${option.name}`];
}
break;
}
case "string": {
if (option.isTSConfigOnly) {
args = ["-p", "tsconfig.json"];
optionValue = { [option.name]: "someString" };
}
else {
args = [`--${option.name}`, "someString"];
}
break;
}
case "number": {
if (option.isTSConfigOnly) {
args = ["-p", "tsconfig.json"];
optionValue = { [option.name]: 0 };
}
else {
args = [`--${option.name}`, "0"];
}
break;
}
case "object": {
args = ["-p", "tsconfig.json"];
optionValue = { [option.name]: {} };
break;
}
default: {
const iterResult = option.type.keys().next();
if (iterResult.done) return Debug.fail("Expected 'option.type' to have entries");
const val = iterResult.value;
if (option.isTSConfigOnly) {
args = ["-p", "tsconfig.json"];
optionValue = { [option.name]: val };
}
else {
args = [`--${option.name}`, val];
}
break;
}
}
const configObject = optionValue &&
(isCompilerOptions ? { compilerOptions: optionValue } : { watchOptions: optionValue });
showTSConfigCorrectly(`Shows tsconfig for single option/${option.name}`, args, configObject);
}
});
}