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

Function getContextualType

test/fixtures/snapshot/typescript.js:72981–73051  ·  view source on GitHub ↗

* Whoa! Do you really want to use this function? * * Unless you're trying to get the *non-apparent* type for a * value-literal type or you're authoring relevant portions of this algorithm, * you probably meant to use 'getApparentTypeOfContextualType'. * O

(node, contextFlags)

Source from the content-addressed store, hash-verified

72979 * @returns the contextual type of an expression.
72980 */
72981 function getContextualType(node, contextFlags) {
72982 if (node.flags & 33554432 /* NodeFlags.InWithStatement */) {
72983 // We cannot answer semantic questions within a with block, do not proceed any further
72984 return undefined;
72985 }
72986 if (node.contextualType) {
72987 return node.contextualType;
72988 }
72989 var parent = node.parent;
72990 switch (parent.kind) {
72991 case 254 /* SyntaxKind.VariableDeclaration */:
72992 case 164 /* SyntaxKind.Parameter */:
72993 case 167 /* SyntaxKind.PropertyDeclaration */:
72994 case 166 /* SyntaxKind.PropertySignature */:
72995 case 203 /* SyntaxKind.BindingElement */:
72996 return getContextualTypeForInitializerExpression(node, contextFlags);
72997 case 214 /* SyntaxKind.ArrowFunction */:
72998 case 247 /* SyntaxKind.ReturnStatement */:
72999 return getContextualTypeForReturnExpression(node);
73000 case 224 /* SyntaxKind.YieldExpression */:
73001 return getContextualTypeForYieldOperand(parent);
73002 case 218 /* SyntaxKind.AwaitExpression */:
73003 return getContextualTypeForAwaitOperand(parent, contextFlags);
73004 case 208 /* SyntaxKind.CallExpression */:
73005 case 209 /* SyntaxKind.NewExpression */:
73006 return getContextualTypeForArgument(parent, node);
73007 case 211 /* SyntaxKind.TypeAssertionExpression */:
73008 case 229 /* SyntaxKind.AsExpression */:
73009 return ts.isConstTypeReference(parent.type) ? tryFindWhenConstTypeReference(parent) : getTypeFromTypeNode(parent.type);
73010 case 221 /* SyntaxKind.BinaryExpression */:
73011 return getContextualTypeForBinaryOperand(node, contextFlags);
73012 case 296 /* SyntaxKind.PropertyAssignment */:
73013 case 297 /* SyntaxKind.ShorthandPropertyAssignment */:
73014 return getContextualTypeForObjectLiteralElement(parent, contextFlags);
73015 case 298 /* SyntaxKind.SpreadAssignment */:
73016 return getContextualType(parent.parent, contextFlags);
73017 case 204 /* SyntaxKind.ArrayLiteralExpression */: {
73018 var arrayLiteral = parent;
73019 var type = getApparentTypeOfContextualType(arrayLiteral, contextFlags);
73020 return getContextualTypeForElementExpression(type, ts.indexOfNode(arrayLiteral.elements, node));
73021 }
73022 case 222 /* SyntaxKind.ConditionalExpression */:
73023 return getContextualTypeForConditionalOperand(node, contextFlags);
73024 case 233 /* SyntaxKind.TemplateSpan */:
73025 ts.Debug.assert(parent.parent.kind === 223 /* SyntaxKind.TemplateExpression */);
73026 return getContextualTypeForSubstitutionExpression(parent.parent, node);
73027 case 212 /* SyntaxKind.ParenthesizedExpression */: {
73028 // Like in `checkParenthesizedExpression`, an `/** @type {xyz} */` comment before a parenthesized expression acts as a type cast.
73029 var tag = ts.isInJSFile(parent) ? ts.getJSDocTypeTag(parent) : undefined;
73030 return !tag ? getContextualType(parent, contextFlags) :
73031 ts.isJSDocTypeTag(tag) && ts.isConstTypeReference(tag.typeExpression.type) ? tryFindWhenConstTypeReference(parent) :
73032 getTypeFromTypeNode(tag.typeExpression.type);
73033 }
73034 case 230 /* SyntaxKind.NonNullExpression */:
73035 return getContextualType(parent, contextFlags);
73036 case 271 /* SyntaxKind.ExportAssignment */:
73037 return tryGetTypeFromEffectiveTypeNode(parent);
73038 case 288 /* SyntaxKind.JsxExpression */:

Tested by

no test coverage detected