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

Function checkArrayLiteral

test/fixtures/snapshot/typescript.js:73346–73416  ·  view source on GitHub ↗
(node, checkMode, forceTuple)

Source from the content-addressed store, hash-verified

73344 (node.kind === 221 /* SyntaxKind.BinaryExpression */ && node.operatorToken.kind === 63 /* SyntaxKind.EqualsToken */);
73345 }
73346 function checkArrayLiteral(node, checkMode, forceTuple) {
73347 var elements = node.elements;
73348 var elementCount = elements.length;
73349 var elementTypes = [];
73350 var elementFlags = [];
73351 var contextualType = getApparentTypeOfContextualType(node);
73352 var inDestructuringPattern = ts.isAssignmentTarget(node);
73353 var inConstContext = isConstContext(node);
73354 var hasOmittedExpression = false;
73355 for (var i = 0; i < elementCount; i++) {
73356 var e = elements[i];
73357 if (e.kind === 225 /* SyntaxKind.SpreadElement */) {
73358 if (languageVersion < 2 /* ScriptTarget.ES2015 */) {
73359 checkExternalEmitHelpers(e, compilerOptions.downlevelIteration ? 1536 /* ExternalEmitHelpers.SpreadIncludes */ : 1024 /* ExternalEmitHelpers.SpreadArray */);
73360 }
73361 var spreadType = checkExpression(e.expression, checkMode, forceTuple);
73362 if (isArrayLikeType(spreadType)) {
73363 elementTypes.push(spreadType);
73364 elementFlags.push(8 /* ElementFlags.Variadic */);
73365 }
73366 else if (inDestructuringPattern) {
73367 // Given the following situation:
73368 // var c: {};
73369 // [...c] = ["", 0];
73370 //
73371 // c is represented in the tree as a spread element in an array literal.
73372 // But c really functions as a rest element, and its purpose is to provide
73373 // a contextual type for the right hand side of the assignment. Therefore,
73374 // instead of calling checkExpression on "...c", which will give an error
73375 // if c is not iterable/array-like, we need to act as if we are trying to
73376 // get the contextual element type from it. So we do something similar to
73377 // getContextualTypeForElementExpression, which will crucially not error
73378 // if there is no index type / iterated type.
73379 var restElementType = getIndexTypeOfType(spreadType, numberType) ||
73380 getIteratedTypeOrElementType(65 /* IterationUse.Destructuring */, spreadType, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false) ||
73381 unknownType;
73382 elementTypes.push(restElementType);
73383 elementFlags.push(4 /* ElementFlags.Rest */);
73384 }
73385 else {
73386 elementTypes.push(checkIteratedTypeOrElementType(33 /* IterationUse.Spread */, spreadType, undefinedType, e.expression));
73387 elementFlags.push(4 /* ElementFlags.Rest */);
73388 }
73389 }
73390 else if (exactOptionalPropertyTypes && e.kind === 227 /* SyntaxKind.OmittedExpression */) {
73391 hasOmittedExpression = true;
73392 elementTypes.push(missingType);
73393 elementFlags.push(2 /* ElementFlags.Optional */);
73394 }
73395 else {
73396 var elementContextualType = getContextualTypeForElementExpression(contextualType, elementTypes.length);
73397 var type = checkExpressionForMutableLocation(e, checkMode, elementContextualType, forceTuple);
73398 elementTypes.push(addOptionality(type, /*isProperty*/ true, hasOmittedExpression));
73399 elementFlags.push(hasOmittedExpression ? 2 /* ElementFlags.Optional */ : 1 /* ElementFlags.Required */);
73400 if (contextualType && someType(contextualType, isTupleLikeType) && checkMode && checkMode & 2 /* CheckMode.Inferential */ && !(checkMode & 4 /* CheckMode.SkipContextSensitive */) && isContextSensitive(e)) {
73401 var inferenceContext = getInferenceContext(node);
73402 ts.Debug.assert(inferenceContext); // In CheckMode.Inferential we should always have an inference context
73403 addIntraExpressionInferenceSite(inferenceContext, e, type);

Callers 2

elaborateArrayLiteralFunction · 0.85
checkExpressionWorkerFunction · 0.85

Calls 15

isConstContextFunction · 0.85
checkExternalEmitHelpersFunction · 0.85
isArrayLikeTypeFunction · 0.85
getIndexTypeOfTypeFunction · 0.85
addOptionalityFunction · 0.85
someTypeFunction · 0.85
isContextSensitiveFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…