Skip to content

Commit 8e16bff

Browse files
committed
Handle the json extension when ResolvedModule result is json file
Fixes microsoft#24932
1 parent 3261473 commit 8e16bff

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

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)