Skip to content

Commit 8541577

Browse files
author
Yui T
committed
Add fourslash support and tests cases
1 parent 4f6ccab commit 8541577

2 files changed

Lines changed: 65 additions & 30 deletions

File tree

src/harness/fourslash.ts

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,11 @@ module FourSlash {
136136
outDir: 'outDir',
137137
sourceMap: 'sourceMap',
138138
sourceRoot: 'sourceRoot',
139+
resolveReference: 'ResolveReference', // This flag is used to specify entry file for resolve file references. The flag is only allow once per test file
139140
};
140141

141142
// List of allowed metadata names
142-
var fileMetadataNames = [testOptMetadataNames.filename, testOptMetadataNames.emitThisFile];
143+
var fileMetadataNames = [testOptMetadataNames.filename, testOptMetadataNames.emitThisFile, testOptMetadataNames.resolveReference];
143144
var globalMetadataNames = [testOptMetadataNames.baselineFile, testOptMetadataNames.declaration,
144145
testOptMetadataNames.mapRoot, testOptMetadataNames.module, testOptMetadataNames.out,
145146
testOptMetadataNames.outDir, testOptMetadataNames.sourceMap, testOptMetadataNames.sourceRoot]
@@ -236,6 +237,25 @@ module FourSlash {
236237
throw new Error("Operation should be cancelled");
237238
}
238239

240+
// This function creates IScriptSnapshot object for testing getPreProcessedFileInfo
241+
// Return object may lack some functionalities for other purposes.
242+
function createScriptSnapShot(sourceText: string): TypeScript.IScriptSnapshot {
243+
return {
244+
getText: (start: number, end: number) => {
245+
return sourceText.substr(start, end - start);
246+
},
247+
getLength: () => {
248+
return sourceText.length;
249+
},
250+
getLineStartPositions: () => {
251+
return <number[]>[];
252+
},
253+
getChangeRange: (oldSnapshot: TypeScript.IScriptSnapshot) => {
254+
return <TypeScript.TextChangeRange>undefined;
255+
}
256+
};
257+
}
258+
239259
export class TestState {
240260
// Language service instance
241261
public languageServiceShimHost: Harness.LanguageService.TypeScriptLS;
@@ -274,9 +294,15 @@ module FourSlash {
274294
this.languageServiceShimHost.setCompilationSettings(compilationSettings);
275295

276296
var inputFiles: { unitName: string; content: string }[] = [];
297+
var startResolveFileRef: FourSlashFile = undefined;
277298

278299
testData.files.forEach(file => {
279-
var fixedPath = file.fileName.substr(file.fileName.indexOf('tests/'));
300+
if (!startResolveFileRef && file.fileOptions[testOptMetadataNames.resolveReference]) {
301+
startResolveFileRef = file;
302+
} else if (startResolveFileRef) {
303+
// If entry point for resolving file references is already specified, report duplication error
304+
throw new Error("There exists a Fourslash file which has resolveReference flag specified; remove duplicated resolveReference flag");
305+
}
280306
});
281307

282308
// NEWTODO: disable resolution for now.
@@ -294,33 +320,42 @@ module FourSlash {
294320
});
295321
//}
296322

297-
298-
// NEWTODO: Re-implement commented-out section
299-
//harnessCompiler.addInputFiles(inputFiles);
300-
//try {
301-
// var resolvedFiles = harnessCompiler.resolve();
302-
303-
// resolvedFiles.forEach(file => {
304-
// if (!Harness.isLibraryFile(file.path)) {
305-
// var fixedPath = file.path.substr(file.path.indexOf('tests/'));
306-
// var content = harnessCompiler.getContentForFile(fixedPath);
307-
// this.languageServiceShimHost.addScript(fixedPath, content);
308-
// }
309-
// });
310-
311-
// this.languageServiceShimHost.addScript('lib.d.ts', Harness.Compiler.libTextMinimal);
312-
//}
313-
//finally {
314-
// // harness no longer needs the results of the above work, make sure the next test operations are in a clean state
315-
// harnessCompiler.reset();
316-
//}
317-
318-
/// NEWTODO: For now do not resolve, just use the input files
319-
inputFiles.forEach(file => {
320-
if (!Harness.isLibraryFile(file.unitName)) {
321-
this.languageServiceShimHost.addScript(file.unitName, file.content);
322-
}
323-
});
323+
if (startResolveFileRef) {
324+
// Add the entry-point file itself into the languageServiceShimHost
325+
this.languageServiceShimHost.addScript(startResolveFileRef.fileName, startResolveFileRef.content);
326+
327+
var jsonResolvedResult = JSON.parse(this.languageServiceShimHost.getCoreService().getPreProcessedFileInfo(startResolveFileRef.fileName,
328+
createScriptSnapShot(startResolveFileRef.content)));
329+
var resolvedResult = jsonResolvedResult.result;
330+
var referencedFiles: ts.IFileReference[] = resolvedResult.referencedFiles;
331+
var importedFiles: ts.IFileReference[] = resolvedResult.importedFiles;
332+
referencedFiles.forEach(refFile => {
333+
inputFiles.forEach(inputFile => {
334+
// Fourslash insert tests/cases/fourslash into inputFile.unitName so we will properly append the same base directory to refFile path
335+
var appendRefFilePath = "tests/cases/fourslash/" + refFile.path;
336+
if (appendRefFilePath === inputFile.unitName && !Harness.isLibraryFile(inputFile.unitName)) {
337+
this.languageServiceShimHost.addScript(inputFile.unitName, inputFile.content);
338+
}
339+
});
340+
});
341+
importedFiles.forEach(importedFile => {
342+
inputFiles.forEach(inputFile => {
343+
// Fourslash insert tests/cases/fourslash into inputFile.unitName and import statement doesn't require ".ts"
344+
// so convert them before making appropriate comparison
345+
var appendRefFilePath = "tests/cases/fourslash/" + importedFile.path + ".ts";
346+
if (appendRefFilePath === inputFile.unitName && !Harness.isLibraryFile(inputFile.unitName)) {
347+
this.languageServiceShimHost.addScript(inputFile.unitName, inputFile.content);
348+
}
349+
});
350+
});
351+
} else {
352+
// resolveReference file-option is not specified then do not resolve any files and include all inputFiles
353+
inputFiles.forEach(file => {
354+
if (!Harness.isLibraryFile(file.unitName)) {
355+
this.languageServiceShimHost.addScript(file.unitName, file.content);
356+
}
357+
});
358+
}
324359

325360
this.languageServiceShimHost.addDefaultLibrary();
326361

src/services/shims.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module ts {
5959
///
6060
// Note: This is being using by the host (VS) and is marshaled back and forth.
6161
// When changing this make sure the changes are reflected in the managed side as well
62-
interface IFileReference {
62+
export interface IFileReference {
6363
path: string;
6464
position: number;
6565
length: number;

0 commit comments

Comments
 (0)