Check if module reference requires type reference before path reference (#25385)
This commit is contained in:
parent
304d45d833
commit
cb4cb7a75f
|
@ -3747,7 +3747,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -5316,7 +5316,7 @@ namespace ts {
|
|||
reportPrivateInBaseOfClassExpression?(propertyName: string): void;
|
||||
reportInaccessibleUniqueSymbolError?(): void;
|
||||
moduleResolverHost?: ModuleSpecifierResolutionHost;
|
||||
trackReferencedAmbientModule?(decl: ModuleDeclaration): void;
|
||||
trackReferencedAmbientModule?(decl: ModuleDeclaration, symbol: Symbol): void;
|
||||
}
|
||||
|
||||
export interface TextSpan {
|
||||
|
|
|
@ -4497,7 +4497,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;
|
||||
|
|
|
@ -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;
|
|
@ -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))
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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();
|
Loading…
Reference in a new issue