Skip to content

Commit 384ee18

Browse files
committed
Gracefully handle errors where 'typings' is not a string (fixes #4828)
1 parent 2ef436f commit 384ee18

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/compiler/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ namespace ts {
9999
jsonContent = { typings: undefined };
100100
}
101101

102-
if (jsonContent.typings) {
102+
if (typeof jsonContent.typings === "string") {
103103
const result = loadNodeModuleFromFile(extensions, normalizePath(combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host);
104104
if (result) {
105105
return result;

tests/cases/unittests/moduleResolution.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,34 @@ module ts {
9999
assert.equal(resolution.failedLookupLocations.length, supportedTypeScriptExtensions.length);
100100
}
101101

102-
it("module name as directory - load from typings", () => {
102+
it("module name as directory - load from 'typings'", () => {
103103
testLoadingFromPackageJson("/a/b/c/d.ts", "/a/b/c/bar/package.json", "c/d/e.d.ts", "/a/b/c/bar/c/d/e.d.ts", "./bar");
104104
testLoadingFromPackageJson("/a/b/c/d.ts", "/a/bar/package.json", "e.d.ts", "/a/bar/e.d.ts", "../../bar");
105105
testLoadingFromPackageJson("/a/b/c/d.ts", "/bar/package.json", "e.d.ts", "/bar/e.d.ts", "/bar");
106106
testLoadingFromPackageJson("c:/a/b/c/d.ts", "c:/bar/package.json", "e.d.ts", "c:/bar/e.d.ts", "c:/bar");
107107
});
108108

109+
function testTypingsIgnored(typings: any): void {
110+
let containingFile = { name: "/a/b.ts" };
111+
let packageJson = { name: "/node_modules/b/package.json", content: JSON.stringify({ "typings": typings }) };
112+
let moduleFile = { name: "/a/b.d.ts" };
113+
114+
let indexPath = "/node_modules/b/index.d.ts";
115+
let indexFile = { name: indexPath }
116+
117+
let resolution = nodeModuleNameResolver("b", containingFile.name, {}, createModuleResolutionHost(containingFile, packageJson, moduleFile, indexFile));
118+
119+
assert.equal(resolution.resolvedModule.resolvedFileName, indexPath);
120+
}
121+
122+
it("module name as directory - handle invalid 'typings'", () => {
123+
testTypingsIgnored(["a", "b"]);
124+
testTypingsIgnored({ "a": "b" });
125+
testTypingsIgnored(true);
126+
testTypingsIgnored(null);
127+
testTypingsIgnored(undefined);
128+
});
129+
109130
it("module name as directory - load index.d.ts", () => {
110131
let containingFile = { name: "/a/b/c.ts" };
111132
let packageJson = { name: "/a/b/foo/package.json", content: JSON.stringify({ main: "/c/d" }) };

0 commit comments

Comments
 (0)