Skip to content

Commit 60e25a7

Browse files
committed
use commandlineParser's optionDeclarations to parse compiler options instead of doing it explicitlly
1 parent b911dc3 commit 60e25a7

File tree

57 files changed

+127
-264
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+127
-264
lines changed

src/harness/harness.ts

+63-199
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,9 @@ module Harness {
838838
}
839839

840840
export function createSourceFileAndAssertInvariants(
841-
fileName: string,
842-
sourceText: string,
843-
languageVersion: ts.ScriptTarget) {
841+
fileName: string,
842+
sourceText: string,
843+
languageVersion: ts.ScriptTarget) {
844844
// We'll only assert inletiants outside of light mode.
845845
const shouldAssertInvariants = !Harness.lightMode;
846846

@@ -870,13 +870,13 @@ module Harness {
870870
}
871871

872872
export function createCompilerHost(
873-
inputFiles: { unitName: string; content: string; }[],
874-
writeFile: (fn: string, contents: string, writeByteOrderMark: boolean) => void,
875-
scriptTarget: ts.ScriptTarget,
876-
useCaseSensitiveFileNames: boolean,
877-
// the currentDirectory is needed for rwcRunner to passed in specified current directory to compiler host
878-
currentDirectory?: string,
879-
newLineKind?: ts.NewLineKind): ts.CompilerHost {
873+
inputFiles: { unitName: string; content: string; }[],
874+
writeFile: (fn: string, contents: string, writeByteOrderMark: boolean) => void,
875+
scriptTarget: ts.ScriptTarget,
876+
useCaseSensitiveFileNames: boolean,
877+
// the currentDirectory is needed for rwcRunner to passed in specified current directory to compiler host
878+
currentDirectory?: string,
879+
newLineKind?: ts.NewLineKind): ts.CompilerHost {
880880

881881
// Local get canonical file name function, that depends on passed in parameter for useCaseSensitiveFileNames
882882
function getCanonicalFileName(fileName: string): string {
@@ -894,7 +894,7 @@ module Harness {
894894
}
895895
};
896896
inputFiles.forEach(register);
897-
897+
898898
function getSourceFile(fn: string, languageVersion: ts.ScriptTarget) {
899899
fn = ts.normalizePath(fn);
900900
if (Object.prototype.hasOwnProperty.call(filemap, getCanonicalFileName(fn))) {
@@ -938,200 +938,64 @@ module Harness {
938938

939939
interface HarnesOptions {
940940
useCaseSensitiveFileNames?: boolean;
941-
includeBuiltFileNames?: string[];
941+
includeBuiltFile?: string;
942942
baselineFile?: string;
943943
}
944944

945+
// Additional options not already in ts.optionDeclarations
946+
const harnessOptionDeclarations: ts.CommandLineOption[] = [
947+
{ name: "allowNonTsExtensions", type: "boolean" },
948+
{ name: "useCaseSensitiveFileNames", type: "boolean" },
949+
{ name: "baselineFile", type: "string" },
950+
{ name: "includeBuiltFile", type: "string" },
951+
{ name: "fileName", type: "string" },
952+
{ name: "noErrorTruncation", type: "boolean" }
953+
];
954+
955+
let optionsIndex: ts.Map<ts.CommandLineOption>;
956+
function getCommandLineOption(name: string): ts.CommandLineOption {
957+
if (!optionsIndex) {
958+
optionsIndex = {};
959+
let optionDeclarations = harnessOptionDeclarations.concat(ts.optionDeclarations);
960+
for (let option of optionDeclarations) {
961+
optionsIndex[option.name.toLowerCase()] = option;
962+
}
963+
}
964+
return ts.lookUp(optionsIndex, name.toLowerCase());
965+
}
966+
945967
export function setCompilerOptionsFromHarnessSetting(settings: Harness.TestCaseParser.CompilerSettings, options: ts.CompilerOptions & HarnesOptions): void {
946968
for (let name in settings) {
947969
if (settings.hasOwnProperty(name)) {
948-
let value = settings[name] ? settings[name].toLowerCase() : settings[name];
949-
switch (name.toLowerCase()) {
950-
case "module":
951-
if (value === "amd") {
952-
options.module = ts.ModuleKind.AMD;
953-
} else if (value === "umd") {
954-
options.module = ts.ModuleKind.UMD;
955-
} else if (value === "commonjs") {
956-
options.module = ts.ModuleKind.CommonJS;
957-
} else if (value === "system") {
958-
options.module = ts.ModuleKind.System;
959-
} else if (value === "unspecified") {
960-
options.module = ts.ModuleKind.None;
961-
} else {
962-
throw new Error("Unknown module type " + value);
963-
}
964-
break;
965-
966-
case "target":
967-
if (value === "es3") {
968-
options.target = ts.ScriptTarget.ES3;
969-
} else if (value === "es5") {
970-
options.target = ts.ScriptTarget.ES5;
971-
} else if (value === "es6") {
972-
options.target = ts.ScriptTarget.ES6;
973-
} else {
974-
throw new Error("Unknown compile target " + value);
975-
}
976-
break;
977-
978-
case "experimentaldecorators":
979-
options.experimentalDecorators = value === "true";
980-
break;
981-
982-
case "emitdecoratormetadata":
983-
options.emitDecoratorMetadata = value === "true";
984-
break;
985-
986-
case "experimentalasyncfunctions":
987-
options.experimentalAsyncFunctions = value === "true";
988-
break;
989-
990-
case "noemithelpers":
991-
options.noEmitHelpers = value === "true";
992-
break;
993-
994-
case "noemitonerror":
995-
options.noEmitOnError = value === "true";
996-
break;
997-
998-
case "noresolve":
999-
options.noResolve = value === "true";
1000-
break;
1001-
1002-
case "noimplicitany":
1003-
options.noImplicitAny = value === "true";
1004-
break;
1005-
1006-
case "nolib":
1007-
options.noLib = value === "true";
1008-
break;
1009-
1010-
case "out":
1011-
options.out = settings[name];
1012-
break;
1013-
1014-
case "outfile":
1015-
options.outFile = settings[name];
1016-
break;
1017-
1018-
case "outdir":
1019-
options.outDir = settings[name];
1020-
break;
1021-
1022-
case "skipdefaultlibcheck":
1023-
options.skipDefaultLibCheck = value === "true";
1024-
break;
1025-
1026-
case "sourceroot":
1027-
options.sourceRoot = settings[name];
1028-
break;
1029-
1030-
case "maproot":
1031-
options.mapRoot = settings[name];
1032-
break;
1033-
1034-
case "sourcemap":
1035-
options.sourceMap = value === "true";
1036-
break;
1037-
1038-
case "declaration":
1039-
options.declaration = value === "true";
1040-
break;
1041-
1042-
case "newline":
1043-
if (value === "crlf") {
1044-
options.newLine = ts.NewLineKind.CarriageReturnLineFeed;
1045-
}
1046-
else if (value === "lf") {
1047-
options.newLine = ts.NewLineKind.LineFeed;
1048-
}
1049-
else {
1050-
throw new Error("Unknown option for newLine: " + value);
1051-
}
1052-
break;
1053-
1054-
case "comments":
1055-
options.removeComments = value === "false";
1056-
break;
1057-
1058-
case "stripinternal":
1059-
options.stripInternal = value === "true";
1060-
break;
1061-
1062-
case "usecasesensitivefilenames":
1063-
options.useCaseSensitiveFileNames = value === "true";
1064-
break;
1065-
1066-
case "filename":
1067-
// Not supported yet
1068-
break;
1069-
1070-
case "emitbom":
1071-
options.emitBOM = value === "true";
1072-
break;
1073-
1074-
case "errortruncation":
1075-
options.noErrorTruncation = value === "false";
1076-
break;
1077-
1078-
case "preserveconstenums":
1079-
options.preserveConstEnums = value === "true";
1080-
break;
1081-
1082-
case "isolatedmodules":
1083-
options.isolatedModules = value === "true";
1084-
break;
1085-
1086-
case "suppressimplicitanyindexerrors":
1087-
options.suppressImplicitAnyIndexErrors = value === "true";
1088-
break;
1089-
1090-
case "includebuiltfile":
1091-
if (!options.includeBuiltFileNames) {
1092-
options.includeBuiltFileNames = [];
1093-
}
1094-
options.includeBuiltFileNames.push(settings[name]);
1095-
break;
1096-
1097-
case "inlinesourcemap":
1098-
options.inlineSourceMap = value === "true";
1099-
break;
1100-
1101-
case "inlinesources":
1102-
options.inlineSources = value === "true";
1103-
break;
1104-
1105-
case "jsx":
1106-
if (value === "react") {
1107-
options.jsx = ts.JsxEmit.React;
1108-
}
1109-
else if (value === "preserve") {
1110-
options.jsx = ts.JsxEmit.Preserve;
1111-
}
1112-
else if (value === "none") {
1113-
options.jsx = ts.JsxEmit.None;
1114-
}
1115-
else {
1116-
throw new Error("Unknown option for jsx: " + value);
1117-
}
1118-
break;
1119-
1120-
case "allownontsextensions":
1121-
options.allowNonTsExtensions = value === "true";
1122-
break;
1123-
1124-
case "baselinefile":
1125-
options.baselineFile = settings[name];
1126-
break;
1127-
1128-
default:
1129-
throw new Error("Unsupported compiler setting " + value);
970+
let value = settings[name];
971+
let option = getCommandLineOption(name);
972+
if (option) {
973+
switch (option.type) {
974+
case "boolean":
975+
options[option.name] = value.toLowerCase() === "true";
976+
break;
977+
case "string":
978+
options[option.name] = value;
979+
break;
980+
// If not a primitive, the possible types are specified in what is effectively a map of options.
981+
default:
982+
let map = <ts.Map<number>>option.type;
983+
let key = (value).toLowerCase();
984+
if (ts.hasProperty(map, key)) {
985+
options[option.name] = map[key];
986+
}
987+
else {
988+
throw new Error(`Unkown value '${value}' for compiler option '${name}'.`);
989+
}
990+
}
991+
}
992+
else {
993+
throw new Error(`Unkown compiler option '${name}'.`);
1130994
}
1131995
}
1132996
}
1133997
}
1134-
998+
1135999
export class HarnessCompiler {
11361000
private inputFiles: { unitName: string; content: string }[] = [];
11371001
private compileOptions: ts.CompilerOptions;
@@ -1194,24 +1058,24 @@ module Harness {
11941058
options.module = options.module || ts.ModuleKind.None;
11951059
options.newLine = options.newLine || ts.NewLineKind.CarriageReturnLineFeed;
11961060
options.noErrorTruncation = true;
1061+
options.skipDefaultLibCheck = true;
11971062

11981063
if (settingsCallback) {
11991064
settingsCallback(null);
12001065
}
12011066

12021067
let newLine = "\r\n";
1203-
options.skipDefaultLibCheck = true;
12041068

12051069
// Parse settings
12061070
setCompilerOptionsFromHarnessSetting(this.settings, options);
12071071

12081072
// Files from built\local that are requested by test "@includeBuiltFiles" to be in the context.
12091073
// Treat them as library files, so include them in build, but not in baselines.
12101074
let includeBuiltFiles: { unitName: string; content: string }[] = [];
1211-
ts.forEach(options.includeBuiltFileNames, fileName => {
1212-
let builtFileName = libFolder + fileName;
1075+
if (options.includeBuiltFile) {
1076+
let builtFileName = libFolder + options.includeBuiltFile;
12131077
includeBuiltFiles.push({ unitName: builtFileName, content: normalizeLineEndings(IO.readFile(builtFileName), newLine) });
1214-
});
1078+
}
12151079

12161080
let useCaseSensitiveFileNames = options.useCaseSensitiveFileNames !== undefined ? options.useCaseSensitiveFileNames : Harness.IO.useCaseSensitiveFileNames();
12171081

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// @comments:true
1+
// @removeComments: false
22
function foo(/** nothing */) {
33
}

tests/cases/compiler/commentOnClassMethod1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @comments:true
1+
// @removeComments: false
22
class WebControls {
33
/**
44
* Render a control
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// @comments: true
1+
// @removeComments: false
22

33
1 + 1; // Comment.

tests/cases/compiler/commentOnIfStatement1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @comments: true
1+
// @removeComments: false
22

33
// Test
44
if (true) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//@module: amd
2-
// @comments: true
1+
// @module: amd
2+
// @removeComments: false
33
/* Copyright */
44

55
import foo = require('./foo');
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@module: commonjs
2-
// @comments: true
1+
// @module: commonjs
2+
// @removeComments: false
33
/* not copyright */
44
import foo = require('./foo');

tests/cases/compiler/commentOnImportStatement3.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//@module: commonjs
2-
// @comments: true
1+
// @module: commonjs
2+
// @removeComments: false
33
/* copyright */
44

55
/* not copyright */

tests/cases/compiler/commentOnSimpleArrowFunctionBody1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @comments: true
1+
// @removeComments: false
22
function Foo(x: any)
33
{
44
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @comments:true
1+
// @removeComments: false
22
var v = {
33
f: /**own f*/ (a) => 0
44
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@module: amd
2-
// @comments: true
1+
// @module: amd
2+
// @removeComments: false
33
/** b's comment*/
44
export var b: number;

tests/cases/compiler/commentsClass.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @target: ES5
22
// @declaration: true
3-
// @comments: true
3+
// @removeComments: false
44

55
/** This is class c2 without constuctor*/
66
class c2 {

0 commit comments

Comments
 (0)