Skip to content

Commit 34b9c4d

Browse files
authored
Merge pull request microsoft#24959 from Microsoft/moduleResolutionWithoutExtensionWithJson
Handle the json extension when ResolvedModule result is json file
2 parents 1cd604a + fe26058 commit 34b9c4d

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/harness/fourslash.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,17 @@ namespace FourSlash {
507507
}
508508

509509
private getAllDiagnostics(): ts.Diagnostic[] {
510-
return ts.flatMap(this.languageServiceAdapterHost.getFilenames(), fileName =>
511-
ts.isAnySupportedFileExtension(fileName) ? this.getDiagnostics(fileName) : []);
510+
return ts.flatMap(this.languageServiceAdapterHost.getFilenames(), fileName => {
511+
if (!ts.isAnySupportedFileExtension(fileName)) {
512+
return [];
513+
}
514+
515+
const baseName = ts.getBaseFileName(fileName);
516+
if (baseName === "package.json" || baseName === "tsconfig.json" || baseName === "jsconfig.json") {
517+
return [];
518+
}
519+
return this.getDiagnostics(fileName);
520+
});
512521
}
513522

514523
public verifyErrorExistsAfterMarker(markerName: string, shouldExist: boolean, after: boolean) {

src/parser/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7995,7 +7995,7 @@ namespace ts {
79957995
}
79967996

79977997
export function tryGetExtensionFromPath(path: string): Extension | undefined {
7998-
return find<Extension>(supportedTypescriptExtensionsForExtractExtension, e => fileExtensionIs(path, e)) || find(supportedJavascriptExtensions, e => fileExtensionIs(path, e));
7998+
return find<Extension>(extensionsToRemove, e => fileExtensionIs(path, e));
79997999
}
80008000

80018001
function getAnyExtensionFromPathWorker(path: string, extensions: string | ReadonlyArray<string>, stringEqualityComparer: (a: string, b: string) => boolean) {

src/testRunner/unittests/tscWatchMode.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,4 +2532,43 @@ declare module "fs" {
25322532
checkWatchedDirectoriesDetailed(host, [mainPackageRoot, linkedPackageRoot, `${mainPackageRoot}/node_modules/@types`, `${projectRoot}/node_modules/@types`], 1, /*recursive*/ true);
25332533
});
25342534
});
2535+
2536+
describe("tsc-watch with custom module resolution", () => {
2537+
const projectRoot = "/user/username/projects/project";
2538+
const configFileJson: any = {
2539+
compilerOptions: { module: "commonjs", resolveJsonModule: true },
2540+
files: ["index.ts"]
2541+
};
2542+
const mainFile: File = {
2543+
path: `${projectRoot}/index.ts`,
2544+
content: "import settings from './settings.json';"
2545+
};
2546+
const config: File = {
2547+
path: `${projectRoot}/tsconfig.json`,
2548+
content: JSON.stringify(configFileJson)
2549+
};
2550+
const settingsJson: File = {
2551+
path: `${projectRoot}/settings.json`,
2552+
content: JSON.stringify({ content: "Print this" })
2553+
};
2554+
2555+
it("verify that module resolution with json extension works when returned without extension", () => {
2556+
const files = [libFile, mainFile, config, settingsJson];
2557+
const host = createWatchedSystem(files, { currentDirectory: projectRoot });
2558+
const compilerHost = createWatchCompilerHostOfConfigFile(config.path, {}, host);
2559+
const parsedCommandResult = parseJsonConfigFileContent(configFileJson, host, config.path);
2560+
compilerHost.resolveModuleNames = (moduleNames, containingFile) => moduleNames.map(m => {
2561+
const result = resolveModuleName(m, containingFile, parsedCommandResult.options, compilerHost);
2562+
const resolvedModule = result.resolvedModule!;
2563+
return {
2564+
resolvedFileName: resolvedModule.resolvedFileName,
2565+
isExternalLibraryImport: resolvedModule.isExternalLibraryImport,
2566+
originalFileName: resolvedModule.originalPath,
2567+
};
2568+
});
2569+
const watch = createWatchProgram(compilerHost);
2570+
const program = watch.getCurrentProgram().getProgram();
2571+
checkProgramActualFiles(program, [mainFile.path, libFile.path, settingsJson.path]);
2572+
});
2573+
});
25352574
}

0 commit comments

Comments
 (0)