Skip to content

Commit be4133a

Browse files
author
Yui T
committed
Add meta-data flag name to modify compilationSettings
1 parent 6ef41a7 commit be4133a

File tree

4 files changed

+52
-39
lines changed

4 files changed

+52
-39
lines changed

src/harness/fourslash.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,47 @@ module FourSlash {
122122
return s.replace(/[&<>"'\/]/g, ch => entityMap[ch]);
123123
}
124124

125+
// Name of ts.CompilerOptions properties that will be used by globalOptions
126+
// To add additional option, add property into the compilerOptMetadataNames, refer the property in either globalMetadataNames or fileMetadataNames
127+
// Add cases into convertGlobalOptionsToCompilationsSettings function for the compiler to acknowledge such option from meta data
128+
var compilerOptMetadataNames = {
129+
out: 'out',
130+
outDir: 'outDir',
131+
declaration: 'declaration',
132+
sourceMap: 'sourceMap',
133+
sourceRoot: 'sourceRoot'
134+
};
135+
125136
// List of allowed metadata names
126137
var fileMetadataNames = ['Filename'];
127-
var globalMetadataNames = ['Module', 'Target', 'BaselineFile']; // Note: Only BaselineFile is actually supported at the moment
138+
var globalMetadataNames = ['BaselineFile', compilerOptMetadataNames.out, compilerOptMetadataNames.outDir, compilerOptMetadataNames.declaration, compilerOptMetadataNames.outDir,
139+
compilerOptMetadataNames.declaration, compilerOptMetadataNames.sourceMap, compilerOptMetadataNames.sourceRoot]
140+
141+
function convertGlobalOptionsToCompilationSettings(globalOptions: { [idx: string]: string }): ts.CompilationSettings {
142+
var settings: ts.CompilationSettings = {};
143+
// Convert all property in globalOptions into ts.CompilationSettings
144+
for (var prop in globalOptions) {
145+
if (globalOptions.hasOwnProperty(prop)) {
146+
switch (prop) {
147+
case compilerOptMetadataNames.out:
148+
settings.outFileOption = globalOptions[prop];
149+
break;
150+
case compilerOptMetadataNames.outDir:
151+
settings.outDirOption = globalOptions[prop];
152+
break;
153+
case compilerOptMetadataNames.declaration:
154+
settings.generateDeclarationFiles = true;
155+
break;
156+
case compilerOptMetadataNames.sourceMap:
157+
settings.mapSourceFiles = true;
158+
break;
159+
case compilerOptMetadataNames.sourceRoot:
160+
settings.sourceRoot = globalOptions[prop]
161+
}
162+
}
163+
}
164+
return settings;
165+
}
128166

129167
export var currentTestState: TestState = null;
130168

@@ -189,12 +227,6 @@ module FourSlash {
189227
// Whether or not we should format on keystrokes
190228
public enableFormatting = true;
191229

192-
// Whether or not to generate .d.ts file
193-
public enableDeclaration = false;
194-
195-
// Output filename for single-output-file option
196-
public singleOutputFilename: string = undefined;
197-
198230
public formatCodeOptions: ts.FormatCodeOptions;
199231

200232
public cancellationToken: TestCancellationToken;
@@ -205,11 +237,15 @@ module FourSlash {
205237
private scenarioActions: string[] = [];
206238
private taoInvalidReason: string = null;
207239

240+
208241
constructor(public testData: FourSlashData) {
209242
// Initialize the language service with all the scripts
210243
this.cancellationToken = new TestCancellationToken();
211244
this.languageServiceShimHost = new Harness.LanguageService.TypeScriptLS(this.cancellationToken);
212245

246+
var compilationSettings = convertGlobalOptionsToCompilationSettings(this.testData.globalOptions);
247+
this.languageServiceShimHost.setCompilationSettings(compilationSettings);
248+
213249
var inputFiles: { unitName: string; content: string }[] = [];
214250

215251
testData.files.forEach(file => {
@@ -226,9 +262,9 @@ module FourSlash {
226262
//if (/require\(/.test(lastFile.content) || /reference\spath/.test(lastFile.content)) {
227263
// inputFiles.push({ unitName: lastFile.fileName, content: lastFile.content });
228264
//} else {
229-
inputFiles = testData.files.map(file => {
230-
return { unitName: file.fileName, content: file.content };
231-
});
265+
inputFiles = testData.files.map(file => {
266+
return { unitName: file.fileName, content: file.content };
267+
});
232268
//}
233269

234270

@@ -459,14 +495,6 @@ module FourSlash {
459495
}
460496

461497
public verifyEmitOutput(state: ts.EmitReturnStatus, filename?: string) {
462-
if (this.enableDeclaration) {
463-
this.languageServiceShimHost.setCompilationSettings({ generateDeclarationFiles: true });
464-
}
465-
466-
if (this.singleOutputFilename !== undefined) {
467-
this.languageServiceShimHost.setCompilationSettings({ outFileOption: this.singleOutputFilename });
468-
}
469-
470498
var expectedFilenames:string[] = [];
471499
if (filename !== undefined) {
472500
expectedFilenames = filename.split(" ");

src/harness/harnessLanguageService.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ module Harness.LanguageService {
134134
private ls: ts.LanguageServiceShim = null;
135135

136136
private fileNameToScript: ts.Map<ScriptInfo> = {};
137-
138-
private settings: any = {};
137+
private settings: ts.CompilationSettings = {};
139138

140139
constructor(private cancellationToken: ts.CancellationToken = CancellationToken.None) {
141140
}
@@ -246,7 +245,7 @@ module Harness.LanguageService {
246245
return this.ls;
247246
}
248247

249-
public setCompilationSettings(settings: any) {
248+
public setCompilationSettings(settings: ts.CompilationSettings) {
250249
for (var key in settings) {
251250
if (settings.hasOwnProperty(key)) {
252251
this.settings[key] = settings[key];

src/services/shims.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ module ts {
159159
Asynchronous = 2,
160160
}
161161

162-
interface CompilationSettings {
162+
export interface CompilationSettings {
163163
propagateEnumConstants?: boolean;
164164
removeComments?: boolean;
165165
watch?: boolean;
@@ -179,6 +179,9 @@ module ts {
179179
gatherDiagnostics?: boolean;
180180
codepage?: number;
181181
emitBOM?: boolean;
182+
183+
// Declare indexer signature
184+
[index: string]: any;
182185
}
183186

184187
function languageVersionToScriptTarget(languageVersion: LanguageVersion): ScriptTarget {

tests/cases/fourslash/fourslash.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -446,23 +446,6 @@ module FourSlashInterface {
446446
public disableFormatting() {
447447
FourSlash.currentTestState.enableFormatting = false;
448448
}
449-
450-
public enableDeclaration() {
451-
FourSlash.currentTestState.enableDeclaration = true;
452-
}
453-
454-
public disableDeclaration() {
455-
FourSlash.currentTestState.enableDeclaration = false;
456-
}
457-
458-
public enableSingleOutputFile(outputFilename: string) {
459-
FourSlash.currentTestState.enableSingleOutputFile = true;
460-
FourSlash.currentTestState.singleOutputFilename = outputFilename;
461-
}
462-
463-
public disableSingleOutputFile() {
464-
FourSlash.currentTestState.enableSingleOutputFile = false;
465-
}
466449
}
467450

468451
export class debug {

0 commit comments

Comments
 (0)