Skip to content

Check if module reference can use a type reference before using a path reference #25385

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

Merged
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3744,7 +3744,7 @@ namespace ts {
const ambientDecls = filter(symbol.declarations, isAmbientModule);
if (length(ambientDecls)) {
for (const decl of ambientDecls) {
context.tracker.trackReferencedAmbientModule(decl);
context.tracker.trackReferencedAmbientModule(decl, symbol);
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ namespace ts {
}
}

function trackReferencedAmbientModule(node: ModuleDeclaration) {
function trackReferencedAmbientModule(node: ModuleDeclaration, symbol: Symbol) {
// If it is visible via `// <reference types="..."/>`, then we should just use that
const directives = resolver.getTypeReferenceDirectivesForSymbol(symbol, SymbolFlags.All);
if (length(directives)) {
return recordTypeReferenceDirectivesIfNecessary(directives);
}
// Otherwise we should emit a path-based reference
const container = getSourceFileOfNode(node);
refs.set("" + getOriginalNodeId(container), container);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5312,7 +5312,7 @@ namespace ts {
reportPrivateInBaseOfClassExpression?(propertyName: string): void;
reportInaccessibleUniqueSymbolError?(): void;
moduleResolverHost?: ModuleSpecifierResolutionHost;
trackReferencedAmbientModule?(decl: ModuleDeclaration): void;
trackReferencedAmbientModule?(decl: ModuleDeclaration, symbol: Symbol): void;
}

export interface TextSpan {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4493,7 +4493,7 @@ declare namespace ts {
reportPrivateInBaseOfClassExpression?(propertyName: string): void;
reportInaccessibleUniqueSymbolError?(): void;
moduleResolverHost?: ModuleSpecifierResolutionHost;
trackReferencedAmbientModule?(decl: ModuleDeclaration): void;
trackReferencedAmbientModule?(decl: ModuleDeclaration, symbol: Symbol): void;
}
interface TextSpan {
start: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/referenceTypesPreferedToPathIfPossible.ts] ////

//// [index.d.ts]
declare module "url" {
export class Url {}
export function parse(): Url;
}
//// [usage.ts]
import { parse } from "url";
export const thing = () => parse();


//// [usage.js]
"use strict";
exports.__esModule = true;
var url_1 = require("url");
exports.thing = function () { return url_1.parse(); };


//// [usage.d.ts]
/// <reference types="node" />
export declare const thing: () => import("url").Url;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/usage.ts ===
import { parse } from "url";
>parse : Symbol(parse, Decl(usage.ts, 0, 8))

export const thing = () => parse();
>thing : Symbol(thing, Decl(usage.ts, 1, 12))
>parse : Symbol(parse, Decl(usage.ts, 0, 8))

=== node_modules/@types/node/index.d.ts ===
declare module "url" {
>"url" : Symbol("url", Decl(index.d.ts, 0, 0))

export class Url {}
>Url : Symbol(Url, Decl(index.d.ts, 0, 22))

export function parse(): Url;
>parse : Symbol(parse, Decl(index.d.ts, 1, 23))
>Url : Symbol(Url, Decl(index.d.ts, 0, 22))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/usage.ts ===
import { parse } from "url";
>parse : () => import("url").Url

export const thing = () => parse();
>thing : () => import("url").Url
>() => parse() : () => import("url").Url
>parse() : import("url").Url
>parse : () => import("url").Url

=== node_modules/@types/node/index.d.ts ===
declare module "url" {
>"url" : typeof import("url")

export class Url {}
>Url : Url

export function parse(): Url;
>parse : () => Url
>Url : Url
}
10 changes: 10 additions & 0 deletions tests/cases/compiler/referenceTypesPreferedToPathIfPossible.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @declaration: true
// @noImplicitReferences: true
// @filename: /.src/node_modules/@types/node/index.d.ts
declare module "url" {
export class Url {}
export function parse(): Url;
}
// @filename: usage.ts
import { parse } from "url";
export const thing = () => parse();