From 00bab0a27bb371fa625e3f998679ef9052f841ac Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 07:59:55 -0700 Subject: [PATCH] Handle jsconfig.json in fourslash tests --- src/harness/fourslash.ts | 27 +++++++++------------------ src/harness/harness.ts | 7 ++++++- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index ceeec7f9b9c24..58b54974915c5 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -263,13 +263,14 @@ namespace FourSlash { let startResolveFileRef: FourSlashFile; let configFileName: string; - ts.forEach(testData.files, file => { + for (const file of testData.files) { // Create map between fileName and its content for easily looking up when resolveReference flag is specified this.inputFiles.set(file.fileName, file.content); - if (ts.getBaseFileName(file.fileName).toLowerCase() === "tsconfig.json") { + const config = Harness.getConfigNameFromFileName(file.fileName); + if (config) { const configJson = ts.parseConfigFileTextToJson(file.fileName, file.content); if (configJson.config === undefined) { - throw new Error(`Failed to parse test tsconfig.json: ${configJson.error.messageText}`); + throw new Error(`Failed to parse test ${config}: ${configJson.error.messageText}`); } // Extend our existing compiler options so that we can also support tsconfig only options @@ -291,7 +292,7 @@ namespace FourSlash { // If entry point for resolving file references is already specified, report duplication error throw new Error("There exists a Fourslash file which has resolveReference flag specified; remove duplicated resolveReference flag"); } - }); + } if (configFileName) { const baseDir = ts.normalizePath(ts.getDirectoryPath(configFileName)); @@ -3098,13 +3099,11 @@ ${code} } // @Filename is the only directive that can be used in a test that contains tsconfig.json file. - if (containTSConfigJson(files)) { - let directive = getNonFileNameOptionInFileList(files); - if (!directive) { - directive = getNonFileNameOptionInObject(globalOptions); - } + const config = ts.forEach(files, f => Harness.getConfigNameFromFileName(f.fileName)); + if (config) { + const directive = ts.forEach(files, f => getNonFileNameOptionInObject(f.fileOptions)) || getNonFileNameOptionInObject(globalOptions); if (directive) { - throw Error("It is not allowed to use tsconfig.json along with directive '" + directive + "'"); + throw Error(`It is not allowed to use ${config} along with directive '${directive}'`); } } @@ -3117,14 +3116,6 @@ ${code} }; } - function containTSConfigJson(files: FourSlashFile[]): boolean { - return ts.forEach(files, f => f.fileOptions["Filename"] === "tsconfig.json"); - } - - function getNonFileNameOptionInFileList(files: FourSlashFile[]): string { - return ts.forEach(files, f => getNonFileNameOptionInObject(f.fileOptions)); - } - function getNonFileNameOptionInObject(optionObject: { [s: string]: string }): string { for (const option in optionObject) { if (option !== metadataOptionNames.fileName) { diff --git a/src/harness/harness.ts b/src/harness/harness.ts index e944e68f11ea7..e2b796a6c7a99 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1832,7 +1832,7 @@ namespace Harness { let tsConfig: ts.ParsedCommandLine; for (let i = 0; i < testUnitData.length; i++) { const data = testUnitData[i]; - if (ts.getBaseFileName(data.name).toLowerCase() === "tsconfig.json") { + if (getConfigNameFromFileName(data.name)) { const configJson = ts.parseConfigFileTextToJson(data.name, data.content); assert.isTrue(configJson.config !== undefined); let baseDir = ts.normalizePath(ts.getDirectoryPath(data.name)); @@ -1985,5 +1985,10 @@ namespace Harness { return { unitName: libFile, content: io.readFile(libFile) }; } + export function getConfigNameFromFileName(filename: string): "tsconfig.json" | "jsconfig.json" | undefined { + const flc = ts.getBaseFileName(filename).toLowerCase(); + return ts.find(["tsconfig.json" as "tsconfig.json", "jsconfig.json" as "jsconfig.json"], x => x === flc); + } + if (Error) (Error).stackTraceLimit = 100; }