Removes trailing comma logic and fixes default values

This commit is contained in:
Tingan Ho
2015-07-29 10:26:18 +08:00
parent 5daf3f1101
commit db6e46df12
7 changed files with 275 additions and 273 deletions
+28 -42
View File
@@ -386,49 +386,35 @@ namespace ts {
continue;
}
if(ch === CharacterCodes.slash) {
if (text.charCodeAt(pos + 1) === CharacterCodes.slash) {
pos += 2;
while (pos <= end) {
if (isLineBreak(text.charCodeAt(pos))) {
break;
}
pos++;
}
continue;
}
else if (text.charCodeAt(pos + 1) === CharacterCodes.asterisk) {
pos += 2;
while (pos <= end) {
ch = text.charCodeAt(pos);
if (ch === CharacterCodes.asterisk &&
text.charCodeAt(pos + 1) === CharacterCodes.slash) {
pos += 2;
break;
}
pos++;
}
continue;
}
}
if (pendingCommaInsertion) {
if (ch !== CharacterCodes.closeBracket &&
ch !== CharacterCodes.closeBrace) {
result += ',';
}
pendingCommaInsertion = false;
}
switch (ch) {
case CharacterCodes.comma:
pendingCommaInsertion = true;
break;
case CharacterCodes.slash:
if (text.charCodeAt(pos + 1) === CharacterCodes.slash) {
pos += 2;
while (pos <= end) {
if (isLineBreak(text.charCodeAt(pos))) {
break;
}
pos++;
}
break;
}
else if (text.charCodeAt(pos + 1) === CharacterCodes.asterisk) {
pos += 2;
while (pos <= end) {
ch = text.charCodeAt(pos);
if (ch === CharacterCodes.asterisk &&
text.charCodeAt(pos + 1) === CharacterCodes.slash) {
pos += 2;
break;
}
pos++;
}
break;
}
case CharacterCodes.doubleQuote:
result += text[pos];
-184
View File
@@ -3,190 +3,6 @@
/* @internal */
namespace ts {
export const defaultInitCompilerOptions: CompilerOptions = {
module: ModuleKind.CommonJS,
target: ScriptTarget.ES3,
noImplicitAny: true,
outDir: "built",
rootDir: ".",
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();
writeTsConfigDotJsonFile();
function writeTsConfigDotJsonFile() {
write("{");
writeLine();
increaseIndent();
writeCompilerOptions();
if (fileNames && fileNames.length > 0) {
write(",");
writeLine();
writeFileNames();
}
if (excludes) {
write(",");
writeLine();
writeExcludeOptions();
}
writeLine();
decreaseIndent();
write("}");
}
function writeCompilerOptions() {
write(`"compilerOptions": {`);
writeLine();
increaseIndent();
for (var option in compilerOptions) {
switch (option) {
case "init":
case "watch":
case "help":
case "version":
continue;
case "module":
case "target":
case "newLine":
writeComplexCompilerOption(option);
break;
default:
writeSimpleCompilerOption(option);
}
}
decreaseIndent();
write("}");
}
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) {
writeOptionalOptionDescription(option);
write(`"${option}": `);
if (typeof compilerOptions[option] === "string") {
write(`"${compilerOptions[option]}",`);
writeLine();
}
else {
if (compilerOptions[option]) {
write("true,");
}
else {
write("false,");
}
writeLine();
}
}
function writeComplexCompilerOption(option: string) {
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}",`);
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}",`);
writeLine();
break;
case "newLine":
var newlineValue: string;
switch (compilerOptions.newLine) {
case NewLineKind.CarriageReturnLineFeed:
newlineValue = "CRLF";
break;
default:
newlineValue = "LF";
break;
}
write(`"newLine": "${newlineValue}",`);
writeLine();
break;
}
}
function writeFileNames() {
write(`"files": [`);
writeLine();
increaseIndent();
for (let fileName of fileNames) {
write(`"${fileName}",`);
writeLine();
}
decreaseIndent();
write("]");
}
function writeExcludeOptions() {
write(`"exclude": [`);
writeLine();
increaseIndent();
for (let exclude of excludes) {
write(`"${exclude}",`);
writeLine();
}
decreaseIndent();
write("]");
}
}
export function isExternalModuleOrDeclarationFile(sourceFile: SourceFile) {
return isExternalModule(sourceFile) || isDeclarationFile(sourceFile);
}
+214 -4
View File
@@ -1,5 +1,6 @@
/// <reference path="sys.ts" />
/// <reference path="emitter.ts" />
/// <reference path="core.ts" />
namespace ts {
/* @internal */ export let programTime = 0;
@@ -26,10 +27,219 @@ namespace ts {
return undefined;
}
export function writeConfigFile(file: string, compilerOptions: CompilerOptions, fileNames: string[]): void {
let writer = createTextWriter("\n");
buildConfigFile(writer, compilerOptions, fileNames, ["node_modules"]);
sys.writeFile(file, writer.getText());
export const defaultInitCompilerOptions: CompilerOptions = {
module: ModuleKind.CommonJS,
target: ScriptTarget.ES3,
noImplicitAny: false,
outDir: "built",
rootDir: ".",
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("}");
}
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("}");
}
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 createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost {
+3 -1
View File
@@ -150,7 +150,9 @@ namespace ts {
reportDiagnostic(createCompilerDiagnostic(Diagnostics.You_already_have_a_tsconfig_json_file_defined));
}
else {
writeConfigFile(file, commandLine.options, commandLine.fileNames);
let writer = createTextWriter("\n");
buildConfigFile(writer, compilerOptions, commandLine.fileNames, ["node_modules"]);
sys.writeFile(file, writer.getText());
}
return sys.exit(ExitStatus.Success);
}