diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 2e98a95831fca..4c0c72a200f3f 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -258,13 +258,13 @@ 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 (isTsconfig(file)) { + if (isConfig(file)) { 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 ${file.fileName}: ${configJson.error.messageText}`); } // Extend our existing compiler options so that we can also support tsconfig only options @@ -286,7 +286,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)); @@ -295,12 +295,7 @@ namespace FourSlash { const configJsonObj = ts.parseConfigFileTextToJson(configFileName, this.inputFiles.get(configFileName)); assert.isTrue(configJsonObj.config !== undefined); - const { options, errors } = ts.parseJsonConfigFileContent(configJsonObj.config, host, baseDir); - - // Extend our existing compiler options so that we can also support tsconfig only options - if (!errors || errors.length === 0) { - compilationOptions = ts.extend(compilationOptions, options); - } + compilationOptions = ts.parseJsonConfigFileContent(configJsonObj.config, host, baseDir, compilationOptions, configFileName).options; } @@ -3401,13 +3396,14 @@ ${code} } // @Filename is the only directive that can be used in a test that contains tsconfig.json file. - if (files.some(isTsconfig)) { + const config = ts.find(files, isConfig); + if (config) { let directive = getNonFileNameOptionInFileList(files); if (!directive) { directive = 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.fileName} along with directive '${directive}'`); } } @@ -3420,8 +3416,8 @@ ${code} }; } - function isTsconfig(file: FourSlashFile): boolean { - return ts.getBaseFileName(file.fileName).toLowerCase() === "tsconfig.json"; + function isConfig(file: FourSlashFile): boolean { + return Harness.getConfigNameFromFileName(file.fileName) !== undefined; } function getNonFileNameOptionInFileList(files: FourSlashFile[]): string { diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 612224cb312a2..f2f8587d83412 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1961,7 +1961,7 @@ namespace Harness { let tsConfigFileUnitData: TestUnitData; 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.parseJsonText(data.name, data.content); assert.isTrue(configJson.endOfFileToken !== undefined); let baseDir = ts.normalizePath(ts.getDirectoryPath(data.name)); @@ -2172,5 +2172,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; } diff --git a/tests/cases/fourslash/jsconfig.ts b/tests/cases/fourslash/jsconfig.ts new file mode 100644 index 0000000000000..ce09c46b8574f --- /dev/null +++ b/tests/cases/fourslash/jsconfig.ts @@ -0,0 +1,16 @@ +/// + +// @Filename: /a.js +////function f(/**/x) { +////} + +// @Filename: /jsconfig.json +////{ +//// "compilerOptions": { +//// "checkJs": true, +//// "noImplicitAny": true +//// } +////} + +goTo.file("/a.js"); +verify.errorExistsAfterMarker("");