Skip to content

Commit c78c88f

Browse files
committed
Fix references to path-mapped ambient modules in declaration files
1 parent 370a596 commit c78c88f

8 files changed

+139
-0
lines changed

src/compiler/emitter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ namespace ts {
752752
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
753753
getProgramBuildInfo: returnUndefined,
754754
getSourceFileFromReference: returnUndefined,
755+
redirectTargetsMap: createMultiMap()
755756
};
756757
emitFiles(
757758
notImplementedResolver,

src/compiler/program.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,7 @@ namespace ts {
14461446
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
14471447
getProgramBuildInfo: () => program.getProgramBuildInfo && program.getProgramBuildInfo(),
14481448
getSourceFileFromReference: (file, ref) => program.getSourceFileFromReference(file, ref),
1449+
redirectTargetsMap,
14491450
};
14501451
}
14511452

src/compiler/transformers/declarations.ts

+20
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,26 @@ namespace ts {
327327
}
328328

329329
if (declFileName) {
330+
const specifier = moduleSpecifiers.getModuleSpecifier(
331+
// We pathify the baseUrl since we pathify the other paths here, so we can still easily check if the other paths are within the baseUrl
332+
// TODO: Should we _always_ be pathifying the baseUrl as we read it in?
333+
{...options, baseUrl: options.baseUrl && toPath(options.baseUrl, host.getCurrentDirectory(), host.getCanonicalFileName)},
334+
currentSourceFile,
335+
toPath(outputFilePath, host.getCurrentDirectory(), host.getCanonicalFileName),
336+
toPath(declFileName, host.getCurrentDirectory(), host.getCanonicalFileName),
337+
host,
338+
host.getSourceFiles(),
339+
/*preferences*/ undefined,
340+
host.redirectTargetsMap
341+
);
342+
if (!pathIsRelative(specifier)) {
343+
// If some compiler option/symlink/whatever allows access to the file containing the ambient module declaration
344+
// via a non-relative name, emit a type reference directive to that non-relative name, rather than
345+
// a relative path to the declaration file
346+
recordTypeReferenceDirectivesIfNecessary([specifier]);
347+
return;
348+
}
349+
330350
let fileName = getRelativePathToDirectoryOrUrl(
331351
outputFilePath,
332352
declFileName,

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5440,6 +5440,7 @@ namespace ts {
54405440
writeFile: WriteFileCallback;
54415441
getProgramBuildInfo(): ProgramBuildInfo | undefined;
54425442
getSourceFileFromReference: Program["getSourceFileFromReference"];
5443+
readonly redirectTargetsMap: RedirectTargetsMap;
54435444
}
54445445

54455446
export interface TransformationContext {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/declarationEmitPathMappingMonorepo.ts] ////
2+
3+
//// [index.d.ts]
4+
declare module "@ts-bug/a" {
5+
export type AText = {
6+
value: string;
7+
};
8+
export function a(text: string): AText;
9+
}
10+
11+
//// [index.ts]
12+
import { a } from "@ts-bug/a";
13+
14+
export function b(text: string) {
15+
return a(text);
16+
}
17+
18+
//// [index.js]
19+
"use strict";
20+
exports.__esModule = true;
21+
var a_1 = require("@ts-bug/a");
22+
function b(text) {
23+
return a_1.a(text);
24+
}
25+
exports.b = b;
26+
27+
28+
//// [index.d.ts]
29+
/// <reference types="@ts-bug/a" />
30+
export declare function b(text: string): import("@ts-bug/a").AText;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/packages/a/index.d.ts ===
2+
declare module "@ts-bug/a" {
3+
>"@ts-bug/a" : Symbol("@ts-bug/a", Decl(index.d.ts, 0, 0))
4+
5+
export type AText = {
6+
>AText : Symbol(AText, Decl(index.d.ts, 0, 28))
7+
8+
value: string;
9+
>value : Symbol(value, Decl(index.d.ts, 1, 25))
10+
11+
};
12+
export function a(text: string): AText;
13+
>a : Symbol(a, Decl(index.d.ts, 3, 6))
14+
>text : Symbol(text, Decl(index.d.ts, 4, 22))
15+
>AText : Symbol(AText, Decl(index.d.ts, 0, 28))
16+
}
17+
18+
=== tests/cases/compiler/packages/b/src/index.ts ===
19+
import { a } from "@ts-bug/a";
20+
>a : Symbol(a, Decl(index.ts, 0, 8))
21+
22+
export function b(text: string) {
23+
>b : Symbol(b, Decl(index.ts, 0, 30))
24+
>text : Symbol(text, Decl(index.ts, 2, 18))
25+
26+
return a(text);
27+
>a : Symbol(a, Decl(index.ts, 0, 8))
28+
>text : Symbol(text, Decl(index.ts, 2, 18))
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/packages/a/index.d.ts ===
2+
declare module "@ts-bug/a" {
3+
>"@ts-bug/a" : typeof import("@ts-bug/a")
4+
5+
export type AText = {
6+
>AText : AText
7+
8+
value: string;
9+
>value : string
10+
11+
};
12+
export function a(text: string): AText;
13+
>a : (text: string) => AText
14+
>text : string
15+
}
16+
17+
=== tests/cases/compiler/packages/b/src/index.ts ===
18+
import { a } from "@ts-bug/a";
19+
>a : (text: string) => import("@ts-bug/a").AText
20+
21+
export function b(text: string) {
22+
>b : (text: string) => import("@ts-bug/a").AText
23+
>text : string
24+
25+
return a(text);
26+
>a(text) : import("@ts-bug/a").AText
27+
>a : (text: string) => import("@ts-bug/a").AText
28+
>text : string
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @filename: packages/a/index.d.ts
2+
declare module "@ts-bug/a" {
3+
export type AText = {
4+
value: string;
5+
};
6+
export function a(text: string): AText;
7+
}
8+
9+
// @filename: packages/b/src/index.ts
10+
import { a } from "@ts-bug/a";
11+
12+
export function b(text: string) {
13+
return a(text);
14+
}
15+
// @filename: packages/b/tsconfig.json
16+
{
17+
"compilerOptions": {
18+
"outDir": "dist",
19+
"declaration": true,
20+
"baseUrl": ".",
21+
"paths": {
22+
"@ts-bug/a": ["../a"]
23+
}
24+
}
25+
}
26+
27+
// @link: tests/cases/compiler/packages/a -> tests/cases/compiler/node_modules/@ts-bug/a
28+
// @link: tests/cases/compiler/packages/b -> tests/cases/compiler/node_modules/@ts-bug/b

0 commit comments

Comments
 (0)