mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
Use same parsing function for fourslash tests
This commit is contained in:
@@ -29,13 +29,13 @@ module FourSlash {
|
||||
fileName: string;
|
||||
version: number;
|
||||
// File-specific options (name/value pairs)
|
||||
fileOptions: { [index: string]: string; };
|
||||
fileOptions: Harness.TestCaseParser.CompilerSettings;
|
||||
}
|
||||
|
||||
// Represents a set of parsed source files and options
|
||||
export interface FourSlashData {
|
||||
// Global options (name/value pairs)
|
||||
globalOptions: { [index: string]: string; };
|
||||
globalOptions: Harness.TestCaseParser.CompilerSettings;
|
||||
|
||||
files: FourSlashFile[];
|
||||
|
||||
@@ -117,70 +117,18 @@ module FourSlash {
|
||||
// Add cases into convertGlobalOptionsToCompilationsSettings function for the compiler to acknowledge such option from meta data
|
||||
let metadataOptionNames = {
|
||||
baselineFile: "BaselineFile",
|
||||
declaration: "declaration",
|
||||
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
|
||||
fileName: "Filename",
|
||||
mapRoot: "mapRoot",
|
||||
module: "module",
|
||||
out: "out",
|
||||
outDir: "outDir",
|
||||
sourceMap: "sourceMap",
|
||||
sourceRoot: "sourceRoot",
|
||||
allowNonTsExtensions: "allowNonTsExtensions",
|
||||
resolveReference: "ResolveReference", // This flag is used to specify entry file for resolve file references. The flag is only allow once per test file
|
||||
};
|
||||
|
||||
// List of allowed metadata names
|
||||
let fileMetadataNames = [metadataOptionNames.fileName, metadataOptionNames.emitThisFile, metadataOptionNames.resolveReference];
|
||||
let globalMetadataNames = [metadataOptionNames.allowNonTsExtensions, metadataOptionNames.baselineFile, metadataOptionNames.declaration,
|
||||
metadataOptionNames.mapRoot, metadataOptionNames.module, metadataOptionNames.out,
|
||||
metadataOptionNames.outDir, metadataOptionNames.sourceMap, metadataOptionNames.sourceRoot];
|
||||
|
||||
function convertGlobalOptionsToCompilerOptions(globalOptions: { [idx: string]: string }): ts.CompilerOptions {
|
||||
let settings: ts.CompilerOptions = { target: ts.ScriptTarget.ES5 };
|
||||
// Convert all property in globalOptions into ts.CompilationSettings
|
||||
for (let prop in globalOptions) {
|
||||
if (globalOptions.hasOwnProperty(prop)) {
|
||||
switch (prop) {
|
||||
case metadataOptionNames.allowNonTsExtensions:
|
||||
settings.allowNonTsExtensions = globalOptions[prop] === "true";
|
||||
break;
|
||||
case metadataOptionNames.declaration:
|
||||
settings.declaration = globalOptions[prop] === "true";
|
||||
break;
|
||||
case metadataOptionNames.mapRoot:
|
||||
settings.mapRoot = globalOptions[prop];
|
||||
break;
|
||||
case metadataOptionNames.module:
|
||||
// create appropriate external module target for CompilationSettings
|
||||
switch (globalOptions[prop]) {
|
||||
case "AMD":
|
||||
settings.module = ts.ModuleKind.AMD;
|
||||
break;
|
||||
case "CommonJS":
|
||||
settings.module = ts.ModuleKind.CommonJS;
|
||||
break;
|
||||
default:
|
||||
ts.Debug.assert(globalOptions[prop] === undefined || globalOptions[prop] === "None");
|
||||
settings.module = ts.ModuleKind.None;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case metadataOptionNames.out:
|
||||
settings.out = globalOptions[prop];
|
||||
break;
|
||||
case metadataOptionNames.outDir:
|
||||
settings.outDir = globalOptions[prop];
|
||||
break;
|
||||
case metadataOptionNames.sourceMap:
|
||||
settings.sourceMap = globalOptions[prop] === "true";
|
||||
break;
|
||||
case metadataOptionNames.sourceRoot:
|
||||
settings.sourceRoot = globalOptions[prop];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Harness.Compiler.setCompilerOptionForSetting(globalOptions, settings);
|
||||
return settings;
|
||||
}
|
||||
|
||||
@@ -2495,12 +2443,16 @@ module FourSlash {
|
||||
// Comment line, check for global/file @options and record them
|
||||
let match = optionRegex.exec(line.substr(2));
|
||||
if (match) {
|
||||
let globalMetadataNamesIndex = globalMetadataNames.indexOf(match[1]);
|
||||
let fileMetadataNamesIndex = fileMetadataNames.indexOf(match[1]);
|
||||
if (globalMetadataNamesIndex === -1) {
|
||||
if (fileMetadataNamesIndex === -1) {
|
||||
throw new Error(`Unrecognized metadata name "${match[1]}". Available global metadata names are: ${globalMetadataNames.join(", ")}; file metadata names are: ${fileMetadataNames.join(", ")}`);
|
||||
} else if (fileMetadataNamesIndex === fileMetadataNames.indexOf(metadataOptionNames.fileName)) {
|
||||
if (fileMetadataNamesIndex === -1) {
|
||||
// Check if the match is already existed in the global options
|
||||
if (globalOptions[match[1]] !== undefined) {
|
||||
throw new Error("Global Option : '" + match[1] + "' is already existed");
|
||||
}
|
||||
globalOptions[match[1]] = match[2];
|
||||
}
|
||||
else {
|
||||
if (fileMetadataNamesIndex === fileMetadataNames.indexOf(metadataOptionNames.fileName)) {
|
||||
// Found an @FileName directive, if this is not the first then create a new subfile
|
||||
if (currentFileContent) {
|
||||
let file = parseFileContent(currentFileContent, currentFileName, markerPositions, markers, ranges);
|
||||
@@ -2521,12 +2473,6 @@ module FourSlash {
|
||||
// Add other fileMetadata flag
|
||||
currentFileOptions[match[1]] = match[2];
|
||||
}
|
||||
} else {
|
||||
// Check if the match is already existed in the global options
|
||||
if (globalOptions[match[1]] !== undefined) {
|
||||
throw new Error("Global Option : '" + match[1] + "' is already existed");
|
||||
}
|
||||
globalOptions[match[1]] = match[2];
|
||||
}
|
||||
}
|
||||
// TODO: should be '==='?
|
||||
|
||||
@@ -913,6 +913,7 @@ module Harness {
|
||||
interface HarnesOptions {
|
||||
useCaseSensitiveFileNames?: boolean;
|
||||
includeBuiltFileNames?: string[];
|
||||
baselineFile?: string;
|
||||
}
|
||||
|
||||
export function setCompilerOptionForSetting(settings: Harness.TestCaseParser.CompilerSettings, options: ts.CompilerOptions & HarnesOptions): void {
|
||||
@@ -1086,6 +1087,14 @@ module Harness {
|
||||
}
|
||||
break;
|
||||
|
||||
case "allownontsextensions":
|
||||
options.allowNonTsExtensions = value === "true";
|
||||
break;
|
||||
|
||||
case "baselinefile":
|
||||
options.baselineFile = settings[name];
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error("Unsupported compiler setting " + value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user