@@ -2443,14 +2443,15 @@ export function convertParamSpecValueToType(paramSpecEntry: ParamSpecValue, omit
2443
2443
// it to be replaced with something else.
2444
2444
class TypeVarTransformer {
2445
2445
private _isTransformingTypeArg = false ;
2446
+ private _pendingTypeVarTransformations = new Set < string > ( ) ;
2446
2447
2447
- apply ( type : Type , recursionSet = new Set < string > ( ) , recursionCount = 0 ) : Type {
2448
+ apply ( type : Type , recursionCount = 0 ) : Type {
2448
2449
if ( recursionCount > maxTypeRecursionCount ) {
2449
2450
return type ;
2450
2451
}
2451
2452
recursionCount ++ ;
2452
2453
2453
- type = this . _transformGenericTypeAlias ( type , recursionSet , recursionCount ) ;
2454
+ type = this . _transformGenericTypeAlias ( type , recursionCount ) ;
2454
2455
2455
2456
// Shortcut the operation if possible.
2456
2457
if ( ! requiresSpecialization ( type ) ) {
@@ -2476,7 +2477,7 @@ class TypeVarTransformer {
2476
2477
2477
2478
let requiresUpdate = false ;
2478
2479
const typeArgs = type . typeAliasInfo . typeArguments . map ( ( typeArg ) => {
2479
- const replacementType = this . apply ( typeArg , recursionSet , recursionCount ) ;
2480
+ const replacementType = this . apply ( typeArg , recursionCount ) ;
2480
2481
if ( replacementType !== typeArg ) {
2481
2482
requiresUpdate = true ;
2482
2483
}
@@ -2500,15 +2501,16 @@ class TypeVarTransformer {
2500
2501
let replacementType : Type = type ;
2501
2502
2502
2503
// Recursively transform the results, but ensure that we don't replace the
2503
- // same type variable recursively by setting it in the recursionSet.
2504
+ // same type variable recursively by setting it in the
2505
+ // _pendingTypeVarTransformations set.
2504
2506
const typeVarName = TypeVarType . getNameWithScope ( type ) ;
2505
- if ( ! recursionSet . has ( typeVarName ) ) {
2507
+ if ( ! this . _pendingTypeVarTransformations . has ( typeVarName ) ) {
2506
2508
replacementType = this . transformTypeVar ( type ) ;
2507
2509
2508
2510
if ( ! this . _isTransformingTypeArg ) {
2509
- recursionSet . add ( typeVarName ) ;
2510
- replacementType = this . apply ( replacementType , recursionSet , recursionCount ) ;
2511
- recursionSet . delete ( typeVarName ) ;
2511
+ this . _pendingTypeVarTransformations . add ( typeVarName ) ;
2512
+ replacementType = this . apply ( replacementType , recursionCount ) ;
2513
+ this . _pendingTypeVarTransformations . delete ( typeVarName ) ;
2512
2514
}
2513
2515
2514
2516
// If we're transforming a variadic type variable that was in a union,
@@ -2523,7 +2525,7 @@ class TypeVarTransformer {
2523
2525
2524
2526
if ( isUnion ( type ) ) {
2525
2527
const newUnionType = mapSubtypes ( type , ( subtype ) => {
2526
- let transformedType = this . apply ( subtype , recursionSet , recursionCount ) ;
2528
+ let transformedType = this . apply ( subtype , recursionCount ) ;
2527
2529
2528
2530
// If we're transforming a variadic type variable within a union,
2529
2531
// combine the individual types within the variadic type variable.
@@ -2547,11 +2549,11 @@ class TypeVarTransformer {
2547
2549
}
2548
2550
2549
2551
if ( isClass ( type ) ) {
2550
- return this . _transformTypeVarsInClassType ( type , recursionSet , recursionCount ) ;
2552
+ return this . _transformTypeVarsInClassType ( type , recursionCount ) ;
2551
2553
}
2552
2554
2553
2555
if ( isFunction ( type ) ) {
2554
- return this . _transformTypeVarsInFunctionType ( type , recursionSet , recursionCount ) ;
2556
+ return this . _transformTypeVarsInFunctionType ( type , recursionCount ) ;
2555
2557
}
2556
2558
2557
2559
if ( isOverloadedFunction ( type ) ) {
@@ -2560,7 +2562,7 @@ class TypeVarTransformer {
2560
2562
// Specialize each of the functions in the overload.
2561
2563
const newOverloads : FunctionType [ ] = [ ] ;
2562
2564
type . overloads . forEach ( ( entry ) => {
2563
- const replacementType = this . _transformTypeVarsInFunctionType ( entry , recursionSet , recursionCount ) ;
2565
+ const replacementType = this . _transformTypeVarsInFunctionType ( entry , recursionCount ) ;
2564
2566
newOverloads . push ( replacementType ) ;
2565
2567
if ( replacementType !== entry ) {
2566
2568
requiresUpdate = true ;
@@ -2590,14 +2592,14 @@ class TypeVarTransformer {
2590
2592
return type ;
2591
2593
}
2592
2594
2593
- private _transformGenericTypeAlias ( type : Type , recursionSet : Set < string > , recursionCount : number ) {
2595
+ private _transformGenericTypeAlias ( type : Type , recursionCount : number ) {
2594
2596
if ( ! type . typeAliasInfo || ! type . typeAliasInfo . typeParameters || ! type . typeAliasInfo . typeArguments ) {
2595
2597
return type ;
2596
2598
}
2597
2599
2598
2600
let requiresUpdate = false ;
2599
2601
const newTypeArgs = type . typeAliasInfo . typeArguments . map ( ( typeArg ) => {
2600
- const updatedType = this . apply ( typeArg , recursionSet , recursionCount ) ;
2602
+ const updatedType = this . apply ( typeArg , recursionCount ) ;
2601
2603
if ( type !== updatedType ) {
2602
2604
requiresUpdate = true ;
2603
2605
}
@@ -2616,11 +2618,7 @@ class TypeVarTransformer {
2616
2618
: type ;
2617
2619
}
2618
2620
2619
- private _transformTypeVarsInClassType (
2620
- classType : ClassType ,
2621
- recursionSet : Set < string > ,
2622
- recursionCount : number
2623
- ) : ClassType {
2621
+ private _transformTypeVarsInClassType ( classType : ClassType , recursionCount : number ) : ClassType {
2624
2622
// Handle the common case where the class has no type parameters.
2625
2623
if ( ClassType . getTypeParameters ( classType ) . length === 0 && ! ClassType . isSpecialBuiltIn ( classType ) ) {
2626
2624
return classType ;
@@ -2651,7 +2649,7 @@ class TypeVarTransformer {
2651
2649
return transformParamSpec ( oldTypeArgType ) ;
2652
2650
}
2653
2651
2654
- let newTypeArgType = this . apply ( oldTypeArgType , recursionSet , recursionCount ) ;
2652
+ let newTypeArgType = this . apply ( oldTypeArgType , recursionCount ) ;
2655
2653
if ( newTypeArgType !== oldTypeArgType ) {
2656
2654
specializationNeeded = true ;
2657
2655
@@ -2678,14 +2676,14 @@ class TypeVarTransformer {
2678
2676
}
2679
2677
} else {
2680
2678
const typeParamName = TypeVarType . getNameWithScope ( typeParam ) ;
2681
- if ( ! recursionSet . has ( typeParamName ) ) {
2679
+ if ( ! this . _pendingTypeVarTransformations . has ( typeParamName ) ) {
2682
2680
replacementType = this . transformTypeVar ( typeParam ) ;
2683
2681
2684
2682
if ( replacementType !== typeParam ) {
2685
2683
if ( ! this . _isTransformingTypeArg ) {
2686
- recursionSet . add ( typeParamName ) ;
2687
- replacementType = this . apply ( replacementType , recursionSet , recursionCount ) ;
2688
- recursionSet . delete ( typeParamName ) ;
2684
+ this . _pendingTypeVarTransformations . add ( typeParamName ) ;
2685
+ replacementType = this . apply ( replacementType , recursionCount ) ;
2686
+ this . _pendingTypeVarTransformations . delete ( typeParamName ) ;
2689
2687
}
2690
2688
2691
2689
specializationNeeded = true ;
@@ -2701,7 +2699,7 @@ class TypeVarTransformer {
2701
2699
if ( classType . tupleTypeArguments ) {
2702
2700
newVariadicTypeArgs = [ ] ;
2703
2701
classType . tupleTypeArguments . forEach ( ( oldTypeArgType ) => {
2704
- const newTypeArgType = this . apply ( oldTypeArgType . type , recursionSet , recursionCount ) ;
2702
+ const newTypeArgType = this . apply ( oldTypeArgType . type , recursionCount ) ;
2705
2703
2706
2704
if ( newTypeArgType !== oldTypeArgType . type ) {
2707
2705
specializationNeeded = true ;
@@ -2742,11 +2740,7 @@ class TypeVarTransformer {
2742
2740
) ;
2743
2741
}
2744
2742
2745
- private _transformTypeVarsInFunctionType (
2746
- sourceType : FunctionType ,
2747
- recursionSet : Set < string > ,
2748
- recursionCount : number
2749
- ) : FunctionType {
2743
+ private _transformTypeVarsInFunctionType ( sourceType : FunctionType , recursionCount : number ) : FunctionType {
2750
2744
let functionType = sourceType ;
2751
2745
2752
2746
// Handle functions with a parameter specification in a special manner.
@@ -2758,9 +2752,7 @@ class TypeVarTransformer {
2758
2752
}
2759
2753
2760
2754
const declaredReturnType = FunctionType . getSpecializedReturnType ( functionType ) ;
2761
- const specializedReturnType = declaredReturnType
2762
- ? this . apply ( declaredReturnType , recursionSet , recursionCount )
2763
- : undefined ;
2755
+ const specializedReturnType = declaredReturnType ? this . apply ( declaredReturnType , recursionCount ) : undefined ;
2764
2756
let typesRequiredSpecialization = declaredReturnType !== specializedReturnType ;
2765
2757
2766
2758
const specializedParameters : SpecializedFunctionTypes = {
@@ -2808,7 +2800,7 @@ class TypeVarTransformer {
2808
2800
2809
2801
for ( let i = 0 ; i < functionType . details . parameters . length ; i ++ ) {
2810
2802
const paramType = FunctionType . getEffectiveParameterType ( functionType , i ) ;
2811
- const specializedType = this . apply ( paramType , recursionSet , recursionCount ) ;
2803
+ const specializedType = this . apply ( paramType , recursionCount ) ;
2812
2804
specializedParameters . parameterTypes . push ( specializedType ) ;
2813
2805
if (
2814
2806
variadicParamIndex === undefined &&
@@ -2833,7 +2825,7 @@ class TypeVarTransformer {
2833
2825
2834
2826
let specializedInferredReturnType : Type | undefined ;
2835
2827
if ( functionType . inferredReturnType ) {
2836
- specializedInferredReturnType = this . apply ( functionType . inferredReturnType , recursionSet , recursionCount ) ;
2828
+ specializedInferredReturnType = this . apply ( functionType . inferredReturnType , recursionCount ) ;
2837
2829
}
2838
2830
2839
2831
// If there was no unpacked variadic type variable, we're done.
0 commit comments