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

Function checkForInStatement

test/fixtures/snapshot/typescript.js:82895–82938  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

82893 }
82894 }
82895 function checkForInStatement(node) {
82896 // Grammar checking
82897 checkGrammarForInOrForOfStatement(node);
82898 var rightType = getNonNullableTypeIfNeeded(checkExpression(node.expression));
82899 // TypeScript 1.0 spec (April 2014): 5.4
82900 // In a 'for-in' statement of the form
82901 // for (let VarDecl in Expr) Statement
82902 // VarDecl must be a variable declaration without a type annotation that declares a variable of type Any,
82903 // and Expr must be an expression of type Any, an object type, or a type parameter type.
82904 if (node.initializer.kind === 255 /* SyntaxKind.VariableDeclarationList */) {
82905 var variable = node.initializer.declarations[0];
82906 if (variable && ts.isBindingPattern(variable.name)) {
82907 error(variable.name, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern);
82908 }
82909 checkForInOrForOfVariableDeclaration(node);
82910 }
82911 else {
82912 // In a 'for-in' statement of the form
82913 // for (Var in Expr) Statement
82914 // Var must be an expression classified as a reference of type Any or the String primitive type,
82915 // and Expr must be an expression of type Any, an object type, or a type parameter type.
82916 var varExpr = node.initializer;
82917 var leftType = checkExpression(varExpr);
82918 if (varExpr.kind === 204 /* SyntaxKind.ArrayLiteralExpression */ || varExpr.kind === 205 /* SyntaxKind.ObjectLiteralExpression */) {
82919 error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern);
82920 }
82921 else if (!isTypeAssignableTo(getIndexTypeOrString(rightType), leftType)) {
82922 error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_must_be_of_type_string_or_any);
82923 }
82924 else {
82925 // run check only former check succeeded to avoid cascading errors
82926 checkReferenceExpression(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_must_be_a_variable_or_a_property_access, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_may_not_be_an_optional_property_access);
82927 }
82928 }
82929 // unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
82930 // in this case error about missing name is already reported - do not report extra one
82931 if (rightType === neverType || !isTypeAssignableToKind(rightType, 67108864 /* TypeFlags.NonPrimitive */ | 58982400 /* TypeFlags.InstantiableNonPrimitive */)) {
82932 error(node.expression, ts.Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter_but_here_has_type_0, typeToString(rightType));
82933 }
82934 checkSourceElement(node.statement);
82935 if (node.locals) {
82936 registerForUnusedIdentifiersCheck(node);
82937 }
82938 }
82939 function checkForInOrForOfVariableDeclaration(iterationStatement) {
82940 var variableDeclarationList = iterationStatement.initializer;
82941 // checkGrammarForInOrForOfStatement will check that there is exactly one declaration.

Callers 1

checkSourceElementWorkerFunction · 0.85

Calls 12

isTypeAssignableToFunction · 0.85
getIndexTypeOrStringFunction · 0.85
checkReferenceExpressionFunction · 0.85
isTypeAssignableToKindFunction · 0.85
typeToStringFunction · 0.85
checkSourceElementFunction · 0.85
checkExpressionFunction · 0.70
errorFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…