Skip to content

Commit 10c8d5e

Browse files
author
Andy
authored
In services, show the aliasSymbol for a type even if it's not accessible in the current scope (#17433)
* In services, show the aliasSymbol for a type even if it's not accessible in the current scope * Rename flag
1 parent 80a7ed9 commit 10c8d5e

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
@@ -2156,6 +2156,11 @@ namespace ts {
21562156
return false;
21572157
}
21582158

2159+
function isTypeSymbolAccessible(typeSymbol: Symbol, enclosingDeclaration: Node): boolean {
2160+
const access = isSymbolAccessible(typeSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false);
2161+
return access.accessibility === SymbolAccessibility.Accessible;
2162+
}
2163+
21592164
/**
21602165
* Check if the given symbol in given enclosing declaration is accessible and mark all associated alias to be visible if requested
21612166
*
@@ -2486,8 +2491,7 @@ namespace ts {
24862491
// Ignore constraint/default when creating a usage (as opposed to declaration) of a type parameter.
24872492
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
24882493
}
2489-
if (!inTypeAlias && type.aliasSymbol &&
2490-
isSymbolAccessible(type.aliasSymbol, context.enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
2494+
if (!inTypeAlias && type.aliasSymbol && isTypeSymbolAccessible(type.aliasSymbol, context.enclosingDeclaration)) {
24912495
const name = symbolToTypeReferenceName(type.aliasSymbol);
24922496
const typeArgumentNodes = mapToTypeNodes(type.aliasTypeArguments, context);
24932497
return createTypeReferenceNode(name, typeArgumentNodes);
@@ -3254,7 +3258,7 @@ namespace ts {
32543258
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, nextFlags);
32553259
}
32563260
else if (!(flags & TypeFormatFlags.InTypeAlias) && type.aliasSymbol &&
3257-
isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
3261+
((flags & TypeFormatFlags.UseAliasDefinedOutsideCurrentScope) || isTypeSymbolAccessible(type.aliasSymbol, enclosingDeclaration))) {
32583262
const typeArguments = type.aliasTypeArguments;
32593263
writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, length(typeArguments), nextFlags);
32603264
}

src/compiler/types.ts

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

27272729
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.UseAliasDefinedOutsideCurrentScope;
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)