Addresses CR feedback

This commit is contained in:
Tingan Ho
2015-08-25 15:05:02 +08:00
parent 07fb139326
commit 963ba1918e
5 changed files with 78 additions and 354 deletions

View File

@@ -36,209 +36,38 @@ namespace ts {
sourceMap: false,
}
export function buildConfigFile(writer: EmitTextWriter, compilerOptions: CompilerOptions, fileNames?: string[], excludes?: string[]) {
compilerOptions = extend(compilerOptions, defaultInitCompilerOptions);
let { write, writeLine, increaseIndent, decreaseIndent } = writer;
let { optionNameMap } = getOptionNameMap();
writeConfigFile();
function writeConfigFile() {
write("{");
writeLine();
increaseIndent();
writeCompilerOptions();
if (fileNames && fileNames.length > 0) {
write(",");
writeLine();
writeFileNames();
}
if (excludes) {
write(",");
writeLine();
writeExcludeOptions();
}
writeLine();
decreaseIndent();
write("}");
export function scriptTargetToString(target: ScriptTarget): string {
switch (target) {
case ScriptTarget.ES5:
return "es5";
case ScriptTarget.ES6:
return "es6";
default:
return "es3";
}
}
function writeCompilerOptions() {
write(`"compilerOptions": {`);
writeLine();
increaseIndent();
let length = 0;
for (var option in compilerOptions) {
length++;
}
let i = 0;
for (var option in compilerOptions) {
switch (option) {
case "init":
case "watch":
case "help":
case "version":
i++;
continue;
case "module":
case "target":
case "newLine":
writeComplexCompilerOption(option, i < length - 1);
break;
default:
writeSimpleCompilerOption(option, i < length - 1);
}
i++;
}
decreaseIndent();
write("}");
export function moduleKindToString(kind: ModuleKind): string {
switch (kind) {
case ModuleKind.None:
return undefined;
case ModuleKind.CommonJS:
return "commonjs";
case ModuleKind.System:
return "system";
case ModuleKind.UMD:
return "umd";
default:
return "amd";
}
}
function writeOptionalOptionDescription(option: string) {
option = option.toLowerCase();
if (optionNameMap[option].description &&
optionNameMap[option].description.key) {
write(`// ${optionNameMap[option].description.key}`);
writeLine();
}
}
/**
* Write simple compiler option. A simple compiler option is an option
* with boolean or non string set value.
*/
function writeSimpleCompilerOption(option: string, writeComma: boolean) {
writeOptionalOptionDescription(option);
write(`"${option}": `);
if (typeof compilerOptions[option] === "string") {
write(`"${compilerOptions[option]}"`);
}
else {
if (compilerOptions[option]) {
write("true");
}
else {
write("false");
}
}
if (writeComma) {
write(",");
}
writeLine();
}
/**
* Write complex compiler option. A complex compiler option is an option
* which maps to a TypeScript enum type.
*/
function writeComplexCompilerOption(option: string, writeComma: boolean) {
writeOptionalOptionDescription(option);
outer: switch (option) {
case "module":
var moduleValue: string;
switch (compilerOptions.module) {
case ModuleKind.None:
break outer;
case ModuleKind.CommonJS:
moduleValue = "commonjs";
break;
case ModuleKind.System:
moduleValue = "system";
break;
case ModuleKind.UMD:
moduleValue = "umd";
break;
default:
moduleValue = "amd";
break;
}
write(`"module": "${moduleValue}"`);
if (writeComma) {
write(",");
}
writeLine();
break;
case "target":
var targetValue: string;
switch (compilerOptions.target) {
case ScriptTarget.ES5:
targetValue = "es5";
break;
case ScriptTarget.ES6:
targetValue = "es6";
break;
default:
targetValue = "es3";
break;
}
write(`"target": "${targetValue}"`);
if (writeComma) {
write(",");
}
writeLine();
break;
case "newLine":
var newlineValue: string;
switch (compilerOptions.newLine) {
case NewLineKind.CarriageReturnLineFeed:
newlineValue = "CRLF";
break;
default:
newlineValue = "LF";
break;
}
write(`"newLine": "${newlineValue}"`);
if (writeComma) {
write(",");
}
writeLine();
break;
}
}
function writeFileNames() {
write(`"files": [`);
writeLine();
increaseIndent();
forEach(fileNames, (fileName, index) => {
write(`"${fileName}"`);
if (index < fileNames.length - 1) {
write(",");
}
writeLine();
});
decreaseIndent();
write("]");
}
function writeExcludeOptions() {
write(`"exclude": [`);
writeLine();
increaseIndent();
forEach(excludes, (exclude, index) => {
write(`"${exclude}"`);
if (index < excludes.length - 1) {
write(",");
}
writeLine();
});
decreaseIndent();
write("]");
export function newLineKindToString(kind: NewLineKind): string {
switch (kind) {
case NewLineKind.CarriageReturnLineFeed:
return "CRLF";
default:
return "LF";
}
}