Search for node_modules in parent directories when getting type roots.

This commit is contained in:
Andy Hanson 2016-09-02 13:39:56 -07:00
parent d6dac6a6cb
commit 12ad9d0aaf
15 changed files with 422 additions and 23 deletions

View file

@ -9,8 +9,6 @@ namespace ts {
const emptyArray: any[] = [];
const defaultTypeRoots = ["node_modules/@types"];
export function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean): string {
while (true) {
const fileName = combinePaths(searchPath, "tsconfig.json");
@ -169,7 +167,7 @@ namespace ts {
const typeReferenceExtensions = [".d.ts"];
function getEffectiveTypeRoots(options: CompilerOptions, host: ModuleResolutionHost) {
function getEffectiveTypeRoots(options: CompilerOptions, host: ModuleResolutionHost): string[] | undefined {
if (options.typeRoots) {
return options.typeRoots;
}
@ -182,12 +180,37 @@ namespace ts {
currentDirectory = host.getCurrentDirectory();
}
if (!currentDirectory) {
return undefined;
}
return map(defaultTypeRoots, d => combinePaths(currentDirectory, d));
return currentDirectory && getDefaultTypeRoots(currentDirectory, host);
}
/**
* Returns the path to every node_modules/@types directory from some ancestor directory.
* Returns undefined if there are none.
*/
function getDefaultTypeRoots(currentDirectory: string, host: ModuleResolutionHost): string[] | undefined {
if (!host.directoryExists) {
return [combinePaths(currentDirectory, nodeModulesAtTypes)];
}
let typeRoots: string[];
while (true) {
const atTypes = combinePaths(currentDirectory, nodeModulesAtTypes);
if (host.directoryExists(atTypes)) {
(typeRoots || (typeRoots = [])).push(atTypes);
}
const parent = getDirectoryPath(currentDirectory);
if (parent === currentDirectory) {
break;
}
currentDirectory = parent;
}
return typeRoots;
}
const nodeModulesAtTypes = combinePaths("node_modules", "@types");
/**
* @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups

View file

@ -1,8 +1,6 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/b/consumer.ts', root directory '/node_modules/@types'. ========",
"Resolving with primary search path '/node_modules/@types'",
"File '/node_modules/@types/jquery/package.json' does not exist.",
"File '/node_modules/@types/jquery/index.d.ts' does not exist.",
"======== Resolving type reference directive 'jquery', containing file '/a/b/consumer.ts', root directory not set. ========",
"Root directory cannot be determined, skipping primary search paths.",
"Looking up in 'node_modules' folder, initial location '/a/b'",
"File '/a/b/node_modules/jquery.ts' does not exist.",
"File '/a/b/node_modules/jquery.d.ts' does not exist.",

View file

@ -1,8 +1,6 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/b/consumer.ts', root directory '/node_modules/@types'. ========",
"Resolving with primary search path '/node_modules/@types'",
"File '/node_modules/@types/jquery/package.json' does not exist.",
"File '/node_modules/@types/jquery/index.d.ts' does not exist.",
"======== Resolving type reference directive 'jquery', containing file '/a/b/consumer.ts', root directory not set. ========",
"Root directory cannot be determined, skipping primary search paths.",
"Looking up in 'node_modules' folder, initial location '/a/b'",
"File '/a/b/node_modules/jquery.ts' does not exist.",
"File '/a/b/node_modules/jquery.d.ts' does not exist.",

View file

@ -1,8 +1,6 @@
[
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory '/src/node_modules/@types'. ========",
"Resolving with primary search path '/src/node_modules/@types'",
"File '/src/node_modules/@types/jquery/package.json' does not exist.",
"File '/src/node_modules/@types/jquery/index.d.ts' does not exist.",
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory not set. ========",
"Root directory cannot be determined, skipping primary search paths.",
"Looking up in 'node_modules' folder, initial location '/src'",
"File '/src/node_modules/jquery.ts' does not exist.",
"File '/src/node_modules/jquery.d.ts' does not exist.",

View file

@ -1,8 +1,6 @@
[
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory '/node_modules/@types'. ========",
"Resolving with primary search path '/node_modules/@types'",
"File '/node_modules/@types/jquery/package.json' does not exist.",
"File '/node_modules/@types/jquery/index.d.ts' does not exist.",
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory not set. ========",
"Root directory cannot be determined, skipping primary search paths.",
"Looking up in 'node_modules' folder, initial location '/src'",
"File '/src/node_modules/jquery.ts' does not exist.",
"File '/src/node_modules/jquery.d.ts' does not exist.",

View file

@ -0,0 +1,31 @@
//// [tests/cases/compiler/typeRootsFromMultipleNodeModulesDirectories.ts] ////
//// [index.d.ts]
declare module "xyz" {
export const x: number;
}
//// [index.d.ts]
declare module "pdq" {
export const y: number;
}
//// [index.d.ts]
declare module "abc" {
export const z: number;
}
//// [a.ts]
import { x } from "xyz";
import { y } from "pdq";
import { z } from "abc";
x + y + z;
//// [a.js]
"use strict";
var xyz_1 = require("xyz");
var pdq_1 = require("pdq");
var abc_1 = require("abc");
xyz_1.x + pdq_1.y + abc_1.z;

View file

@ -0,0 +1,34 @@
=== /foo/bar/a.ts ===
import { x } from "xyz";
>x : Symbol(x, Decl(a.ts, 0, 8))
import { y } from "pdq";
>y : Symbol(y, Decl(a.ts, 1, 8))
import { z } from "abc";
>z : Symbol(z, Decl(a.ts, 2, 8))
x + y + z;
>x : Symbol(x, Decl(a.ts, 0, 8))
>y : Symbol(y, Decl(a.ts, 1, 8))
>z : Symbol(z, Decl(a.ts, 2, 8))
=== /node_modules/@types/dopey/index.d.ts ===
declare module "xyz" {
export const x: number;
>x : Symbol(x, Decl(index.d.ts, 2, 16))
}
=== /foo/node_modules/@types/grumpy/index.d.ts ===
declare module "pdq" {
export const y: number;
>y : Symbol(y, Decl(index.d.ts, 1, 16))
}
=== /foo/node_modules/@types/sneezy/index.d.ts ===
declare module "abc" {
export const z: number;
>z : Symbol(z, Decl(index.d.ts, 1, 16))
}

View file

@ -0,0 +1,157 @@
[
"======== Resolving module 'xyz' from '/foo/bar/a.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'xyz' from 'node_modules' folder.",
"File '/foo/bar/node_modules/xyz.ts' does not exist.",
"File '/foo/bar/node_modules/xyz.tsx' does not exist.",
"File '/foo/bar/node_modules/xyz.d.ts' does not exist.",
"File '/foo/bar/node_modules/xyz/package.json' does not exist.",
"File '/foo/bar/node_modules/xyz/index.ts' does not exist.",
"File '/foo/bar/node_modules/xyz/index.tsx' does not exist.",
"File '/foo/bar/node_modules/xyz/index.d.ts' does not exist.",
"File '/foo/bar/node_modules/@types/xyz.ts' does not exist.",
"File '/foo/bar/node_modules/@types/xyz.tsx' does not exist.",
"File '/foo/bar/node_modules/@types/xyz.d.ts' does not exist.",
"File '/foo/bar/node_modules/@types/xyz/package.json' does not exist.",
"File '/foo/bar/node_modules/@types/xyz/index.ts' does not exist.",
"File '/foo/bar/node_modules/@types/xyz/index.tsx' does not exist.",
"File '/foo/bar/node_modules/@types/xyz/index.d.ts' does not exist.",
"File '/foo/node_modules/xyz.ts' does not exist.",
"File '/foo/node_modules/xyz.tsx' does not exist.",
"File '/foo/node_modules/xyz.d.ts' does not exist.",
"File '/foo/node_modules/xyz/package.json' does not exist.",
"File '/foo/node_modules/xyz/index.ts' does not exist.",
"File '/foo/node_modules/xyz/index.tsx' does not exist.",
"File '/foo/node_modules/xyz/index.d.ts' does not exist.",
"File '/foo/node_modules/@types/xyz.ts' does not exist.",
"File '/foo/node_modules/@types/xyz.tsx' does not exist.",
"File '/foo/node_modules/@types/xyz.d.ts' does not exist.",
"File '/foo/node_modules/@types/xyz/package.json' does not exist.",
"File '/foo/node_modules/@types/xyz/index.ts' does not exist.",
"File '/foo/node_modules/@types/xyz/index.tsx' does not exist.",
"File '/foo/node_modules/@types/xyz/index.d.ts' does not exist.",
"File '/node_modules/xyz.ts' does not exist.",
"File '/node_modules/xyz.tsx' does not exist.",
"File '/node_modules/xyz.d.ts' does not exist.",
"File '/node_modules/xyz/package.json' does not exist.",
"File '/node_modules/xyz/index.ts' does not exist.",
"File '/node_modules/xyz/index.tsx' does not exist.",
"File '/node_modules/xyz/index.d.ts' does not exist.",
"File '/node_modules/@types/xyz.ts' does not exist.",
"File '/node_modules/@types/xyz.tsx' does not exist.",
"File '/node_modules/@types/xyz.d.ts' does not exist.",
"File '/node_modules/@types/xyz/package.json' does not exist.",
"File '/node_modules/@types/xyz/index.ts' does not exist.",
"File '/node_modules/@types/xyz/index.tsx' does not exist.",
"File '/node_modules/@types/xyz/index.d.ts' does not exist.",
"======== Module name 'xyz' was not resolved. ========",
"======== Resolving module 'pdq' from '/foo/bar/a.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'pdq' from 'node_modules' folder.",
"File '/foo/bar/node_modules/pdq.ts' does not exist.",
"File '/foo/bar/node_modules/pdq.tsx' does not exist.",
"File '/foo/bar/node_modules/pdq.d.ts' does not exist.",
"File '/foo/bar/node_modules/pdq/package.json' does not exist.",
"File '/foo/bar/node_modules/pdq/index.ts' does not exist.",
"File '/foo/bar/node_modules/pdq/index.tsx' does not exist.",
"File '/foo/bar/node_modules/pdq/index.d.ts' does not exist.",
"File '/foo/bar/node_modules/@types/pdq.ts' does not exist.",
"File '/foo/bar/node_modules/@types/pdq.tsx' does not exist.",
"File '/foo/bar/node_modules/@types/pdq.d.ts' does not exist.",
"File '/foo/bar/node_modules/@types/pdq/package.json' does not exist.",
"File '/foo/bar/node_modules/@types/pdq/index.ts' does not exist.",
"File '/foo/bar/node_modules/@types/pdq/index.tsx' does not exist.",
"File '/foo/bar/node_modules/@types/pdq/index.d.ts' does not exist.",
"File '/foo/node_modules/pdq.ts' does not exist.",
"File '/foo/node_modules/pdq.tsx' does not exist.",
"File '/foo/node_modules/pdq.d.ts' does not exist.",
"File '/foo/node_modules/pdq/package.json' does not exist.",
"File '/foo/node_modules/pdq/index.ts' does not exist.",
"File '/foo/node_modules/pdq/index.tsx' does not exist.",
"File '/foo/node_modules/pdq/index.d.ts' does not exist.",
"File '/foo/node_modules/@types/pdq.ts' does not exist.",
"File '/foo/node_modules/@types/pdq.tsx' does not exist.",
"File '/foo/node_modules/@types/pdq.d.ts' does not exist.",
"File '/foo/node_modules/@types/pdq/package.json' does not exist.",
"File '/foo/node_modules/@types/pdq/index.ts' does not exist.",
"File '/foo/node_modules/@types/pdq/index.tsx' does not exist.",
"File '/foo/node_modules/@types/pdq/index.d.ts' does not exist.",
"File '/node_modules/pdq.ts' does not exist.",
"File '/node_modules/pdq.tsx' does not exist.",
"File '/node_modules/pdq.d.ts' does not exist.",
"File '/node_modules/pdq/package.json' does not exist.",
"File '/node_modules/pdq/index.ts' does not exist.",
"File '/node_modules/pdq/index.tsx' does not exist.",
"File '/node_modules/pdq/index.d.ts' does not exist.",
"File '/node_modules/@types/pdq.ts' does not exist.",
"File '/node_modules/@types/pdq.tsx' does not exist.",
"File '/node_modules/@types/pdq.d.ts' does not exist.",
"File '/node_modules/@types/pdq/package.json' does not exist.",
"File '/node_modules/@types/pdq/index.ts' does not exist.",
"File '/node_modules/@types/pdq/index.tsx' does not exist.",
"File '/node_modules/@types/pdq/index.d.ts' does not exist.",
"======== Module name 'pdq' was not resolved. ========",
"======== Resolving module 'abc' from '/foo/bar/a.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'abc' from 'node_modules' folder.",
"File '/foo/bar/node_modules/abc.ts' does not exist.",
"File '/foo/bar/node_modules/abc.tsx' does not exist.",
"File '/foo/bar/node_modules/abc.d.ts' does not exist.",
"File '/foo/bar/node_modules/abc/package.json' does not exist.",
"File '/foo/bar/node_modules/abc/index.ts' does not exist.",
"File '/foo/bar/node_modules/abc/index.tsx' does not exist.",
"File '/foo/bar/node_modules/abc/index.d.ts' does not exist.",
"File '/foo/bar/node_modules/@types/abc.ts' does not exist.",
"File '/foo/bar/node_modules/@types/abc.tsx' does not exist.",
"File '/foo/bar/node_modules/@types/abc.d.ts' does not exist.",
"File '/foo/bar/node_modules/@types/abc/package.json' does not exist.",
"File '/foo/bar/node_modules/@types/abc/index.ts' does not exist.",
"File '/foo/bar/node_modules/@types/abc/index.tsx' does not exist.",
"File '/foo/bar/node_modules/@types/abc/index.d.ts' does not exist.",
"File '/foo/node_modules/abc.ts' does not exist.",
"File '/foo/node_modules/abc.tsx' does not exist.",
"File '/foo/node_modules/abc.d.ts' does not exist.",
"File '/foo/node_modules/abc/package.json' does not exist.",
"File '/foo/node_modules/abc/index.ts' does not exist.",
"File '/foo/node_modules/abc/index.tsx' does not exist.",
"File '/foo/node_modules/abc/index.d.ts' does not exist.",
"File '/foo/node_modules/@types/abc.ts' does not exist.",
"File '/foo/node_modules/@types/abc.tsx' does not exist.",
"File '/foo/node_modules/@types/abc.d.ts' does not exist.",
"File '/foo/node_modules/@types/abc/package.json' does not exist.",
"File '/foo/node_modules/@types/abc/index.ts' does not exist.",
"File '/foo/node_modules/@types/abc/index.tsx' does not exist.",
"File '/foo/node_modules/@types/abc/index.d.ts' does not exist.",
"File '/node_modules/abc.ts' does not exist.",
"File '/node_modules/abc.tsx' does not exist.",
"File '/node_modules/abc.d.ts' does not exist.",
"File '/node_modules/abc/package.json' does not exist.",
"File '/node_modules/abc/index.ts' does not exist.",
"File '/node_modules/abc/index.tsx' does not exist.",
"File '/node_modules/abc/index.d.ts' does not exist.",
"File '/node_modules/@types/abc.ts' does not exist.",
"File '/node_modules/@types/abc.tsx' does not exist.",
"File '/node_modules/@types/abc.d.ts' does not exist.",
"File '/node_modules/@types/abc/package.json' does not exist.",
"File '/node_modules/@types/abc/index.ts' does not exist.",
"File '/node_modules/@types/abc/index.tsx' does not exist.",
"File '/node_modules/@types/abc/index.d.ts' does not exist.",
"======== Module name 'abc' was not resolved. ========",
"======== Resolving type reference directive 'grumpy', containing file '/src/__inferred type names__.ts', root directory '/foo/node_modules/@types,/node_modules/@types'. ========",
"Resolving with primary search path '/foo/node_modules/@types, /node_modules/@types'",
"File '/foo/node_modules/@types/grumpy/package.json' does not exist.",
"File '/foo/node_modules/@types/grumpy/index.d.ts' exist - use it as a name resolution result.",
"======== Type reference directive 'grumpy' was successfully resolved to '/foo/node_modules/@types/grumpy/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'sneezy', containing file '/src/__inferred type names__.ts', root directory '/foo/node_modules/@types,/node_modules/@types'. ========",
"Resolving with primary search path '/foo/node_modules/@types, /node_modules/@types'",
"File '/foo/node_modules/@types/sneezy/package.json' does not exist.",
"File '/foo/node_modules/@types/sneezy/index.d.ts' exist - use it as a name resolution result.",
"======== Type reference directive 'sneezy' was successfully resolved to '/foo/node_modules/@types/sneezy/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'dopey', containing file '/src/__inferred type names__.ts', root directory '/foo/node_modules/@types,/node_modules/@types'. ========",
"Resolving with primary search path '/foo/node_modules/@types, /node_modules/@types'",
"File '/foo/node_modules/@types/dopey/package.json' does not exist.",
"File '/foo/node_modules/@types/dopey/index.d.ts' does not exist.",
"File '/node_modules/@types/dopey/package.json' does not exist.",
"File '/node_modules/@types/dopey/index.d.ts' exist - use it as a name resolution result.",
"======== Type reference directive 'dopey' was successfully resolved to '/node_modules/@types/dopey/index.d.ts', primary: true. ========"
]

View file

@ -0,0 +1,36 @@
=== /foo/bar/a.ts ===
import { x } from "xyz";
>x : number
import { y } from "pdq";
>y : number
import { z } from "abc";
>z : number
x + y + z;
>x + y + z : number
>x + y : number
>x : number
>y : number
>z : number
=== /node_modules/@types/dopey/index.d.ts ===
declare module "xyz" {
export const x: number;
>x : number
}
=== /foo/node_modules/@types/grumpy/index.d.ts ===
declare module "pdq" {
export const y: number;
>y : number
}
=== /foo/node_modules/@types/sneezy/index.d.ts ===
declare module "abc" {
export const z: number;
>z : number
}

View file

@ -0,0 +1,17 @@
//// [tests/cases/compiler/typeRootsFromNodeModulesInParentDirectory.ts] ////
//// [index.d.ts]
declare module "xyz" {
export const x: number;
}
//// [a.ts]
import { x } from "xyz";
x;
//// [a.js]
"use strict";
var xyz_1 = require("xyz");
xyz_1.x;

View file

@ -0,0 +1,14 @@
=== /src/a.ts ===
import { x } from "xyz";
>x : Symbol(x, Decl(a.ts, 0, 8))
x;
>x : Symbol(x, Decl(a.ts, 0, 8))
=== /node_modules/@types/foo/index.d.ts ===
declare module "xyz" {
export const x: number;
>x : Symbol(x, Decl(index.d.ts, 2, 16))
}

View file

@ -0,0 +1,39 @@
[
"======== Resolving module 'xyz' from '/src/a.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'xyz' from 'node_modules' folder.",
"File '/src/node_modules/xyz.ts' does not exist.",
"File '/src/node_modules/xyz.tsx' does not exist.",
"File '/src/node_modules/xyz.d.ts' does not exist.",
"File '/src/node_modules/xyz/package.json' does not exist.",
"File '/src/node_modules/xyz/index.ts' does not exist.",
"File '/src/node_modules/xyz/index.tsx' does not exist.",
"File '/src/node_modules/xyz/index.d.ts' does not exist.",
"File '/src/node_modules/@types/xyz.ts' does not exist.",
"File '/src/node_modules/@types/xyz.tsx' does not exist.",
"File '/src/node_modules/@types/xyz.d.ts' does not exist.",
"File '/src/node_modules/@types/xyz/package.json' does not exist.",
"File '/src/node_modules/@types/xyz/index.ts' does not exist.",
"File '/src/node_modules/@types/xyz/index.tsx' does not exist.",
"File '/src/node_modules/@types/xyz/index.d.ts' does not exist.",
"File '/node_modules/xyz.ts' does not exist.",
"File '/node_modules/xyz.tsx' does not exist.",
"File '/node_modules/xyz.d.ts' does not exist.",
"File '/node_modules/xyz/package.json' does not exist.",
"File '/node_modules/xyz/index.ts' does not exist.",
"File '/node_modules/xyz/index.tsx' does not exist.",
"File '/node_modules/xyz/index.d.ts' does not exist.",
"File '/node_modules/@types/xyz.ts' does not exist.",
"File '/node_modules/@types/xyz.tsx' does not exist.",
"File '/node_modules/@types/xyz.d.ts' does not exist.",
"File '/node_modules/@types/xyz/package.json' does not exist.",
"File '/node_modules/@types/xyz/index.ts' does not exist.",
"File '/node_modules/@types/xyz/index.tsx' does not exist.",
"File '/node_modules/@types/xyz/index.d.ts' does not exist.",
"======== Module name 'xyz' was not resolved. ========",
"======== Resolving type reference directive 'foo', containing file '/src/__inferred type names__.ts', root directory '/node_modules/@types'. ========",
"Resolving with primary search path '/node_modules/@types'",
"File '/node_modules/@types/foo/package.json' does not exist.",
"File '/node_modules/@types/foo/index.d.ts' exist - use it as a name resolution result.",
"======== Type reference directive 'foo' was successfully resolved to '/node_modules/@types/foo/index.d.ts', primary: true. ========"
]

View file

@ -0,0 +1,14 @@
=== /src/a.ts ===
import { x } from "xyz";
>x : number
x;
>x : number
=== /node_modules/@types/foo/index.d.ts ===
declare module "xyz" {
export const x: number;
>x : number
}

View file

@ -0,0 +1,27 @@
// @noImplicitReferences: true
// @traceResolution: true
// @currentDirectory: /src
// @Filename: /node_modules/@types/dopey/index.d.ts
declare module "xyz" {
export const x: number;
}
// @Filename: /foo/node_modules/@types/grumpy/index.d.ts
declare module "pdq" {
export const y: number;
}
// @Filename: /foo/node_modules/@types/sneezy/index.d.ts
declare module "abc" {
export const z: number;
}
// @Filename: /foo/bar/a.ts
import { x } from "xyz";
import { y } from "pdq";
import { z } from "abc";
x + y + z;
// @Filename: /foo/bar/tsconfig.json
{}

View file

@ -0,0 +1,15 @@
// @noImplicitReferences: true
// @traceResolution: true
// @currentDirectory: /src
// @Filename: /node_modules/@types/foo/index.d.ts
declare module "xyz" {
export const x: number;
}
// @Filename: /src/a.ts
import { x } from "xyz";
x;
// @Filename: /src/tsconfig.json
{}