Skip to content

Handle jsconfig.json in fourslash tests #16484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand All @@ -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;
}


Expand Down Expand Up @@ -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}'`);
}
}

Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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) (<any>Error).stackTraceLimit = 100;
}
16 changes: 16 additions & 0 deletions tests/cases/fourslash/jsconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path="fourslash.ts" />

// @Filename: /a.js
////function f(/**/x) {
////}

// @Filename: /jsconfig.json
////{
//// "compilerOptions": {
//// "checkJs": true,
//// "noImplicitAny": true
//// }
////}

goTo.file("/a.js");
verify.errorExistsAfterMarker("");