Skip to content

Commit b8acf8d

Browse files
Andymhegazy
Andy
authored andcommitted
Handle jsconfig.json in fourslash tests (#16484)
1 parent d2fd137 commit b8acf8d

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/harness/fourslash.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@ namespace FourSlash {
258258
let startResolveFileRef: FourSlashFile;
259259

260260
let configFileName: string;
261-
ts.forEach(testData.files, file => {
261+
for (const file of testData.files) {
262262
// Create map between fileName and its content for easily looking up when resolveReference flag is specified
263263
this.inputFiles.set(file.fileName, file.content);
264-
if (isTsconfig(file)) {
264+
if (isConfig(file)) {
265265
const configJson = ts.parseConfigFileTextToJson(file.fileName, file.content);
266266
if (configJson.config === undefined) {
267-
throw new Error(`Failed to parse test tsconfig.json: ${configJson.error.messageText}`);
267+
throw new Error(`Failed to parse test ${file.fileName}: ${configJson.error.messageText}`);
268268
}
269269

270270
// Extend our existing compiler options so that we can also support tsconfig only options
@@ -286,7 +286,7 @@ namespace FourSlash {
286286
// If entry point for resolving file references is already specified, report duplication error
287287
throw new Error("There exists a Fourslash file which has resolveReference flag specified; remove duplicated resolveReference flag");
288288
}
289-
});
289+
}
290290

291291
if (configFileName) {
292292
const baseDir = ts.normalizePath(ts.getDirectoryPath(configFileName));
@@ -295,12 +295,7 @@ namespace FourSlash {
295295
const configJsonObj = ts.parseConfigFileTextToJson(configFileName, this.inputFiles.get(configFileName));
296296
assert.isTrue(configJsonObj.config !== undefined);
297297

298-
const { options, errors } = ts.parseJsonConfigFileContent(configJsonObj.config, host, baseDir);
299-
300-
// Extend our existing compiler options so that we can also support tsconfig only options
301-
if (!errors || errors.length === 0) {
302-
compilationOptions = ts.extend(compilationOptions, options);
303-
}
298+
compilationOptions = ts.parseJsonConfigFileContent(configJsonObj.config, host, baseDir, compilationOptions, configFileName).options;
304299
}
305300

306301

@@ -3401,13 +3396,14 @@ ${code}
34013396
}
34023397

34033398
// @Filename is the only directive that can be used in a test that contains tsconfig.json file.
3404-
if (files.some(isTsconfig)) {
3399+
const config = ts.find(files, isConfig);
3400+
if (config) {
34053401
let directive = getNonFileNameOptionInFileList(files);
34063402
if (!directive) {
34073403
directive = getNonFileNameOptionInObject(globalOptions);
34083404
}
34093405
if (directive) {
3410-
throw Error("It is not allowed to use tsconfig.json along with directive '" + directive + "'");
3406+
throw Error(`It is not allowed to use ${config.fileName} along with directive '${directive}'`);
34113407
}
34123408
}
34133409

@@ -3420,8 +3416,8 @@ ${code}
34203416
};
34213417
}
34223418

3423-
function isTsconfig(file: FourSlashFile): boolean {
3424-
return ts.getBaseFileName(file.fileName).toLowerCase() === "tsconfig.json";
3419+
function isConfig(file: FourSlashFile): boolean {
3420+
return Harness.getConfigNameFromFileName(file.fileName) !== undefined;
34253421
}
34263422

34273423
function getNonFileNameOptionInFileList(files: FourSlashFile[]): string {

src/harness/harness.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,7 @@ namespace Harness {
19611961
let tsConfigFileUnitData: TestUnitData;
19621962
for (let i = 0; i < testUnitData.length; i++) {
19631963
const data = testUnitData[i];
1964-
if (ts.getBaseFileName(data.name).toLowerCase() === "tsconfig.json") {
1964+
if (getConfigNameFromFileName(data.name)) {
19651965
const configJson = ts.parseJsonText(data.name, data.content);
19661966
assert.isTrue(configJson.endOfFileToken !== undefined);
19671967
let baseDir = ts.normalizePath(ts.getDirectoryPath(data.name));
@@ -2172,5 +2172,10 @@ namespace Harness {
21722172
return { unitName: libFile, content: io.readFile(libFile) };
21732173
}
21742174

2175+
export function getConfigNameFromFileName(filename: string): "tsconfig.json" | "jsconfig.json" | undefined {
2176+
const flc = ts.getBaseFileName(filename).toLowerCase();
2177+
return ts.find(["tsconfig.json" as "tsconfig.json", "jsconfig.json" as "jsconfig.json"], x => x === flc);
2178+
}
2179+
21752180
if (Error) (<any>Error).stackTraceLimit = 100;
21762181
}

tests/cases/fourslash/jsconfig.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.js
4+
////function f(/**/x) {
5+
////}
6+
7+
// @Filename: /jsconfig.json
8+
////{
9+
//// "compilerOptions": {
10+
//// "checkJs": true,
11+
//// "noImplicitAny": true
12+
//// }
13+
////}
14+
15+
goTo.file("/a.js");
16+
verify.errorExistsAfterMarker("");

0 commit comments

Comments
 (0)