Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix references to path-mapped ambient modules in declaration files #32878

Merged
merged 2 commits into from
Aug 13, 2019
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
1 change: 1 addition & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ namespace ts {
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
getProgramBuildInfo: returnUndefined,
getSourceFileFromReference: returnUndefined,
redirectTargetsMap: createMultiMap()
};
emitFiles(
notImplementedResolver,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,7 @@ namespace ts {
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
getProgramBuildInfo: () => program.getProgramBuildInfo && program.getProgramBuildInfo(),
getSourceFileFromReference: (file, ref) => program.getSourceFileFromReference(file, ref),
redirectTargetsMap,
};
}

Expand Down
20 changes: 20 additions & 0 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,26 @@ namespace ts {
}

if (declFileName) {
const specifier = moduleSpecifiers.getModuleSpecifier(
// 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
// TODO: Should we _always_ be pathifying the baseUrl as we read it in?
{ ...options, baseUrl: options.baseUrl && toPath(options.baseUrl, host.getCurrentDirectory(), host.getCanonicalFileName) },
currentSourceFile,
toPath(outputFilePath, host.getCurrentDirectory(), host.getCanonicalFileName),
toPath(declFileName, host.getCurrentDirectory(), host.getCanonicalFileName),
host,
host.getSourceFiles(),
/*preferences*/ undefined,
host.redirectTargetsMap
);
if (!pathIsRelative(specifier)) {
// If some compiler option/symlink/whatever allows access to the file containing the ambient module declaration
// via a non-relative name, emit a type reference directive to that non-relative name, rather than
// a relative path to the declaration file
recordTypeReferenceDirectivesIfNecessary([specifier]);
return;
}

let fileName = getRelativePathToDirectoryOrUrl(
outputFilePath,
declFileName,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5440,6 +5440,7 @@ namespace ts {
writeFile: WriteFileCallback;
getProgramBuildInfo(): ProgramBuildInfo | undefined;
getSourceFileFromReference: Program["getSourceFileFromReference"];
readonly redirectTargetsMap: RedirectTargetsMap;
}

export interface TransformationContext {
Expand Down
30 changes: 30 additions & 0 deletions tests/baselines/reference/declarationEmitPathMappingMonorepo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/declarationEmitPathMappingMonorepo.ts] ////

//// [index.d.ts]
declare module "@ts-bug/a" {
export type AText = {
value: string;
};
export function a(text: string): AText;
}

//// [index.ts]
import { a } from "@ts-bug/a";

export function b(text: string) {
return a(text);
}

//// [index.js]
"use strict";
exports.__esModule = true;
var a_1 = require("@ts-bug/a");
function b(text) {
return a_1.a(text);
}
exports.b = b;


//// [index.d.ts]
/// <reference types="@ts-bug/a" />
export declare function b(text: string): import("@ts-bug/a").AText;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside, since the import is import("@ts-bug/a"), we don't need the /// <reference types="@ts-bug/a" />, since they refer to the "same" thing (or at least cause the same file to be found and included). However since the import could be something like @ts-bug/a/b (which could be an ambient module in @ts-bug/a), we can't generally elide the reference. A cleanup pass where we remove types directives which exactly match an import somewhere in the file may be pertinent.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/packages/a/index.d.ts ===
declare module "@ts-bug/a" {
>"@ts-bug/a" : Symbol("@ts-bug/a", Decl(index.d.ts, 0, 0))

export type AText = {
>AText : Symbol(AText, Decl(index.d.ts, 0, 28))

value: string;
>value : Symbol(value, Decl(index.d.ts, 1, 25))

};
export function a(text: string): AText;
>a : Symbol(a, Decl(index.d.ts, 3, 6))
>text : Symbol(text, Decl(index.d.ts, 4, 22))
>AText : Symbol(AText, Decl(index.d.ts, 0, 28))
}

=== tests/cases/compiler/packages/b/src/index.ts ===
import { a } from "@ts-bug/a";
>a : Symbol(a, Decl(index.ts, 0, 8))

export function b(text: string) {
>b : Symbol(b, Decl(index.ts, 0, 30))
>text : Symbol(text, Decl(index.ts, 2, 18))

return a(text);
>a : Symbol(a, Decl(index.ts, 0, 8))
>text : Symbol(text, Decl(index.ts, 2, 18))
}
29 changes: 29 additions & 0 deletions tests/baselines/reference/declarationEmitPathMappingMonorepo.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/packages/a/index.d.ts ===
declare module "@ts-bug/a" {
>"@ts-bug/a" : typeof import("@ts-bug/a")

export type AText = {
>AText : AText

value: string;
>value : string

};
export function a(text: string): AText;
>a : (text: string) => AText
>text : string
}

=== tests/cases/compiler/packages/b/src/index.ts ===
import { a } from "@ts-bug/a";
>a : (text: string) => import("@ts-bug/a").AText

export function b(text: string) {
>b : (text: string) => import("@ts-bug/a").AText
>text : string

return a(text);
>a(text) : import("@ts-bug/a").AText
>a : (text: string) => import("@ts-bug/a").AText
>text : string
}
28 changes: 28 additions & 0 deletions tests/cases/compiler/declarationEmitPathMappingMonorepo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @filename: packages/a/index.d.ts
declare module "@ts-bug/a" {
export type AText = {
value: string;
};
export function a(text: string): AText;
}

// @filename: packages/b/src/index.ts
import { a } from "@ts-bug/a";

export function b(text: string) {
return a(text);
}
// @filename: packages/b/tsconfig.json
{
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"baseUrl": ".",
"paths": {
"@ts-bug/a": ["../a"]
}
}
}

// @link: tests/cases/compiler/packages/a -> tests/cases/compiler/node_modules/@ts-bug/a
// @link: tests/cases/compiler/packages/b -> tests/cases/compiler/node_modules/@ts-bug/b