Skip to content

Commit e43377d

Browse files
author
Arthur Ozga
committed
Better qualified name handling
1 parent 818bfdc commit e43377d

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,7 +2278,7 @@ namespace ts {
22782278
result |= NodeBuilderFlags.AddUndefined;
22792279
}
22802280

2281-
return result;
2281+
return result;
22822282
}
22832283

22842284
function typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string {
@@ -2325,7 +2325,7 @@ namespace ts {
23252325
};
23262326

23272327
interface NodeBuilderContext {
2328-
readonly enclosingDeclaration: Node | undefined;
2328+
enclosingDeclaration: Node | undefined;
23292329
readonly flags: NodeBuilderFlags | undefined;
23302330

23312331
// State
@@ -2382,7 +2382,7 @@ namespace ts {
23822382
return createKeywordTypeNode(SyntaxKind.BooleanKeyword);
23832383
}
23842384
if (type.flags & TypeFlags.Enum) {
2385-
const name = symbolToName(type.symbol, /*expectsIdentifier*/ false, context);
2385+
const name = symbolToName(type.symbol, /*expectsIdentifier*/ false, SymbolFlags.Type, context);
23862386
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
23872387
}
23882388
if (type.flags & (TypeFlags.StringLiteral)) {
@@ -2396,7 +2396,7 @@ namespace ts {
23962396
}
23972397
if (type.flags & TypeFlags.EnumLiteral) {
23982398
const parentSymbol = getParentOfSymbol(type.symbol);
2399-
const parentName = symbolToName(parentSymbol, /*expectsIdentifier*/ false, context);
2399+
const parentName = symbolToName(parentSymbol, /*expectsIdentifier*/ false, SymbolFlags.Type, context);
24002400
const name = getNameOfSymbol(type.symbol, context);
24012401
const enumLiteralName = createQualifiedName(parentName, name);
24022402
return createTypeReferenceNode(enumLiteralName, /*typeArguments*/ undefined);
@@ -2436,19 +2436,19 @@ namespace ts {
24362436
}
24372437
if (objectFlags & ObjectFlags.ClassOrInterface) {
24382438
Debug.assert(!!(type.flags & TypeFlags.Object));
2439-
const name = symbolToName(type.symbol, /*expectsIdentifier*/ false, context);
2439+
const name = symbolToName(type.symbol, /*expectsIdentifier*/ false, SymbolFlags.Type, context);
24402440
// TODO(aozgaa): handle type arguments.
24412441
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
24422442
}
24432443
if (type.flags & TypeFlags.TypeParameter) {
2444-
const name = symbolToName(type.symbol, /*expectsIdentifier*/ false, context);
2444+
const name = symbolToName(type.symbol, /*expectsIdentifier*/ false, SymbolFlags.Type, context);
24452445
// Ignore constraint/default when creating a usage (as opposed to declaration) of a type parameter.
24462446
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
24472447
}
24482448

24492449
if (!inTypeAlias && type.aliasSymbol &&
24502450
isSymbolAccessible(type.aliasSymbol, context.enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
2451-
const name = symbolToName(type.aliasSymbol, /*expectsIdentifier*/ false, context);
2451+
const name = symbolToName(type.aliasSymbol, /*expectsIdentifier*/ false, SymbolFlags.Type, context);
24522452
const typeArgumentNodes = type.aliasTypeArguments && mapToTypeNodeArray(type.aliasTypeArguments, /*addInElementTypeFlag*/ false);
24532453
return createTypeReferenceNode(name, typeArgumentNodes);
24542454
}
@@ -2526,14 +2526,14 @@ namespace ts {
25262526
if (symbol.flags & SymbolFlags.Class && !getBaseTypeVariableOfClass(symbol) ||
25272527
symbol.flags & (SymbolFlags.Enum | SymbolFlags.ValueModule) ||
25282528
shouldWriteTypeOfFunctionSymbol()) {
2529-
return createTypeQueryNodeFromSymbol(symbol);
2529+
return createTypeQueryNodeFromSymbol(symbol, SymbolFlags.Value);
25302530
}
25312531
else if (contains(context.symbolStack, symbol)) {
25322532
// If type is an anonymous type literal in a type alias declaration, use type alias name
25332533
const typeAlias = getTypeAliasForTypeLiteral(type);
25342534
if (typeAlias) {
25352535
// The specified symbol flags need to be reinterpreted as type flags
2536-
const entityName = symbolToName(typeAlias, /*expectsIdentifier*/ false, context);
2536+
const entityName = symbolToName(typeAlias, /*expectsIdentifier*/ false, SymbolFlags.Type, context);
25372537
return createTypeReferenceNode(entityName, /*typeArguments*/ undefined);
25382538
}
25392539
else {
@@ -2618,8 +2618,8 @@ namespace ts {
26182618
return false;
26192619
}
26202620

2621-
function createTypeQueryNodeFromSymbol(symbol: Symbol) {
2622-
const entityName = symbolToName(symbol, /*expectsIdentifier*/ false, context);
2621+
function createTypeQueryNodeFromSymbol(symbol: Symbol, symbolFlags: SymbolFlags) {
2622+
const entityName = symbolToName(symbol, /*expectsIdentifier*/ false, symbolFlags, context);
26232623
return createTypeQueryNode(entityName);
26242624
}
26252625

@@ -2665,7 +2665,7 @@ namespace ts {
26652665
// the default outer type arguments), we don't show the group.
26662666
if (!rangeEquals(outerTypeParameters, typeArguments, start, i)) {
26672667
// inFirstTypeArgument???
2668-
const qualifiedNamePart = symbolToName(parent, /*expectsIdentifier*/ true, context);
2668+
const qualifiedNamePart = symbolToName(parent, /*expectsIdentifier*/ true, SymbolFlags.Type, context);
26692669
if (!qualifiedName) {
26702670
qualifiedName = createQualifiedName(qualifiedNamePart, /*right*/ undefined);
26712671
}
@@ -2679,7 +2679,7 @@ namespace ts {
26792679
}
26802680
}
26812681
let entityName: EntityName = undefined;
2682-
const nameIdentifier = symbolToName(type.symbol, /*expectsIdentifier*/ true, context);
2682+
const nameIdentifier = symbolToName(type.symbol, /*expectsIdentifier*/ true, SymbolFlags.Type, context);
26832683
if (qualifiedName) {
26842684
Debug.assert(!qualifiedName.right);
26852685
qualifiedName.right = nameIdentifier;
@@ -2717,7 +2717,10 @@ namespace ts {
27172717
// TODO: make logic mirror that of writeObjectLiteralType
27182718
for (const propertySymbol of properties) {
27192719
const propertyType = getTypeOfSymbol(propertySymbol);
2720-
const propertyName = symbolToName(propertySymbol, /*expectsIdentifier*/ true, context);
2720+
const saveEnclosingDeclaration = context.enclosingDeclaration;
2721+
context.enclosingDeclaration = undefined;
2722+
const propertyName = symbolToName(propertySymbol, /*expectsIdentifier*/ true, SymbolFlags.Value, context);
2723+
context.enclosingDeclaration = saveEnclosingDeclaration;
27212724
const optionalToken = propertySymbol.flags & SymbolFlags.Optional ? createToken(SyntaxKind.QuestionToken) : undefined;
27222725
if (propertySymbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && !getPropertiesOfObjectType(propertyType).length) {
27232726
const signatures = getSignaturesOfType(propertyType, SignatureKind.Call);
@@ -2767,7 +2770,6 @@ namespace ts {
27672770
}
27682771

27692772
function signatureToSignatureDeclarationHelper(signature: Signature, kind: SyntaxKind, context: NodeBuilderContext): SignatureDeclaration {
2770-
27712773
const typeParameters = signature.typeParameters && signature.typeParameters.map(parameter => typeParameterToDeclaration(parameter, context));
27722774
const parameters = signature.parameters.map(parameter => symbolToParameterDeclaration(parameter, context));
27732775
if (signature.thisParameter) {
@@ -2801,7 +2803,7 @@ namespace ts {
28012803
const constraintNode = constraint && typeToTypeNodeHelper(constraint, context);
28022804
const defaultParameter = getDefaultFromTypeParameter(type);
28032805
const defaultParameterNode = defaultParameter && typeToTypeNodeHelper(defaultParameter, context);
2804-
const name = symbolToName(type.symbol, /*expectsIdentifier*/ true, context);
2806+
const name = symbolToName(type.symbol, /*expectsIdentifier*/ true, SymbolFlags.Type, context);
28052807
return createTypeParameterDeclaration(name, constraintNode, defaultParameterNode);
28062808
}
28072809

@@ -2825,15 +2827,15 @@ namespace ts {
28252827

28262828
// TODO: add meaning: SymbolFlags argument.
28272829
// TODO: add SymbolFormatFlags?? Yes to add outer type parameters. Defer UseOnlyExternalAliasing until a separate symbolbuilder PR.
2828-
function symbolToName(symbol: Symbol, expectsIdentifier: true, context: NodeBuilderContext): Identifier;
2829-
function symbolToName(symbol: Symbol, expectsIdentifier: false, context: NodeBuilderContext): EntityName;
2830-
function symbolToName(symbol: Symbol, expectsIdentifier: boolean, context: NodeBuilderContext): EntityName {
2830+
function symbolToName(symbol: Symbol, expectsIdentifier: true, meaning: SymbolFlags, context: NodeBuilderContext): Identifier;
2831+
function symbolToName(symbol: Symbol, expectsIdentifier: false, meaning: SymbolFlags, context: NodeBuilderContext): EntityName;
2832+
function symbolToName(symbol: Symbol, expectsIdentifier: boolean, meaning: SymbolFlags, context: NodeBuilderContext): EntityName {
28312833

28322834
// Try to get qualified name if the symbol is not a type parameter and there is an enclosing declaration.
28332835
let chain: Symbol[];
28342836
const isTypeParameter = symbol.flags & SymbolFlags.TypeParameter;
28352837
if (!isTypeParameter && context.enclosingDeclaration) {
2836-
chain = getSymbolChain(symbol, SymbolFlags.None, /*endOfChain*/ true);
2838+
chain = getSymbolChain(symbol, meaning, /*endOfChain*/ true);
28372839
Debug.assert(chain && chain.length > 0);
28382840
}
28392841
else {

0 commit comments

Comments
 (0)