diff --git a/src/compiler/program.ts b/src/compiler/program.ts index ed9f010c56a27..96afd69ea4d95 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -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; diff --git a/tests/cases/unittests/moduleResolution.ts b/tests/cases/unittests/moduleResolution.ts index c4c17ebb223eb..5b924650401dd 100644 --- a/tests/cases/unittests/moduleResolution.ts +++ b/tests/cases/unittests/moduleResolution.ts @@ -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" }) };