MCPcopy Index your code
hub / github.com/nodejs/node / getContextualThisParameterType

Function getContextualThisParameterType

test/fixtures/snapshot/typescript.js:72339–72397  ·  view source on GitHub ↗
(func)

Source from the content-addressed store, hash-verified

72337 });
72338 }
72339 function getContextualThisParameterType(func) {
72340 if (func.kind === 214 /* SyntaxKind.ArrowFunction */) {
72341 return undefined;
72342 }
72343 if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
72344 var contextualSignature = getContextualSignature(func);
72345 if (contextualSignature) {
72346 var thisParameter = contextualSignature.thisParameter;
72347 if (thisParameter) {
72348 return getTypeOfSymbol(thisParameter);
72349 }
72350 }
72351 }
72352 var inJs = ts.isInJSFile(func);
72353 if (noImplicitThis || inJs) {
72354 var containingLiteral = getContainingObjectLiteral(func);
72355 if (containingLiteral) {
72356 // We have an object literal method. Check if the containing object literal has a contextual type
72357 // that includes a ThisType<T>. If so, T is the contextual type for 'this'. We continue looking in
72358 // any directly enclosing object literals.
72359 var contextualType = getApparentTypeOfContextualType(containingLiteral);
72360 var literal = containingLiteral;
72361 var type = contextualType;
72362 while (type) {
72363 var thisType = getThisTypeFromContextualType(type);
72364 if (thisType) {
72365 return instantiateType(thisType, getMapperFromContext(getInferenceContext(containingLiteral)));
72366 }
72367 if (literal.parent.kind !== 296 /* SyntaxKind.PropertyAssignment */) {
72368 break;
72369 }
72370 literal = literal.parent.parent;
72371 type = getApparentTypeOfContextualType(literal);
72372 }
72373 // There was no contextual ThisType<T> for the containing object literal, so the contextual type
72374 // for 'this' is the non-null form of the contextual type for the containing object literal or
72375 // the type of the object literal itself.
72376 return getWidenedType(contextualType ? getNonNullableType(contextualType) : checkExpressionCached(containingLiteral));
72377 }
72378 // In an assignment of the form 'obj.xxx = function(...)' or 'obj[xxx] = function(...)', the
72379 // contextual type for 'this' is 'obj'.
72380 var parent = ts.walkUpParenthesizedExpressions(func.parent);
72381 if (parent.kind === 221 /* SyntaxKind.BinaryExpression */ && parent.operatorToken.kind === 63 /* SyntaxKind.EqualsToken */) {
72382 var target = parent.left;
72383 if (ts.isAccessExpression(target)) {
72384 var expression = target.expression;
72385 // Don't contextually type `this` as `exports` in `exports.Point = function(x, y) { this.x = x; this.y = y; }`
72386 if (inJs && ts.isIdentifier(expression)) {
72387 var sourceFile = ts.getSourceFileOfNode(parent);
72388 if (sourceFile.commonJsModuleIndicator && getResolvedSymbol(expression) === sourceFile.symbol) {
72389 return undefined;
72390 }
72391 }
72392 return getWidenedType(checkExpressionCached(expression));
72393 }
72394 }
72395 }
72396 return undefined;

Callers 2

tryGetThisTypeAtFunction · 0.85

Calls 13

getContextualSignatureFunction · 0.85
getTypeOfSymbolFunction · 0.85
instantiateTypeFunction · 0.85
getMapperFromContextFunction · 0.85
getInferenceContextFunction · 0.85
getWidenedTypeFunction · 0.85
getNonNullableTypeFunction · 0.85
checkExpressionCachedFunction · 0.85

Tested by

no test coverage detected