From 9fc4f6e1b9175f2649f4b14f46285ab889764285 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 8 Feb 2017 19:07:15 -0800 Subject: [PATCH] Write comments for compiler options in `tsc --init` output --- src/compiler/commandLineParser.ts | 71 ++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index d8a6f402a17..1a388c9e96a 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -868,7 +868,7 @@ namespace ts { /* @internal */ export function generateTSConfig(options: CompilerOptions, fileNames: string[]): string { const compilerOptions = extend(options, defaultInitCompilerOptions); - const configurations: any = { + const configurations: { compilerOptions: MapLike; files?: string[] } = { compilerOptions: serializeCompilerOptions(compilerOptions) }; if (fileNames && fileNames.length) { @@ -876,7 +876,8 @@ namespace ts { configurations.files = fileNames; } - return JSON.stringify(configurations, undefined, 4); + + return writeConfigurations(); function getCustomTypeMapOfCommandLineOption(optionDefinition: CommandLineOption): Map | undefined { if (optionDefinition.type === "string" || optionDefinition.type === "number" || optionDefinition.type === "boolean") { @@ -938,6 +939,72 @@ namespace ts { } return result; } + + function getDefaultValueForOption(option: CommandLineOption) { + switch (option.type) { + case "number": + return 1; + case "boolean": + return true; + case "string": + return option.isFilePath ? "./" : ""; + case "list": + return []; + case "object": + return {}; + default: + return arrayFrom((option).type.keys())[0]; + } + } + + function writeConfigurations() { + const categorizedOptions = reduceLeft( + filter(optionDeclarations, o => o.category !== "CommandLine" && o.category !== "Advanced"), + (memo, value) => { + if (value.category) { + (memo[value.category] || (memo[value.category] = [])).push(value); + } + return memo; + }, >{}); + const knownKesyCount = getOwnKeys(configurations.compilerOptions).length; + + const newLine = "\n"; + const tab = " "; + + let result = ""; + let seenKnownKeys = 0; + result += `{${newLine}`; + result += `${tab}"compilerOptions": {${newLine}`; + for (const category in categorizedOptions) { + result += `${tab}${tab}// ${category}${newLine}`; + result += `${newLine}`; + for (const option of categorizedOptions[category]) { + result += `${tab}${tab}// ${option.description && option.description.message || option.name}${newLine}`; + if (option.name in configurations.compilerOptions) { + result += `${tab}${tab}"${option.name}": ${JSON.stringify(configurations.compilerOptions[option.name])}${++seenKnownKeys === knownKesyCount ? "" : ","}${newLine}`; + } + else { + result += `${tab}${tab}// "${option.name}": ${JSON.stringify(getDefaultValueForOption(option))},${newLine}`; + } + result += `${newLine}`; + } + result += `${newLine}`; + } + if (configurations.files && configurations.files.length) { + result += `${tab}},${newLine}`; + result += `${tab}"files": [${newLine}`; + for (let i = 0; i < configurations.files.length; i++) { + result += `${tab}${tab}${JSON.stringify(configurations.files[i])}${i === configurations.files.length - 1 ? "" : ","}${newLine}`; + } + result += `${tab}]${newLine}`; + } + else { + result += `${tab}}${newLine}`; + } + result += `}${newLine}`; + + return result; + } } /**