Skip to content

Commit 42b6ef0

Browse files
committed
Rename contextualMapper to inferenceContext (which it always is)
1 parent 62b0d98 commit 42b6ef0

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

src/compiler/checker.ts

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10778,14 +10778,8 @@ namespace ts {
1077810778
return t => typeParameters.indexOf(t) >= index ? emptyObjectType : t;
1077910779
}
1078010780

10781-
function isInferenceContext(mapper: TypeMapper): mapper is InferenceContext {
10782-
return !!(<InferenceContext>mapper).typeParameters;
10783-
}
10784-
10785-
function cloneTypeMapper(mapper: TypeMapper, extraFlags: InferenceFlags = 0): TypeMapper {
10786-
return mapper && isInferenceContext(mapper) ?
10787-
createInferenceContext(mapper.typeParameters, mapper.signature, mapper.flags | extraFlags, mapper.compareTypes, mapper.inferences) :
10788-
mapper;
10781+
function cloneInferenceContext(context: InferenceContext, extraFlags: InferenceFlags = 0): InferenceContext {
10782+
return createInferenceContext(context.typeParameters, context.signature, context.flags | extraFlags, context.compareTypes, context.inferences);
1078910783
}
1079010784

1079110785
function cloneInferredPartOfContext(context: InferenceContext): InferenceContext | undefined {
@@ -11755,7 +11749,7 @@ namespace ts {
1175511749

1175611750
if (source.typeParameters && source.typeParameters !== target.typeParameters) {
1175711751
target = getCanonicalSignature(target);
11758-
source = instantiateSignatureInContextOf(source, target, /*contextualMapper*/ undefined, compareTypes);
11752+
source = instantiateSignatureInContextOf(source, target, /*inferenceContext*/ undefined, compareTypes);
1175911753
}
1176011754

1176111755
const sourceCount = getParameterCount(source);
@@ -17531,7 +17525,7 @@ namespace ts {
1753117525
while (type) {
1753217526
const thisType = getThisTypeFromContextualType(type);
1753317527
if (thisType) {
17534-
return instantiateType(thisType, getContextualMapper(containingLiteral));
17528+
return instantiateType(thisType, getInferenceContext(containingLiteral));
1753517529
}
1753617530
if (literal.parent.kind !== SyntaxKind.PropertyAssignment) {
1753717531
break;
@@ -18030,9 +18024,9 @@ namespace ts {
1803018024
// return type inferences is available, instantiate those types using that mapper.
1803118025
function instantiateContextualType(contextualType: Type | undefined, node: Expression): Type | undefined {
1803218026
if (contextualType && maybeTypeOfKind(contextualType, TypeFlags.Instantiable)) {
18033-
const returnMapper = (<InferenceContext>getContextualMapper(node)).returnMapper;
18034-
if (returnMapper) {
18035-
return instantiateInstantiableTypes(contextualType, returnMapper);
18027+
const inferenceContext = getInferenceContext(node);
18028+
if (inferenceContext && inferenceContext.returnMapper) {
18029+
return instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
1803618030
}
1803718031
}
1803818032
return contextualType;
@@ -18134,9 +18128,9 @@ namespace ts {
1813418128
return undefined;
1813518129
}
1813618130

18137-
function getContextualMapper(node: Node) {
18138-
const ancestor = findAncestor(node, n => !!n.contextualMapper);
18139-
return ancestor ? ancestor.contextualMapper! : identityMapper;
18131+
function getInferenceContext(node: Node) {
18132+
const ancestor = findAncestor(node, n => !!n.inferenceContext);
18133+
return ancestor && ancestor.inferenceContext!;
1814018134
}
1814118135

1814218136
function getContextualJsxElementAttributesType(node: JsxOpeningLikeElement) {
@@ -20135,19 +20129,19 @@ namespace ts {
2013520129
}
2013620130

2013720131
// Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec)
20138-
function instantiateSignatureInContextOf(signature: Signature, contextualSignature: Signature, contextualMapper?: TypeMapper, compareTypes?: TypeComparer): Signature {
20132+
function instantiateSignatureInContextOf(signature: Signature, contextualSignature: Signature, inferenceContext?: InferenceContext, compareTypes?: TypeComparer): Signature {
2013920133
const context = createInferenceContext(signature.typeParameters!, signature, InferenceFlags.None, compareTypes);
20140-
// We clone the contextualMapper to avoid fixing. For example, when the source signature is <T>(x: T) => T[] and
20134+
// We clone the inferenceContext to avoid fixing. For example, when the source signature is <T>(x: T) => T[] and
2014120135
// the contextual signature is (...args: A) => B, we want to infer the element type of A's constraint (say 'any')
2014220136
// for T but leave it possible to later infer '[any]' back to A.
2014320137
const restType = getEffectiveRestType(contextualSignature);
20144-
const mapper = contextualMapper && restType && restType.flags & TypeFlags.TypeParameter ? cloneTypeMapper(contextualMapper) : contextualMapper;
20138+
const mapper = inferenceContext && restType && restType.flags & TypeFlags.TypeParameter ? cloneInferenceContext(inferenceContext) : inferenceContext;
2014520139
const sourceSignature = mapper ? instantiateSignature(contextualSignature, mapper) : contextualSignature;
2014620140
forEachMatchingParameterType(sourceSignature, signature, (source, target) => {
2014720141
// Type parameters from outer context referenced by source type are fixed by instantiation of the source type
2014820142
inferTypes(context.inferences, source, target);
2014920143
});
20150-
if (!contextualMapper) {
20144+
if (!inferenceContext) {
2015120145
inferTypes(context.inferences, getReturnTypeOfSignature(contextualSignature), getReturnTypeOfSignature(signature), InferencePriority.ReturnType);
2015220146
const signaturePredicate = getTypePredicateOfSignature(signature);
2015320147
const contextualPredicate = getTypePredicateOfSignature(sourceSignature);
@@ -20192,7 +20186,8 @@ namespace ts {
2019220186
// We clone the contextual mapper to avoid disturbing a resolution in progress for an
2019320187
// outer call expression. Effectively we just want a snapshot of whatever has been
2019420188
// inferred for any outer call expression so far.
20195-
const instantiatedType = instantiateType(contextualType, cloneTypeMapper(getContextualMapper(node), InferenceFlags.NoDefault));
20189+
const outerContext = getInferenceContext(node);
20190+
const instantiatedType = instantiateType(contextualType, outerContext && cloneInferenceContext(outerContext, InferenceFlags.NoDefault));
2019620191
// If the contextual type is a generic function type with a single call signature, we
2019720192
// instantiate the type with its own type parameters and type arguments. This ensures that
2019820193
// the type parameters are not erased to type any during type inference such that they can
@@ -20326,7 +20321,7 @@ namespace ts {
2032620321
// However "context" and "updater" are implicit and can't be specify by users. Only the first parameter, props,
2032720322
// can be specified by users through attributes property.
2032820323
const paramType = getEffectiveFirstArgumentForJsxSignature(signature, node);
20329-
const attributesType = checkExpressionWithContextualType(node.attributes, paramType, /*contextualMapper*/ undefined, checkMode);
20324+
const attributesType = checkExpressionWithContextualType(node.attributes, paramType, /*inferenceContext*/ undefined, checkMode);
2033020325
return checkTypeRelatedToAndOptionallyElaborate(attributesType, paramType, relation, reportErrors ? node.tagName : undefined, node.attributes);
2033120326
}
2033220327

@@ -20360,7 +20355,7 @@ namespace ts {
2036020355
const arg = args[i];
2036120356
if (arg.kind !== SyntaxKind.OmittedExpression) {
2036220357
const paramType = getTypeAtPosition(signature, i);
20363-
const argType = checkExpressionWithContextualType(arg, paramType, /*contextualMapper*/ undefined, checkMode);
20358+
const argType = checkExpressionWithContextualType(arg, paramType, /*inferenceContext*/ undefined, checkMode);
2036420359
// If one or more arguments are still excluded (as indicated by CheckMode.SkipContextSensitive),
2036520360
// we obtain the regular type of any object literal arguments because we may not have inferred complete
2036620361
// parameter types yet and therefore excess property checks may yield false positives (see #17041).
@@ -21855,27 +21850,27 @@ namespace ts {
2185521850
return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : fallbackType;
2185621851
}
2185721852

21858-
function inferFromAnnotatedParameters(signature: Signature, context: Signature, mapper: TypeMapper) {
21853+
function inferFromAnnotatedParameters(signature: Signature, context: Signature, inferenceContext: InferenceContext) {
2185921854
const len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0);
2186021855
for (let i = 0; i < len; i++) {
2186121856
const declaration = <ParameterDeclaration>signature.parameters[i].valueDeclaration;
2186221857
if (declaration.type) {
2186321858
const typeNode = getEffectiveTypeAnnotationNode(declaration);
2186421859
if (typeNode) {
21865-
inferTypes((<InferenceContext>mapper).inferences, getTypeFromTypeNode(typeNode), getTypeAtPosition(context, i));
21860+
inferTypes(inferenceContext.inferences, getTypeFromTypeNode(typeNode), getTypeAtPosition(context, i));
2186621861
}
2186721862
}
2186821863
}
2186921864
const restType = getEffectiveRestType(context);
2187021865
if (restType && restType.flags & TypeFlags.TypeParameter) {
2187121866
// The contextual signature has a generic rest parameter. We first instantiate the contextual
2187221867
// signature (without fixing type parameters) and assign types to contextually typed parameters.
21873-
const instantiatedContext = instantiateSignature(context, cloneTypeMapper(mapper));
21868+
const instantiatedContext = instantiateSignature(context, cloneInferenceContext(inferenceContext));
2187421869
assignContextualParameterTypes(signature, instantiatedContext);
2187521870
// We then infer from a tuple type representing the parameters that correspond to the contextual
2187621871
// rest parameter.
2187721872
const restPos = getParameterCount(context) - 1;
21878-
inferTypes((<InferenceContext>mapper).inferences, getRestTypeAtPosition(signature, restPos), restType);
21873+
inferTypes(inferenceContext.inferences, getRestTypeAtPosition(signature, restPos), restType);
2187921874
}
2188021875
}
2188121876

@@ -22316,12 +22311,12 @@ namespace ts {
2231622311
if (contextualSignature) {
2231722312
const signature = getSignaturesOfType(type, SignatureKind.Call)[0];
2231822313
if (isContextSensitive(node)) {
22319-
const contextualMapper = getContextualMapper(node);
22314+
const inferenceContext = getInferenceContext(node);
2232022315
if (checkMode && checkMode & CheckMode.Inferential) {
22321-
inferFromAnnotatedParameters(signature, contextualSignature, contextualMapper);
22316+
inferFromAnnotatedParameters(signature, contextualSignature, inferenceContext!);
2232222317
}
22323-
const instantiatedContextualSignature = contextualMapper === identityMapper ?
22324-
contextualSignature : instantiateSignature(contextualSignature, contextualMapper);
22318+
const instantiatedContextualSignature = inferenceContext ?
22319+
instantiateSignature(contextualSignature, inferenceContext) : contextualSignature;
2232522320
assignContextualParameterTypes(signature, instantiatedContextualSignature);
2232622321
}
2232722322
if (!getReturnTypeFromAnnotation(node) && !signature.resolvedReturnType) {
@@ -23328,20 +23323,20 @@ namespace ts {
2332823323
return node;
2332923324
}
2333023325

23331-
function checkExpressionWithContextualType(node: Expression, contextualType: Type, contextualMapper: TypeMapper | undefined, checkMode: CheckMode): Type {
23326+
function checkExpressionWithContextualType(node: Expression, contextualType: Type, inferenceContext: InferenceContext | undefined, checkMode: CheckMode): Type {
2333223327
const context = getContextNode(node);
2333323328
const saveContextualType = context.contextualType;
23334-
const saveContextualMapper = context.contextualMapper;
23329+
const saveInferenceContext = context.inferenceContext;
2333523330
context.contextualType = contextualType;
23336-
context.contextualMapper = contextualMapper;
23337-
const type = checkExpression(node, checkMode | CheckMode.Contextual | (contextualMapper ? CheckMode.Inferential : 0));
23331+
context.inferenceContext = inferenceContext;
23332+
const type = checkExpression(node, checkMode | CheckMode.Contextual | (inferenceContext ? CheckMode.Inferential : 0));
2333823333
// We strip literal freshness when an appropriate contextual type is present such that contextually typed
2333923334
// literals always preserve their literal types (otherwise they might widen during type inference). An alternative
2334023335
// here would be to not mark contextually typed literals as fresh in the first place.
2334123336
const result = maybeTypeOfKind(type, TypeFlags.Literal) && isLiteralOfContextualType(type, instantiateContextualType(contextualType, node)) ?
2334223337
getRegularTypeOfLiteralType(type) : type;
2334323338
context.contextualType = saveContextualType;
23344-
context.contextualMapper = saveContextualMapper;
23339+
context.inferenceContext = saveInferenceContext;
2334523340
return result;
2334623341
}
2334723342

@@ -23467,7 +23462,7 @@ namespace ts {
2346723462
if (contextualType) {
2346823463
const contextualSignature = getSingleCallSignature(getNonNullableType(contextualType));
2346923464
if (contextualSignature && !contextualSignature.typeParameters) {
23470-
const context = <InferenceContext>getContextualMapper(node);
23465+
const context = <InferenceContext>getInferenceContext(node);
2347123466
// We have an expression that is an argument of a generic function for which we are performing
2347223467
// type argument inference. The expression is of a function type with a single generic call
2347323468
// signature and a contextual function type with a single non-generic call signature. Now check
@@ -23507,7 +23502,7 @@ namespace ts {
2350723502
if (checkMode & CheckMode.Inferential) {
2350823503
// We have skipped a generic function during inferential typing. Obtain the inference context and
2350923504
// indicate this has occurred such that we know a second pass of inference is be needed.
23510-
const context = <InferenceContext>getContextualMapper(node);
23505+
const context = <InferenceContext>getInferenceContext(node);
2351123506
context.flags |= InferenceFlags.SkippedGenericFunction;
2351223507
}
2351323508
}

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ namespace ts {
630630
/* @internal */ flowNode?: FlowNode; // Associated FlowNode (initialized by binding)
631631
/* @internal */ emitNode?: EmitNode; // Associated EmitNode (initialized by transforms)
632632
/* @internal */ contextualType?: Type; // Used to temporarily assign a contextual type during overload resolution
633-
/* @internal */ contextualMapper?: TypeMapper; // Mapper for contextual type
633+
/* @internal */ inferenceContext?: InferenceContext; // Inference context for contextual type
634634
}
635635

636636
export interface JSDocContainer {
@@ -4454,7 +4454,7 @@ namespace ts {
44544454
flags: InferenceFlags; // Inference flags
44554455
compareTypes: TypeComparer; // Type comparer function
44564456
returnMapper?: TypeMapper; // Type mapper for inferences from return types (if any)
4457-
inferredTypeParameters?: ReadonlyArray<TypeParameter>;
4457+
inferredTypeParameters?: ReadonlyArray<TypeParameter>; // Inferred type parameters for function result
44584458
}
44594459

44604460
/* @internal */

0 commit comments

Comments
 (0)