Skip to content

Gracefully handle errors where 'typings' is not a string (fixes #4828) #5971

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Dec 7, 2015
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
2 changes: 1 addition & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace ts {
jsonContent = { typings: undefined };
}

if (jsonContent.typings) {
if (typeof jsonContent.typings === "string") {
const result = loadNodeModuleFromFile(extensions, normalizePath(combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host);
if (result) {
return result;
Expand Down
23 changes: 22 additions & 1 deletion tests/cases/unittests/moduleResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,34 @@ module ts {
assert.equal(resolution.failedLookupLocations.length, supportedTypeScriptExtensions.length);
}

it("module name as directory - load from typings", () => {
it("module name as directory - load from 'typings'", () => {
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");
testLoadingFromPackageJson("/a/b/c/d.ts", "/a/bar/package.json", "e.d.ts", "/a/bar/e.d.ts", "../../bar");
testLoadingFromPackageJson("/a/b/c/d.ts", "/bar/package.json", "e.d.ts", "/bar/e.d.ts", "/bar");
testLoadingFromPackageJson("c:/a/b/c/d.ts", "c:/bar/package.json", "e.d.ts", "c:/bar/e.d.ts", "c:/bar");
});

function testTypingsIgnored(typings: any): void {
let containingFile = { name: "/a/b.ts" };
let packageJson = { name: "/node_modules/b/package.json", content: JSON.stringify({ "typings": typings }) };
let moduleFile = { name: "/a/b.d.ts" };

let indexPath = "/node_modules/b/index.d.ts";
let indexFile = { name: indexPath }

let resolution = nodeModuleNameResolver("b", containingFile.name, {}, createModuleResolutionHost(containingFile, packageJson, moduleFile, indexFile));

assert.equal(resolution.resolvedModule.resolvedFileName, indexPath);
}

it("module name as directory - handle invalid 'typings'", () => {
testTypingsIgnored(["a", "b"]);
testTypingsIgnored({ "a": "b" });
testTypingsIgnored(true);
testTypingsIgnored(null);
testTypingsIgnored(undefined);
});

it("module name as directory - load index.d.ts", () => {
let containingFile = { name: "/a/b/c.ts" };
let packageJson = { name: "/a/b/foo/package.json", content: JSON.stringify({ main: "/c/d" }) };
Expand Down