Skip to content

Commit bccd176

Browse files
committed
Merge pull request microsoft#4383 from Microsoft/cleanHarnessOptionLoading
Clean harness option loading
2 parents 89718cf + c735a98 commit bccd176

File tree

396 files changed

+1385
-1662
lines changed

Some content is hidden

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

396 files changed

+1385
-1662
lines changed

src/harness/compilerRunner.ts

+2-30
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ class CompilerBaselineRunner extends RunnerBase {
4444
// Everything declared here should be cleared out in the "after" callback.
4545
let justName: string;
4646
let content: string;
47-
let testCaseContent: { settings: Harness.TestCaseParser.CompilerSetting[]; testUnitData: Harness.TestCaseParser.TestUnitData[]; };
47+
let testCaseContent: { settings: Harness.TestCaseParser.CompilerSettings; testUnitData: Harness.TestCaseParser.TestUnitData[]; };
4848

4949
let units: Harness.TestCaseParser.TestUnitData[];
50-
let tcSettings: Harness.TestCaseParser.CompilerSetting[];
50+
let tcSettings: Harness.TestCaseParser.CompilerSettings;
5151

5252
let lastUnit: Harness.TestCaseParser.TestUnitData;
5353
let rootDir: string;
@@ -61,15 +61,12 @@ class CompilerBaselineRunner extends RunnerBase {
6161
let otherFiles: { unitName: string; content: string }[];
6262
let harnessCompiler: Harness.Compiler.HarnessCompiler;
6363

64-
let createNewInstance = false;
65-
6664
before(() => {
6765
justName = fileName.replace(/^.*[\\\/]/, ""); // strips the fileName from the path.
6866
content = Harness.IO.readFile(fileName);
6967
testCaseContent = Harness.TestCaseParser.makeUnitsFromTest(content, fileName);
7068
units = testCaseContent.testUnitData;
7169
tcSettings = testCaseContent.settings;
72-
createNewInstance = false;
7370
lastUnit = units[units.length - 1];
7471
rootDir = lastUnit.originalFilePath.indexOf("conformance") === -1 ? "tests/cases/compiler/" : lastUnit.originalFilePath.substring(0, lastUnit.originalFilePath.lastIndexOf("/")) + "/";
7572
harnessCompiler = Harness.Compiler.getCompiler();
@@ -100,27 +97,6 @@ class CompilerBaselineRunner extends RunnerBase {
10097
});
10198
});
10299

103-
beforeEach(() => {
104-
/* The compiler doesn't handle certain flags flipping during a single compilation setting. Tests on these flags will need
105-
a fresh compiler instance for themselves and then create a fresh one for the next test. Would be nice to get dev fixes
106-
eventually to remove this limitation. */
107-
for (let i = 0; i < tcSettings.length; ++i) {
108-
// noImplicitAny is passed to getCompiler, but target is just passed in the settings blob to setCompilerSettings
109-
if (!createNewInstance && (tcSettings[i].flag == "noimplicitany" || tcSettings[i].flag === "target")) {
110-
harnessCompiler = Harness.Compiler.getCompiler();
111-
harnessCompiler.setCompilerSettings(tcSettings);
112-
createNewInstance = true;
113-
}
114-
}
115-
});
116-
117-
afterEach(() => {
118-
if (createNewInstance) {
119-
harnessCompiler = Harness.Compiler.getCompiler();
120-
createNewInstance = false;
121-
}
122-
});
123-
124100
after(() => {
125101
// Mocha holds onto the closure environment of the describe callback even after the test is done.
126102
// Therefore we have to clean out large objects after the test is done.
@@ -402,10 +378,6 @@ class CompilerBaselineRunner extends RunnerBase {
402378
else {
403379
this.tests.forEach(test => this.checkTestCodeOutput(test));
404380
}
405-
406-
describe("Cleanup after compiler baselines", () => {
407-
let harnessCompiler = Harness.Compiler.getCompiler();
408-
});
409381
});
410382
}
411383

src/harness/fourslash.ts

+13-87
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ module FourSlash {
2929
fileName: string;
3030
version: number;
3131
// File-specific options (name/value pairs)
32-
fileOptions: { [index: string]: string; };
32+
fileOptions: Harness.TestCaseParser.CompilerSettings;
3333
}
3434

3535
// Represents a set of parsed source files and options
3636
export interface FourSlashData {
3737
// Global options (name/value pairs)
38-
globalOptions: { [index: string]: string; };
38+
globalOptions: Harness.TestCaseParser.CompilerSettings;
3939

4040
files: FourSlashFile[];
4141

@@ -117,89 +117,17 @@ module FourSlash {
117117
// Add cases into convertGlobalOptionsToCompilationsSettings function for the compiler to acknowledge such option from meta data
118118
let metadataOptionNames = {
119119
baselineFile: "BaselineFile",
120-
declaration: "declaration",
121120
emitThisFile: "emitThisFile", // This flag is used for testing getEmitOutput feature. It allows test-cases to indicate what file to be output in multiple files project
122121
fileName: "Filename",
123-
mapRoot: "mapRoot",
124-
module: "module",
125-
out: "out",
126-
outFile: "outFile",
127-
outDir: "outDir",
128-
sourceMap: "sourceMap",
129-
sourceRoot: "sourceRoot",
130-
allowNonTsExtensions: "allowNonTsExtensions",
131122
resolveReference: "ResolveReference", // This flag is used to specify entry file for resolve file references. The flag is only allow once per test file
132-
jsx: "jsx",
133123
};
134124

135125
// List of allowed metadata names
136126
let fileMetadataNames = [metadataOptionNames.fileName, metadataOptionNames.emitThisFile, metadataOptionNames.resolveReference];
137-
let globalMetadataNames = [metadataOptionNames.allowNonTsExtensions, metadataOptionNames.baselineFile, metadataOptionNames.declaration,
138-
metadataOptionNames.mapRoot, metadataOptionNames.module, metadataOptionNames.out,metadataOptionNames.outFile,
139-
metadataOptionNames.outDir, metadataOptionNames.sourceMap, metadataOptionNames.sourceRoot, metadataOptionNames.jsx];
140127

141-
function convertGlobalOptionsToCompilerOptions(globalOptions: { [idx: string]: string }): ts.CompilerOptions {
128+
function convertGlobalOptionsToCompilerOptions(globalOptions: Harness.TestCaseParser.CompilerSettings): ts.CompilerOptions {
142129
let settings: ts.CompilerOptions = { target: ts.ScriptTarget.ES5 };
143-
// Convert all property in globalOptions into ts.CompilationSettings
144-
for (let prop in globalOptions) {
145-
if (globalOptions.hasOwnProperty(prop)) {
146-
switch (prop) {
147-
case metadataOptionNames.allowNonTsExtensions:
148-
settings.allowNonTsExtensions = globalOptions[prop] === "true";
149-
break;
150-
case metadataOptionNames.declaration:
151-
settings.declaration = globalOptions[prop] === "true";
152-
break;
153-
case metadataOptionNames.mapRoot:
154-
settings.mapRoot = globalOptions[prop];
155-
break;
156-
case metadataOptionNames.module:
157-
// create appropriate external module target for CompilationSettings
158-
switch (globalOptions[prop]) {
159-
case "AMD":
160-
settings.module = ts.ModuleKind.AMD;
161-
break;
162-
case "CommonJS":
163-
settings.module = ts.ModuleKind.CommonJS;
164-
break;
165-
default:
166-
ts.Debug.assert(globalOptions[prop] === undefined || globalOptions[prop] === "None");
167-
settings.module = ts.ModuleKind.None;
168-
break;
169-
}
170-
break;
171-
case metadataOptionNames.out:
172-
settings.out = globalOptions[prop];
173-
break;
174-
case metadataOptionNames.outFile:
175-
settings.outFile = globalOptions[prop];
176-
break;
177-
case metadataOptionNames.outDir:
178-
settings.outDir = globalOptions[prop];
179-
break;
180-
case metadataOptionNames.sourceMap:
181-
settings.sourceMap = globalOptions[prop] === "true";
182-
break;
183-
case metadataOptionNames.sourceRoot:
184-
settings.sourceRoot = globalOptions[prop];
185-
break;
186-
case metadataOptionNames.jsx:
187-
switch (globalOptions[prop].toLowerCase()) {
188-
case "react":
189-
settings.jsx = ts.JsxEmit.React;
190-
break;
191-
case "preserve":
192-
settings.jsx = ts.JsxEmit.Preserve;
193-
break;
194-
default:
195-
ts.Debug.assert(globalOptions[prop] === undefined || globalOptions[prop] === "None");
196-
settings.jsx = ts.JsxEmit.None;
197-
break;
198-
}
199-
break;
200-
}
201-
}
202-
}
130+
Harness.Compiler.setCompilerOptionsFromHarnessSetting(globalOptions, settings);
203131
return settings;
204132
}
205133

@@ -2514,12 +2442,16 @@ module FourSlash {
25142442
// Comment line, check for global/file @options and record them
25152443
let match = optionRegex.exec(line.substr(2));
25162444
if (match) {
2517-
let globalMetadataNamesIndex = globalMetadataNames.indexOf(match[1]);
25182445
let fileMetadataNamesIndex = fileMetadataNames.indexOf(match[1]);
2519-
if (globalMetadataNamesIndex === -1) {
2520-
if (fileMetadataNamesIndex === -1) {
2521-
throw new Error(`Unrecognized metadata name "${match[1]}". Available global metadata names are: ${globalMetadataNames.join(", ")}; file metadata names are: ${fileMetadataNames.join(", ")}`);
2522-
} else if (fileMetadataNamesIndex === fileMetadataNames.indexOf(metadataOptionNames.fileName)) {
2446+
if (fileMetadataNamesIndex === -1) {
2447+
// Check if the match is already existed in the global options
2448+
if (globalOptions[match[1]] !== undefined) {
2449+
throw new Error("Global Option : '" + match[1] + "' is already existed");
2450+
}
2451+
globalOptions[match[1]] = match[2];
2452+
}
2453+
else {
2454+
if (fileMetadataNamesIndex === fileMetadataNames.indexOf(metadataOptionNames.fileName)) {
25232455
// Found an @FileName directive, if this is not the first then create a new subfile
25242456
if (currentFileContent) {
25252457
let file = parseFileContent(currentFileContent, currentFileName, markerPositions, markers, ranges);
@@ -2540,12 +2472,6 @@ module FourSlash {
25402472
// Add other fileMetadata flag
25412473
currentFileOptions[match[1]] = match[2];
25422474
}
2543-
} else {
2544-
// Check if the match is already existed in the global options
2545-
if (globalOptions[match[1]] !== undefined) {
2546-
throw new Error("Global Option : '" + match[1] + "' is already existed");
2547-
}
2548-
globalOptions[match[1]] = match[2];
25492475
}
25502476
}
25512477
// TODO: should be '==='?

0 commit comments

Comments
 (0)