Skip to content

Commit 19820ca

Browse files
committed
Use more nodelike paths for import types when possible
1 parent a58054d commit 19820ca

8 files changed

+201
-2
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4085,7 +4085,7 @@ namespace ts {
40854085
// ambient module, just use declaration/symbol name (fallthrough)
40864086
}
40874087
else {
4088-
return `"${getResolvedExternalModuleName(context!.tracker.moduleResolverHost!, file, getSourceFileOfNode(getOriginalNode(context!.enclosingDeclaration)))}"`;
4088+
return `"${getResolvedExternalModuleNameForPossiblyExternalModule(context!.tracker.moduleResolverHost!, file, getSourceFileOfNode(getOriginalNode(context!.enclosingDeclaration)))}"`;
40894089
}
40904090
}
40914091
const declaration = symbol.declarations[0];

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5287,6 +5287,7 @@ namespace ts {
52875287
getCanonicalFileName(f: string): string;
52885288
getCommonSourceDirectory(): string;
52895289
getCurrentDirectory(): string;
5290+
getCompilerOptions(): CompilerOptions;
52905291
}
52915292

52925293
/** @deprecated See comment on SymbolWriter */

src/compiler/utilities.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,6 +2934,31 @@ namespace ts {
29342934
};
29352935
}
29362936

2937+
/** @internal */
2938+
export function getResolvedExternalModuleNameForPossiblyExternalModule(host: ModuleNameResolverHost, file: SourceFile, referenceFile?: SourceFile) {
2939+
const result = getResolvedExternalModuleName(host, file, referenceFile);
2940+
const opts = host.getCompilerOptions();
2941+
if (getEmitModuleResolutionKind(opts) === ModuleResolutionKind.NodeJs) {
2942+
// Trim leading paths to `node_modules` to allow node module resolution to find the thing in the future without an exact path
2943+
// This simplification means if a package.json for `foo` directs us to `foo/lib/main` instead of `foo/index` we'll write `foo/lib/main` over `foo`, however
2944+
const parts = getPathComponents(result);
2945+
if (parts[0] !== "") {
2946+
return result; // rooted path, leave as is
2947+
}
2948+
else {
2949+
parts.shift();
2950+
}
2951+
let index = 0;
2952+
while (parts[index] && (parts[index] === "." || parts[index] === "..")) {
2953+
index++;
2954+
}
2955+
if (parts[index] && parts[index] === "node_modules") {
2956+
return parts.slice(index + 1).join(directorySeparator);
2957+
}
2958+
}
2959+
return result;
2960+
}
2961+
29372962
export function getResolvedExternalModuleName(host: ModuleNameResolverHost, file: SourceFile, referenceFile?: SourceFile): string {
29382963
return file.moduleName || getExternalModuleNameFromPath(host, file.fileName, referenceFile && referenceFile.fileName);
29392964
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//// [tests/cases/compiler/declarationEmitCommonJsModuleReferencedType.ts] ////
2+
3+
//// [index.d.ts]
4+
export interface NestedProps {}
5+
//// [index.d.ts]
6+
export interface OtherIndexProps {}
7+
//// [other.d.ts]
8+
export interface OtherProps {}
9+
//// [index.d.ts]
10+
import { OtherProps } from "./other";
11+
import { OtherIndexProps } from "./other/index";
12+
import { NestedProps } from "nested";
13+
export interface SomeProps {}
14+
15+
export function foo(): [SomeProps, OtherProps, OtherIndexProps, NestedProps];
16+
//// [index.d.ts]
17+
export interface RootProps {}
18+
19+
export function bar(): RootProps;
20+
//// [entry.ts]
21+
import { foo } from "foo";
22+
import { bar } from "root";
23+
export const x = foo();
24+
export const y = bar();
25+
26+
27+
//// [entry.js]
28+
"use strict";
29+
exports.__esModule = true;
30+
var foo_1 = require("foo");
31+
var root_1 = require("root");
32+
exports.x = foo_1.foo();
33+
exports.y = root_1.bar();
34+
35+
36+
//// [entry.d.ts]
37+
export declare const x: [import("foo/index").SomeProps, import("foo/other").OtherProps, import("foo/other/index").OtherIndexProps, import("foo/node_modules/nested/index").NestedProps];
38+
export declare const y: import("root/index").RootProps;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
=== tests/cases/compiler/r/node_modules/foo/node_modules/nested/index.d.ts ===
2+
export interface NestedProps {}
3+
>NestedProps : Symbol(NestedProps, Decl(index.d.ts, 0, 0))
4+
5+
=== tests/cases/compiler/r/node_modules/foo/other/index.d.ts ===
6+
export interface OtherIndexProps {}
7+
>OtherIndexProps : Symbol(OtherIndexProps, Decl(index.d.ts, 0, 0))
8+
9+
=== tests/cases/compiler/r/node_modules/foo/other.d.ts ===
10+
export interface OtherProps {}
11+
>OtherProps : Symbol(OtherProps, Decl(other.d.ts, 0, 0))
12+
13+
=== tests/cases/compiler/r/node_modules/foo/index.d.ts ===
14+
import { OtherProps } from "./other";
15+
>OtherProps : Symbol(OtherProps, Decl(index.d.ts, 0, 8))
16+
17+
import { OtherIndexProps } from "./other/index";
18+
>OtherIndexProps : Symbol(OtherIndexProps, Decl(index.d.ts, 1, 8))
19+
20+
import { NestedProps } from "nested";
21+
>NestedProps : Symbol(NestedProps, Decl(index.d.ts, 2, 8))
22+
23+
export interface SomeProps {}
24+
>SomeProps : Symbol(SomeProps, Decl(index.d.ts, 2, 37))
25+
26+
export function foo(): [SomeProps, OtherProps, OtherIndexProps, NestedProps];
27+
>foo : Symbol(foo, Decl(index.d.ts, 3, 29))
28+
>SomeProps : Symbol(SomeProps, Decl(index.d.ts, 2, 37))
29+
>OtherProps : Symbol(OtherProps, Decl(index.d.ts, 0, 8))
30+
>OtherIndexProps : Symbol(OtherIndexProps, Decl(index.d.ts, 1, 8))
31+
>NestedProps : Symbol(NestedProps, Decl(index.d.ts, 2, 8))
32+
33+
=== tests/cases/compiler/node_modules/root/index.d.ts ===
34+
export interface RootProps {}
35+
>RootProps : Symbol(RootProps, Decl(index.d.ts, 0, 0))
36+
37+
export function bar(): RootProps;
38+
>bar : Symbol(bar, Decl(index.d.ts, 0, 29))
39+
>RootProps : Symbol(RootProps, Decl(index.d.ts, 0, 0))
40+
41+
=== tests/cases/compiler/r/entry.ts ===
42+
import { foo } from "foo";
43+
>foo : Symbol(foo, Decl(entry.ts, 0, 8))
44+
45+
import { bar } from "root";
46+
>bar : Symbol(bar, Decl(entry.ts, 1, 8))
47+
48+
export const x = foo();
49+
>x : Symbol(x, Decl(entry.ts, 2, 12))
50+
>foo : Symbol(foo, Decl(entry.ts, 0, 8))
51+
52+
export const y = bar();
53+
>y : Symbol(y, Decl(entry.ts, 3, 12))
54+
>bar : Symbol(bar, Decl(entry.ts, 1, 8))
55+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
=== tests/cases/compiler/r/node_modules/foo/node_modules/nested/index.d.ts ===
2+
export interface NestedProps {}
3+
>NestedProps : NestedProps
4+
5+
=== tests/cases/compiler/r/node_modules/foo/other/index.d.ts ===
6+
export interface OtherIndexProps {}
7+
>OtherIndexProps : OtherIndexProps
8+
9+
=== tests/cases/compiler/r/node_modules/foo/other.d.ts ===
10+
export interface OtherProps {}
11+
>OtherProps : OtherProps
12+
13+
=== tests/cases/compiler/r/node_modules/foo/index.d.ts ===
14+
import { OtherProps } from "./other";
15+
>OtherProps : any
16+
17+
import { OtherIndexProps } from "./other/index";
18+
>OtherIndexProps : any
19+
20+
import { NestedProps } from "nested";
21+
>NestedProps : any
22+
23+
export interface SomeProps {}
24+
>SomeProps : SomeProps
25+
26+
export function foo(): [SomeProps, OtherProps, OtherIndexProps, NestedProps];
27+
>foo : () => [SomeProps, OtherProps, OtherIndexProps, NestedProps]
28+
>SomeProps : SomeProps
29+
>OtherProps : OtherProps
30+
>OtherIndexProps : OtherIndexProps
31+
>NestedProps : NestedProps
32+
33+
=== tests/cases/compiler/node_modules/root/index.d.ts ===
34+
export interface RootProps {}
35+
>RootProps : RootProps
36+
37+
export function bar(): RootProps;
38+
>bar : () => RootProps
39+
>RootProps : RootProps
40+
41+
=== tests/cases/compiler/r/entry.ts ===
42+
import { foo } from "foo";
43+
>foo : () => [import("tests/cases/compiler/r/node_modules/foo/index").SomeProps, import("tests/cases/compiler/r/node_modules/foo/other").OtherProps, import("tests/cases/compiler/r/node_modules/foo/other/index").OtherIndexProps, import("tests/cases/compiler/r/node_modules/foo/node_modules/nested/index").NestedProps]
44+
45+
import { bar } from "root";
46+
>bar : () => import("tests/cases/compiler/node_modules/root/index").RootProps
47+
48+
export const x = foo();
49+
>x : [import("tests/cases/compiler/r/node_modules/foo/index").SomeProps, import("tests/cases/compiler/r/node_modules/foo/other").OtherProps, import("tests/cases/compiler/r/node_modules/foo/other/index").OtherIndexProps, import("tests/cases/compiler/r/node_modules/foo/node_modules/nested/index").NestedProps]
50+
>foo() : [import("tests/cases/compiler/r/node_modules/foo/index").SomeProps, import("tests/cases/compiler/r/node_modules/foo/other").OtherProps, import("tests/cases/compiler/r/node_modules/foo/other/index").OtherIndexProps, import("tests/cases/compiler/r/node_modules/foo/node_modules/nested/index").NestedProps]
51+
>foo : () => [import("tests/cases/compiler/r/node_modules/foo/index").SomeProps, import("tests/cases/compiler/r/node_modules/foo/other").OtherProps, import("tests/cases/compiler/r/node_modules/foo/other/index").OtherIndexProps, import("tests/cases/compiler/r/node_modules/foo/node_modules/nested/index").NestedProps]
52+
53+
export const y = bar();
54+
>y : import("tests/cases/compiler/node_modules/root/index").RootProps
55+
>bar() : import("tests/cases/compiler/node_modules/root/index").RootProps
56+
>bar : () => import("tests/cases/compiler/node_modules/root/index").RootProps
57+

tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ exports.thing = umd_1.makeThing();
2121

2222

2323
//// [index.d.ts]
24-
export declare const thing: import("./node_modules/umd").Thing;
24+
export declare const thing: import("umd").Thing;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @declaration: true
2+
// @filename: r/node_modules/foo/node_modules/nested/index.d.ts
3+
export interface NestedProps {}
4+
// @filename: r/node_modules/foo/other/index.d.ts
5+
export interface OtherIndexProps {}
6+
// @filename: r/node_modules/foo/other.d.ts
7+
export interface OtherProps {}
8+
// @filename: r/node_modules/foo/index.d.ts
9+
import { OtherProps } from "./other";
10+
import { OtherIndexProps } from "./other/index";
11+
import { NestedProps } from "nested";
12+
export interface SomeProps {}
13+
14+
export function foo(): [SomeProps, OtherProps, OtherIndexProps, NestedProps];
15+
// @filename: node_modules/root/index.d.ts
16+
export interface RootProps {}
17+
18+
export function bar(): RootProps;
19+
// @filename: r/entry.ts
20+
import { foo } from "foo";
21+
import { bar } from "root";
22+
export const x = foo();
23+
export const y = bar();

0 commit comments

Comments
 (0)