Skip to content

Commit ba1c0d9

Browse files
author
Andy Hanson
committed
In services, show the aliasSymbol for a type even if it's not accessible in the current scope
1 parent 61c6ecb commit ba1c0d9

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,11 @@ namespace ts {
21262126
return false;
21272127
}
21282128

2129+
function isTypeSymbolAccessible(typeSymbol: Symbol, enclosingDeclaration: Node): boolean {
2130+
const access = isSymbolAccessible(typeSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false);
2131+
return access.accessibility === SymbolAccessibility.Accessible;
2132+
}
2133+
21292134
/**
21302135
* Check if the given symbol in given enclosing declaration is accessible and mark all associated alias to be visible if requested
21312136
*
@@ -2456,8 +2461,7 @@ namespace ts {
24562461
// Ignore constraint/default when creating a usage (as opposed to declaration) of a type parameter.
24572462
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
24582463
}
2459-
if (!inTypeAlias && type.aliasSymbol &&
2460-
isSymbolAccessible(type.aliasSymbol, context.enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
2464+
if (!inTypeAlias && type.aliasSymbol && isTypeSymbolAccessible(type.aliasSymbol, context.enclosingDeclaration)) {
24612465
const name = symbolToTypeReferenceName(type.aliasSymbol);
24622466
const typeArgumentNodes = mapToTypeNodes(type.aliasTypeArguments, context);
24632467
return createTypeReferenceNode(name, typeArgumentNodes);
@@ -3226,7 +3230,7 @@ namespace ts {
32263230
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, nextFlags);
32273231
}
32283232
else if (!(flags & TypeFormatFlags.InTypeAlias) && type.aliasSymbol &&
3229-
isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
3233+
((flags & TypeFormatFlags.UseAliasEvenIfNotAccessible) || isTypeSymbolAccessible(type.aliasSymbol, enclosingDeclaration))) {
32303234
const typeArguments = type.aliasTypeArguments;
32313235
writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, length(typeArguments), nextFlags);
32323236
}

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,6 +2691,8 @@ namespace ts {
26912691
AddUndefined = 1 << 13, // Add undefined to types of initialized, non-optional parameters
26922692
WriteClassExpressionAsTypeLiteral = 1 << 14, // Write a type literal instead of (Anonymous class)
26932693
InArrayType = 1 << 15, // Writing an array element type
2694+
UseAliasEvenIfNotAccessible = 1 << 16, // For a `type T = ... ` defined in a different file, write `T` instead of its value,
2695+
// even though `T` can't be accessed in the current scope.
26942696
}
26952697

26962698
export const enum SymbolFormatFlags {

src/services/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,7 @@ namespace ts {
12611261
}
12621262

12631263
export function signatureToDisplayParts(typechecker: TypeChecker, signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags): SymbolDisplayPart[] {
1264+
flags |= TypeFormatFlags.UseAliasEvenIfNotAccessible;
12641265
return mapToDisplayParts(writer => {
12651266
typechecker.getSymbolDisplayBuilder().buildSignatureDisplay(signature, writer, enclosingDeclaration, flags);
12661267
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////export type X = { x: number };
5+
////export function f(x: X): void {}
6+
7+
// @Filename: /b.ts
8+
////import { f } from "./a";
9+
/////**/f({ x: 1 });
10+
11+
verify.quickInfoAt("", "(alias) f(x: X): void\nimport f");

0 commit comments

Comments
 (0)