Use rooted paths in the fourslash virtual file system

This commit is contained in:
Richard Knoll
2016-08-16 14:55:19 -07:00
parent cc35bd5dca
commit 2f4a855ab8
34 changed files with 81 additions and 80 deletions
+1 -1
View File
@@ -19892,7 +19892,7 @@ namespace ts {
function getAmbientModules(): Symbol[] {
const result: Symbol[] = [];
for (const sym in globals) {
if (globals.hasOwnProperty(sym) && ambientModuleSymbolRegex.test(sym)) {
if (hasProperty(globals, sym) && ambientModuleSymbolRegex.test(sym)) {
result.push(globals[sym]);
}
}
+7 -3
View File
@@ -2352,12 +2352,16 @@ namespace FourSlash {
}
export function runFourSlashTestContent(basePath: string, testType: FourSlashTestType, content: string, fileName: string): void {
// Give file paths an absolute path for the virtual file system
const absoluteBasePath = ts.combinePaths(Harness.virtualFileSystemRoot, basePath);
const absoluteFileName = ts.combinePaths(Harness.virtualFileSystemRoot, fileName);
// Parse out the files and their metadata
const testData = parseTestData(basePath, content, fileName);
const state = new TestState(basePath, testType, testData);
const testData = parseTestData(absoluteBasePath, content, absoluteFileName);
const state = new TestState(absoluteBasePath, testType, testData);
const output = ts.transpileModule(content, { reportDiagnostics: true });
if (output.diagnostics.length > 0) {
throw new Error(`Syntax error in ${basePath}: ${output.diagnostics[0].messageText}`);
throw new Error(`Syntax error in ${absoluteBasePath}: ${output.diagnostics[0].messageText}`);
}
runCode(output.outputText, state);
}
+3
View File
@@ -458,6 +458,9 @@ namespace Harness {
// harness always uses one kind of new line
const harnessNewLine = "\r\n";
// Roote for file paths that are stored in a virtual file system
export const virtualFileSystemRoot = "/";
namespace IOImpl {
declare class Enumerator {
public atEnd(): boolean;
+4 -16
View File
@@ -123,7 +123,7 @@ namespace Harness.LanguageService {
}
export class LanguageServiceAdapterHost {
protected virtualFileSystem: Utils.VirtualFileSystem<ScriptInfo> = new Utils.VirtualFileSystem<ScriptInfo>(/*root*/"c:", /*useCaseSensitiveFilenames*/false);
protected virtualFileSystem: Utils.VirtualFileSystem<ScriptInfo> = new Utils.VirtualFileSystem<ScriptInfo>(virtualFileSystemRoot, /*useCaseSensitiveFilenames*/false);
constructor(protected cancellationToken = DefaultHostCancellationToken.Instance,
protected settings = ts.getDefaultCompilerOptions()) {
@@ -191,7 +191,7 @@ namespace Harness.LanguageService {
}
return [];
}
getCurrentDirectory(): string { return ""; }
getCurrentDirectory(): string { return virtualFileSystemRoot }
getDefaultLibFileName(): string { return Harness.Compiler.defaultLibFileName; }
getScriptFileNames(): string[] { return this.getFilenames(); }
getScriptSnapshot(fileName: string): ts.IScriptSnapshot {
@@ -211,7 +211,7 @@ namespace Harness.LanguageService {
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[] {
return ts.matchFiles(path, extensions, exclude, include,
/*useCaseSensitiveFileNames*/false,
/*currentDirectory*/"/",
this.getCurrentDirectory(),
(p) => this.virtualFileSystem.getAccessibleFileSystemEntries(p));
}
readFile(path: string, encoding?: string): string {
@@ -220,19 +220,7 @@ namespace Harness.LanguageService {
}
resolvePath(path: string): string {
if (!ts.isRootedDiskPath(path)) {
// An "absolute" path for fourslash is one that is contained within the tests directory
const components = ts.getNormalizedPathComponents(path, this.getCurrentDirectory());
if (components.length) {
// If this is still a relative path after normalization (i.e. currentDirectory is relative), the root will be the empty string
if (!components[0]) {
components.splice(0, 1);
if (components[0] !== "tests") {
// If not contained within test, assume its relative to the directory containing the test files
return ts.normalizePath(ts.combinePaths("tests/cases/fourslash", components.join(ts.directorySeparator)));
}
}
return ts.normalizePath(components.join(ts.directorySeparator));
}
path = ts.combinePaths(this.getCurrentDirectory(), path);
}
return ts.normalizePath(path);
}
+9 -1
View File
@@ -111,6 +111,7 @@ namespace Utils {
getFileSystemEntries() { return this.root.getFileSystemEntries(); }
addDirectory(path: string) {
path = this.normalizePathRoot(path);
const components = ts.getNormalizedPathComponents(path, this.currentDirectory);
let directory: VirtualDirectory<T> = this.root;
for (const component of components) {
@@ -124,7 +125,7 @@ namespace Utils {
}
addFile(path: string, content?: T) {
const absolutePath = ts.getNormalizedAbsolutePath(path, this.currentDirectory);
const absolutePath = this.normalizePathRoot(ts.getNormalizedAbsolutePath(path, this.currentDirectory));
const fileName = ts.getBaseFileName(path);
const directoryPath = ts.getDirectoryPath(absolutePath);
const directory = this.addDirectory(directoryPath);
@@ -141,6 +142,7 @@ namespace Utils {
}
traversePath(path: string) {
path = this.normalizePathRoot(path);
let directory: VirtualDirectory<T> = this.root;
for (const component of ts.getNormalizedPathComponents(path, this.currentDirectory)) {
const entry = directory.getFileSystemEntry(component);
@@ -192,7 +194,13 @@ namespace Utils {
}
}
normalizePathRoot(path: string) {
const components = ts.getNormalizedPathComponents(path, this.currentDirectory);
// Toss the root component
components[0] = "";
return components.join(ts.directorySeparator);
}
}
export class MockParseConfigHost extends VirtualFileSystem<string> implements ts.ParseConfigHost {
+1 -3
View File
@@ -3154,9 +3154,7 @@ namespace ts {
writeFile: (fileName, data, writeByteOrderMark) => { },
getCurrentDirectory: () => currentDirectory,
fileExists: (fileName): boolean => {
// stub missing host functionality
Debug.assert(!host.resolveModuleNames || !host.resolveTypeReferenceDirectives);
return hostCache.getOrCreateEntry(fileName) !== undefined;
return host.fileExists(fileName);
},
readFile: (fileName): string => {
// stub missing host functionality