Skip to content

Commit 5569d5d

Browse files
Refactor PropertyAssignment node reuse logic based on code review
Co-authored-by: RyanCavanaugh <[email protected]>
1 parent 511d3a9 commit 5569d5d

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

internal/checker/nodebuilderimpl.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,21 @@ func getJSDocTypeAssertionType(node *ast.Node) *ast.TypeNode {
16441644
return nil
16451645
}
16461646

1647+
// getTypeForDeclaration resolves the type for a declaration, considering various special cases.
1648+
func (b *nodeBuilderImpl) getTypeForDeclaration(symbol *ast.Symbol, declaration *ast.Declaration) *Type {
1649+
t := b.ctx.enclosingSymbolTypes[ast.GetSymbolId(symbol)]
1650+
if t == nil {
1651+
if symbol.Flags&ast.SymbolFlagsAccessor != 0 && declaration != nil && declaration.Kind == ast.KindSetAccessor {
1652+
t = b.ch.instantiateType(b.ch.getWriteTypeOfSymbol(symbol), b.ctx.mapper)
1653+
} else if symbol != nil && (symbol.Flags&(ast.SymbolFlagsTypeLiteral|ast.SymbolFlagsSignature) == 0) {
1654+
t = b.ch.instantiateType(b.ch.getWidenedLiteralType(b.ch.getTypeOfSymbol(symbol)), b.ctx.mapper)
1655+
} else {
1656+
t = b.ch.errorType
1657+
}
1658+
}
1659+
return t
1660+
}
1661+
16471662
func (b *nodeBuilderImpl) serializeTypeForExpression(expr *ast.Node) *ast.Node {
16481663
// !!! TODO: shim, add node reuse
16491664
t := b.ch.instantiateType(b.ch.getWidenedType(b.ch.getRegularTypeOfExpression(expr)), b.ctx.mapper)
@@ -1998,28 +2013,22 @@ func (b *nodeBuilderImpl) serializeTypeForDeclaration(declaration *ast.Declarati
19982013
// Check for PropertyAssignment with type assertion that can be reused (PR #60576)
19992014
if declaration != nil && declaration.Kind == ast.KindPropertyAssignment {
20002015
initializer := declaration.AsPropertyAssignment().Initializer
2016+
initializerKind := initializer.Kind
20012017
var assertionNode *ast.TypeNode
20022018

20032019
// Check if initializer is a JSDoc type assertion
20042020
if ast.IsJSDocTypeAssertion(initializer) {
20052021
assertionNode = getJSDocTypeAssertionType(initializer)
2006-
} else if initializer.Kind == ast.KindAsExpression || initializer.Kind == ast.KindTypeAssertionExpression {
2022+
} else if initializerKind == ast.KindAsExpression || initializerKind == ast.KindTypeAssertionExpression {
20072023
// Extract the type node from AsExpression or TypeAssertion
2008-
if initializer.Kind == ast.KindAsExpression {
2009-
assertionNode = initializer.AsAsExpression().Type
2010-
} else {
2011-
assertionNode = initializer.AsTypeAssertion().Type
2012-
}
2024+
assertionNode = getAssertedTypeNode(initializer)
20132025
}
20142026

20152027
// If we have an assertion node, check if we can reuse it
20162028
if assertionNode != nil && !isConstTypeReference(assertionNode) {
20172029
// Try to reuse the existing type node if it's equivalent
20182030
if t == nil {
2019-
t = b.ctx.enclosingSymbolTypes[ast.GetSymbolId(symbol)]
2020-
if t == nil && symbol != nil && (symbol.Flags&(ast.SymbolFlagsTypeLiteral|ast.SymbolFlagsSignature) == 0) {
2021-
t = b.ch.instantiateType(b.ch.getWidenedLiteralType(b.ch.getTypeOfSymbol(symbol)), b.ctx.mapper)
2022-
}
2031+
t = b.getTypeForDeclaration(symbol, declaration)
20232032
}
20242033

20252034
if t != nil {
@@ -2032,16 +2041,7 @@ func (b *nodeBuilderImpl) serializeTypeForDeclaration(declaration *ast.Declarati
20322041
}
20332042

20342043
if t == nil {
2035-
t = b.ctx.enclosingSymbolTypes[ast.GetSymbolId(symbol)]
2036-
if t == nil {
2037-
if symbol.Flags&ast.SymbolFlagsAccessor != 0 && declaration.Kind == ast.KindSetAccessor {
2038-
t = b.ch.instantiateType(b.ch.getWriteTypeOfSymbol(symbol), b.ctx.mapper)
2039-
} else if symbol != nil && (symbol.Flags&(ast.SymbolFlagsTypeLiteral|ast.SymbolFlagsSignature) == 0) {
2040-
t = b.ch.instantiateType(b.ch.getWidenedLiteralType(b.ch.getTypeOfSymbol(symbol)), b.ctx.mapper)
2041-
} else {
2042-
t = b.ch.errorType
2043-
}
2044-
}
2044+
t = b.getTypeForDeclaration(symbol, declaration)
20452045
}
20462046

20472047
// !!! TODO: JSDoc, getEmitResolver call is unfortunate layering for the helper - hoist it into checker

0 commit comments

Comments
 (0)