@@ -93,6 +93,13 @@ type EnumLiteralKey struct {
93
93
value any
94
94
}
95
95
96
+ // EnumRelationKey
97
+
98
+ type EnumRelationKey struct {
99
+ sourceId ast.SymbolId
100
+ targetId ast.SymbolId
101
+ }
102
+
96
103
// TypeCacheKind
97
104
98
105
type CachedTypeKind int32
@@ -149,11 +156,12 @@ type CachedSignatureKey struct {
149
156
}
150
157
151
158
const (
152
- SignatureKeyErased string = "-"
153
- SignatureKeyCanonical string = "*"
154
- SignatureKeyBase string = "#"
155
- SignatureKeyInner string = "<"
156
- SignatureKeyOuter string = ">"
159
+ SignatureKeyErased string = "-"
160
+ SignatureKeyCanonical string = "*"
161
+ SignatureKeyBase string = "#"
162
+ SignatureKeyInner string = "<"
163
+ SignatureKeyOuter string = ">"
164
+ SignatureKeyImplementation string = "+"
157
165
)
158
166
159
167
// StringMappingKey
@@ -705,7 +713,7 @@ type Checker struct {
705
713
assignableRelation *Relation
706
714
comparableRelation *Relation
707
715
identityRelation *Relation
708
- enumRelation *Relation
716
+ enumRelation map[EnumRelationKey]RelationComparisonResult
709
717
getGlobalNonNullableTypeAliasOrNil func() *ast.Symbol
710
718
getGlobalExtractSymbol func() *ast.Symbol
711
719
getGlobalDisposableType func() *Type
@@ -889,7 +897,7 @@ func NewChecker(program *Program) *Checker {
889
897
c.assignableRelation = &Relation{}
890
898
c.comparableRelation = &Relation{}
891
899
c.identityRelation = &Relation{}
892
- c.enumRelation = &Relation{}
900
+ c.enumRelation = make(map[EnumRelationKey]RelationComparisonResult)
893
901
c.getGlobalNonNullableTypeAliasOrNil = c.getGlobalTypeAliasResolver("NonNullable", 1 /*arity*/, false /*reportErrors*/)
894
902
c.getGlobalExtractSymbol = c.getGlobalTypeAliasResolver("Extract", 2 /*arity*/, true /*reportErrors*/)
895
903
c.getGlobalDisposableType = c.getGlobalTypeResolver("Disposable", 0 /*arity*/, true /*reportErrors*/)
@@ -3213,8 +3221,12 @@ func (c *Checker) checkExpressionCachedEx(node *ast.Node, checkMode CheckMode) *
3213
3221
// and requesting the contextual type might cause a circularity or other bad behaviour.
3214
3222
// It sets the contextual type of the node to any before calling getTypeOfExpression.
3215
3223
func (c *Checker) getContextFreeTypeOfExpression(node *ast.Node) *Type {
3224
+ if cached := c.contextFreeTypes[node]; cached != nil {
3225
+ return cached
3226
+ }
3216
3227
c.pushContextualType(node, c.anyType, false /*isCache*/)
3217
3228
t := c.checkExpressionEx(node, CheckModeSkipContextSensitive)
3229
+ c.contextFreeTypes[node] = t
3218
3230
c.popContextualType()
3219
3231
return t
3220
3232
}
@@ -4609,16 +4621,20 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
4609
4621
var checkCandidate *Signature
4610
4622
var inferenceContext *InferenceContext
4611
4623
if len(candidate.typeParameters) != 0 {
4612
- // !!!
4613
- // // If we are *inside the body of candidate*, we need to create a clone of `candidate` with differing type parameter identities,
4614
- // // so our inference results for this call doesn't pollute expression types referencing the outer type parameter!
4615
- // paramLocation := candidate.typeParameters[0].symbol.Declarations[0]. /* ? */ parent
4616
- // candidateParameterContext := paramLocation || (ifElse(candidate.declaration != nil && isConstructorDeclaration(candidate.declaration), candidate.declaration.Parent, candidate.declaration))
4617
- // if candidateParameterContext != nil && findAncestor(node, func(a *ast.Node) bool {
4618
- // return a == candidateParameterContext
4619
- // }) != nil {
4620
- // candidate = c.getImplementationSignature(candidate)
4621
- // }
4624
+ // If we are *inside the body of candidate*, we need to create a clone of `candidate` with differing type parameter identities,
4625
+ // so our inference results for this call doesn't pollute expression types referencing the outer type parameter!
4626
+ var candidateParameterContext *ast.Node
4627
+ typeParamDeclaration := core.FirstOrNil(candidate.typeParameters[0].symbol.Declarations)
4628
+ if typeParamDeclaration != nil {
4629
+ candidateParameterContext = typeParamDeclaration.Parent
4630
+ } else if candidate.declaration != nil && ast.IsConstructorDeclaration(candidate.declaration) {
4631
+ candidateParameterContext = candidate.declaration.Parent
4632
+ } else {
4633
+ candidateParameterContext = candidate.declaration
4634
+ }
4635
+ if candidateParameterContext != nil && ast.FindAncestor(s.node, func(a *ast.Node) bool { return a == candidateParameterContext }) != nil {
4636
+ candidate = c.getImplementationSignature(candidate)
4637
+ }
4622
4638
var typeArgumentTypes []*Type
4623
4639
if len(s.typeArguments) != 0 {
4624
4640
typeArgumentTypes = c.checkTypeArguments(candidate, s.typeArguments, false /*reportErrors*/, nil)
@@ -4681,6 +4697,16 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
4681
4697
return nil
4682
4698
}
4683
4699
4700
+ func (c *Checker) getImplementationSignature(signature *Signature) *Signature {
4701
+ key := CachedSignatureKey{sig: signature, key: SignatureKeyImplementation}
4702
+ if cached := c.cachedSignatures[key]; cached != nil {
4703
+ return cached
4704
+ }
4705
+ result := c.instantiateSignature(signature, newTypeMapper(nil, nil))
4706
+ c.cachedSignatures[key] = result
4707
+ return result
4708
+ }
4709
+
4684
4710
func (c *Checker) hasCorrectArity(node *ast.Node, args []*ast.Node, signature *Signature, signatureHelpTrailingComma bool) bool {
4685
4711
var argCount int
4686
4712
callIsIncomplete := false
@@ -10599,7 +10625,7 @@ func (c *Checker) getTypeOfVariableOrParameterOrPropertyWorker(symbol *ast.Symbo
10599
10625
case ast.KindExportAssignment:
10600
10626
result = c.widenTypeForVariableLikeDeclaration(c.checkExpressionCached(declaration.AsExportAssignment().Expression), declaration, false /*reportErrors*/)
10601
10627
case ast.KindBinaryExpression:
10602
- result = c.getWidenedTypeForAssignmentDeclaration(symbol, nil )
10628
+ result = c.getWidenedTypeForAssignmentDeclaration(symbol)
10603
10629
case ast.KindJsxAttribute:
10604
10630
result = c.checkJsxAttribute(declaration.AsJsxAttribute(), CheckModeNormal)
10605
10631
case ast.KindEnumMember:
@@ -11842,8 +11868,14 @@ func (c *Checker) getTypeOfPrototypeProperty(prototype *ast.Symbol) *Type {
11842
11868
return c.anyType // !!!
11843
11869
}
11844
11870
11845
- func (c *Checker) getWidenedTypeForAssignmentDeclaration(symbol *ast.Symbol, resolvedSymbol *ast.Symbol) *Type {
11846
- return c.anyType // !!!
11871
+ func (c *Checker) getWidenedTypeForAssignmentDeclaration(symbol *ast.Symbol) *Type {
11872
+ var types []*Type
11873
+ for _, declaration := range symbol.Declarations {
11874
+ if ast.IsBinaryExpression(declaration) {
11875
+ types = core.AppendIfUnique(types, c.getWidenedLiteralType(c.checkExpressionCached(declaration.AsBinaryExpression().Right)))
11876
+ }
11877
+ }
11878
+ return c.getWidenedType(c.getUnionType(types))
11847
11879
}
11848
11880
11849
11881
func (c *Checker) widenTypeForVariableLikeDeclaration(t *Type, declaration *ast.Node, reportErrors bool) *Type {
@@ -13147,7 +13179,7 @@ func (c *Checker) getTypeWithThisArgument(t *Type, thisArgument *Type, needAppar
13147
13179
func (c *Checker) addInheritedMembers(symbols ast.SymbolTable, baseSymbols []*ast.Symbol) ast.SymbolTable {
13148
13180
for _, base := range baseSymbols {
13149
13181
if !isStaticPrivateIdentifierProperty(base) {
13150
- if _ , ok := symbols[base.Name]; !ok {
13182
+ if s , ok := symbols[base.Name]; !ok || s.Flags&ast.SymbolFlagsValue == 0 {
13151
13183
if symbols == nil {
13152
13184
symbols = make(ast.SymbolTable)
13153
13185
}
@@ -13655,18 +13687,13 @@ func (c *Checker) getReturnTypeFromBody(fn *ast.Node, checkMode CheckMode) *Type
13655
13687
} else if len(returnTypes) != 0 {
13656
13688
returnType = c.getUnionTypeEx(returnTypes, UnionReductionSubtype, nil, nil)
13657
13689
}
13658
- // !!!
13659
- // TODO_IDENTIFIER := c.checkAndAggregateYieldOperandTypes(fn, checkMode)
13660
- // if core.Some(yieldTypes) {
13661
- // yieldType = c.getUnionType(yieldTypes, UnionReductionSubtype)
13662
- // } else {
13663
- // yieldType = nil
13664
- // }
13665
- // if core.Some(nextTypes) {
13666
- // nextType = c.getIntersectionType(nextTypes)
13667
- // } else {
13668
- // nextType = nil
13669
- // }
13690
+ yieldTypes, nextTypes := c.checkAndAggregateYieldOperandTypes(fn, checkMode)
13691
+ if len(yieldTypes) != 0 {
13692
+ yieldType = c.getUnionTypeEx(yieldTypes, UnionReductionSubtype, nil, nil)
13693
+ }
13694
+ if len(nextTypes) != 0 {
13695
+ nextType = c.getIntersectionType(nextTypes)
13696
+ }
13670
13697
default:
13671
13698
types, isNeverReturning := c.checkAndAggregateReturnExpressionTypes(fn, checkMode)
13672
13699
if isNeverReturning {
@@ -13821,6 +13848,28 @@ func mayReturnNever(fn *ast.Node) bool {
13821
13848
return false
13822
13849
}
13823
13850
13851
+ func (c *Checker) checkAndAggregateYieldOperandTypes(fn *ast.Node, checkMode CheckMode) (yieldTypes []*Type, nextTypes []*Type) {
13852
+ isAsync := (getFunctionFlags(fn) & FunctionFlagsAsync) != 0
13853
+ forEachYieldExpression(getBodyOfNode(fn), func(yieldExpr *ast.Node) {
13854
+ yieldExprType := c.undefinedWideningType
13855
+ if yieldExpr.Expression() != nil {
13856
+ yieldExprType = c.checkExpressionEx(yieldExpr.Expression(), checkMode)
13857
+ }
13858
+ yieldTypes = core.AppendIfUnique(yieldTypes, c.getYieldedTypeOfYieldExpression(yieldExpr, yieldExprType, c.anyType, isAsync))
13859
+ var nextType *Type
13860
+ if yieldExpr.AsYieldExpression().AsteriskToken != nil {
13861
+ iterationTypes := c.getIterationTypesOfIterable(yieldExprType, core.IfElse(isAsync, IterationUseAsyncYieldStar, IterationUseYieldStar), yieldExpr.Expression())
13862
+ nextType = iterationTypes.nextType
13863
+ } else {
13864
+ nextType = c.getContextualType(yieldExpr, ContextFlagsNone)
13865
+ }
13866
+ if nextType != nil {
13867
+ nextTypes = core.AppendIfUnique(nextTypes, nextType)
13868
+ }
13869
+ })
13870
+ return
13871
+ }
13872
+
13824
13873
func (c *Checker) createPromiseType(promisedType *Type) *Type {
13825
13874
// creates a `Promise<T>` type where `T` is the promisedType argument
13826
13875
globalPromiseType := c.getGlobalPromiseTypeChecked()
@@ -22011,7 +22060,8 @@ func (c *Checker) getContextualTypeForBinaryOperand(node *ast.Node, contextFlags
22011
22060
switch binary.OperatorToken.Kind {
22012
22061
case ast.KindEqualsToken, ast.KindAmpersandAmpersandEqualsToken, ast.KindBarBarEqualsToken, ast.KindQuestionQuestionEqualsToken:
22013
22062
// In an assignment expression, the right operand is contextually typed by the type of the left operand.
22014
- if node == binary.Right {
22063
+ // If the binary operator has a symbol, this is an assignment declaration and there is no contextual type.
22064
+ if node == binary.Right && binary.Symbol == nil {
22015
22065
return c.getTypeOfExpression(binary.Left)
22016
22066
}
22017
22067
case ast.KindBarBarToken, ast.KindQuestionQuestionToken:
@@ -22021,10 +22071,8 @@ func (c *Checker) getContextualTypeForBinaryOperand(node *ast.Node, contextFlags
22021
22071
// by the type of the left operand, except for the special case of Javascript declarations of the form
22022
22072
// `namespace.prop = namespace.prop || {}`.
22023
22073
t := c.getContextualType(binary.AsNode(), contextFlags)
22024
- if t != nil && node == binary.Right {
22025
- if pattern := c.patternForType[t]; pattern != nil {
22026
- return c.getTypeOfExpression(binary.Left)
22027
- }
22074
+ if node == binary.Right && (t == nil || c.patternForType[t] != nil) {
22075
+ return c.getTypeOfExpression(binary.Left)
22028
22076
}
22029
22077
return t
22030
22078
case ast.KindAmpersandAmpersandToken, ast.KindCommaToken:
0 commit comments