Skip to content

Commit 8882f52

Browse files
Port PR#60052: Update computed name handling in declarations
Co-authored-by: RyanCavanaugh <[email protected]>
1 parent aa90167 commit 8882f52

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

internal/checker/checker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4549,9 +4549,9 @@ func (c *Checker) checkIndexConstraints(t *Type, symbol *ast.Symbol, isStaticInd
45494549
typeDeclaration := symbol.ValueDeclaration
45504550
if typeDeclaration != nil && ast.IsClassLike(typeDeclaration) {
45514551
for _, member := range typeDeclaration.Members() {
4552-
// Only process instance properties with computed names here. Static properties cannot be in conflict with indexers,
4553-
// and properties with literal names were already checked.
4554-
if !ast.IsStatic(member) && !c.hasBindableName(member) {
4552+
// Only process instance properties against instance index signatures and static properties against static index signatures
4553+
if ((!isStaticIndex && !ast.IsStatic(member)) ||
4554+
(isStaticIndex && ast.IsStatic(member))) && !c.hasBindableName(member) {
45554555
symbol := c.getSymbolOfDeclaration(member)
45564556
c.checkIndexConstraintForProperty(t, symbol, c.getTypeOfExpression(member.Name().Expression()), c.getNonMissingTypeOfSymbol(symbol))
45574557
}

internal/checker/nodebuilderimpl.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,48 @@ func (b *nodeBuilderImpl) serializeReturnTypeForSignature(signature *Signature)
19511951
return returnTypeNode
19521952
}
19531953

1954+
func (b *nodeBuilderImpl) indexInfoToObjectComputedNamesOrSignatureDeclaration(indexInfo *IndexInfo, typeNode *ast.TypeNode) []*ast.TypeElement {
1955+
if len(indexInfo.components) != 0 {
1956+
// Index info is derived from object or class computed property names (plus explicit named members) - we can clone those instead of writing out the result computed index signature
1957+
allComponentComputedNamesSerializable := b.ctx.enclosingDeclaration != nil && core.Every(indexInfo.components, func(c *ast.Node) bool {
1958+
return c.Name() != nil &&
1959+
ast.IsComputedPropertyName(c.Name()) &&
1960+
ast.IsEntityNameExpression(c.Name().AsComputedPropertyName().Expression) &&
1961+
b.ch.GetEmitResolver().isEntityNameVisible(c.Name().AsComputedPropertyName().Expression, b.ctx.enclosingDeclaration, false).Accessibility == printer.SymbolAccessibilityAccessible
1962+
})
1963+
if allComponentComputedNamesSerializable {
1964+
// Only use computed name serialization form if all components are visible and take the `a.b.c` form
1965+
var newComponents []*ast.Node
1966+
for _, c := range indexInfo.components {
1967+
// skip late bound props that contribute to the index signature - they'll be created by property creation anyway
1968+
if !b.ch.hasLateBindableName(c) {
1969+
newComponents = append(newComponents, c)
1970+
}
1971+
}
1972+
result := make([]*ast.TypeElement, 0, len(newComponents))
1973+
for _, c := range newComponents {
1974+
// Still need to track visibility even if we've already checked it to paint references as used
1975+
b.trackComputedName(c.Name().AsComputedPropertyName().Expression, b.ctx.enclosingDeclaration)
1976+
var modifiers *ast.ModifierList
1977+
if indexInfo.isReadonly {
1978+
modifiers = b.f.NewModifierList([]*ast.Node{b.f.NewModifier(ast.KindReadonlyKeyword)})
1979+
}
1980+
var questionToken *ast.Node
1981+
if c.QuestionToken() != nil {
1982+
questionToken = b.f.NewToken(ast.KindQuestionToken)
1983+
}
1984+
if typeNode == nil {
1985+
typeNode = b.typeToTypeNode(b.ch.getTypeOfSymbol(c.Symbol()))
1986+
}
1987+
propertySignature := b.f.NewPropertySignatureDeclaration(modifiers, c.Name(), questionToken, typeNode, nil)
1988+
result = append(result, propertySignature)
1989+
}
1990+
return result
1991+
}
1992+
}
1993+
return []*ast.TypeElement{b.indexInfoToIndexSignatureDeclarationHelper(indexInfo, typeNode)}
1994+
}
1995+
19541996
func (b *nodeBuilderImpl) indexInfoToIndexSignatureDeclarationHelper(indexInfo *IndexInfo, typeNode *ast.TypeNode) *ast.Node {
19551997
name := getNameFromIndexInfo(indexInfo)
19561998
indexerTypeNode := b.typeToTypeNode(indexInfo.keyType)
@@ -2083,9 +2125,15 @@ func (b *nodeBuilderImpl) shouldUsePlaceholderForProperty(propertySymbol *ast.Sy
20832125
func (b *nodeBuilderImpl) trackComputedName(accessExpression *ast.Node, enclosingDeclaration *ast.Node) {
20842126
// get symbol of the first identifier of the entityName
20852127
firstIdentifier := ast.GetFirstIdentifier(accessExpression)
2086-
name := b.ch.resolveName(firstIdentifier, firstIdentifier.Text(), ast.SymbolFlagsValue|ast.SymbolFlagsExportValue, nil /*nameNotFoundMessage*/, true /*isUse*/, false)
2128+
name := b.ch.resolveName(enclosingDeclaration, firstIdentifier.Text(), ast.SymbolFlagsValue|ast.SymbolFlagsExportValue, nil /*nameNotFoundMessage*/, true /*isUse*/, false)
20872129
if name != nil {
20882130
b.ctx.tracker.TrackSymbol(name, enclosingDeclaration, ast.SymbolFlagsValue)
2131+
} else {
2132+
// Name does not resolve at target location, track symbol at dest location (should be inaccessible)
2133+
fallback := b.ch.resolveName(firstIdentifier, firstIdentifier.Text(), ast.SymbolFlagsValue|ast.SymbolFlagsExportValue, nil /*nameNotFoundMessage*/, true /*isUse*/, false)
2134+
if fallback != nil {
2135+
b.ctx.tracker.TrackSymbol(fallback, enclosingDeclaration, ast.SymbolFlagsValue)
2136+
}
20892137
}
20902138
}
20912139

@@ -2331,7 +2379,7 @@ func (b *nodeBuilderImpl) createTypeNodesFromResolvedType(resolvedType *Structur
23312379
typeElements = append(typeElements, b.signatureToSignatureDeclarationHelper(signature, ast.KindConstructSignature, nil))
23322380
}
23332381
for _, info := range resolvedType.indexInfos {
2334-
typeElements = append(typeElements, b.indexInfoToIndexSignatureDeclarationHelper(info, core.IfElse(resolvedType.objectFlags&ObjectFlagsReverseMapped != 0, b.createElidedInformationPlaceholder(), nil)))
2382+
typeElements = append(typeElements, b.indexInfoToObjectComputedNamesOrSignatureDeclaration(info, core.IfElse(resolvedType.objectFlags&ObjectFlagsReverseMapped != 0, b.createElidedInformationPlaceholder(), nil))...)
23352383
}
23362384

23372385
properties := resolvedType.properties

0 commit comments

Comments
 (0)