Skip to content

Commit 9ba8ef1

Browse files
author
Andy Hanson
committed
Allow jsconfig.json in fourslash tests
1 parent 1d0087d commit 9ba8ef1

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/harness/fourslash.ts

+9-18
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,14 @@ namespace FourSlash {
263263
let startResolveFileRef: FourSlashFile;
264264

265265
let configFileName: string;
266-
ts.forEach(testData.files, file => {
266+
for (const file of testData.files) {
267267
// Create map between fileName and its content for easily looking up when resolveReference flag is specified
268268
this.inputFiles.set(file.fileName, file.content);
269-
if (ts.getBaseFileName(file.fileName).toLowerCase() === "tsconfig.json") {
269+
const config = Harness.getConfigNameFromFileName(file.fileName);
270+
if (config) {
270271
const configJson = ts.parseConfigFileTextToJson(file.fileName, file.content);
271272
if (configJson.config === undefined) {
272-
throw new Error(`Failed to parse test tsconfig.json: ${configJson.error.messageText}`);
273+
throw new Error(`Failed to parse test ${config}: ${configJson.error.messageText}`);
273274
}
274275

275276
// Extend our existing compiler options so that we can also support tsconfig only options
@@ -291,7 +292,7 @@ namespace FourSlash {
291292
// If entry point for resolving file references is already specified, report duplication error
292293
throw new Error("There exists a Fourslash file which has resolveReference flag specified; remove duplicated resolveReference flag");
293294
}
294-
});
295+
}
295296

296297
if (configFileName) {
297298
const baseDir = ts.normalizePath(ts.getDirectoryPath(configFileName));
@@ -3098,13 +3099,11 @@ ${code}
30983099
}
30993100

31003101
// @Filename is the only directive that can be used in a test that contains tsconfig.json file.
3101-
if (containTSConfigJson(files)) {
3102-
let directive = getNonFileNameOptionInFileList(files);
3103-
if (!directive) {
3104-
directive = getNonFileNameOptionInObject(globalOptions);
3105-
}
3102+
const config = ts.forEach(files, f => Harness.getConfigNameFromFileName(f.fileName));
3103+
if (config) {
3104+
const directive = ts.forEach(files, f => getNonFileNameOptionInObject(f.fileOptions)) || getNonFileNameOptionInObject(globalOptions);
31063105
if (directive) {
3107-
throw Error("It is not allowed to use tsconfig.json along with directive '" + directive + "'");
3106+
throw Error(`It is not allowed to use ${config} along with directive '${directive}'`);
31083107
}
31093108
}
31103109

@@ -3117,14 +3116,6 @@ ${code}
31173116
};
31183117
}
31193118

3120-
function containTSConfigJson(files: FourSlashFile[]): boolean {
3121-
return ts.forEach(files, f => f.fileOptions["Filename"] === "tsconfig.json");
3122-
}
3123-
3124-
function getNonFileNameOptionInFileList(files: FourSlashFile[]): string {
3125-
return ts.forEach(files, f => getNonFileNameOptionInObject(f.fileOptions));
3126-
}
3127-
31283119
function getNonFileNameOptionInObject(optionObject: { [s: string]: string }): string {
31293120
for (const option in optionObject) {
31303121
if (option !== metadataOptionNames.fileName) {

src/harness/harness.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,7 @@ namespace Harness {
18321832
let tsConfig: ts.ParsedCommandLine;
18331833
for (let i = 0; i < testUnitData.length; i++) {
18341834
const data = testUnitData[i];
1835-
if (ts.getBaseFileName(data.name).toLowerCase() === "tsconfig.json") {
1835+
if (getConfigNameFromFileName(data.name)) {
18361836
const configJson = ts.parseConfigFileTextToJson(data.name, data.content);
18371837
assert.isTrue(configJson.config !== undefined);
18381838
let baseDir = ts.normalizePath(ts.getDirectoryPath(data.name));
@@ -1985,5 +1985,10 @@ namespace Harness {
19851985
return { unitName: libFile, content: io.readFile(libFile) };
19861986
}
19871987

1988+
export function getConfigNameFromFileName(filename: string): "tsconfig.json" | "jsconfig.json" | undefined {
1989+
const flc = ts.getBaseFileName(filename).toLowerCase();
1990+
return ts.find(["tsconfig.json" as "tsconfig.json", "jsconfig.json" as "jsconfig.json"], x => x === flc);
1991+
}
1992+
19881993
if (Error) (<any>Error).stackTraceLimit = 100;
19891994
}

0 commit comments

Comments
 (0)