* Flattens an ArrayBindingOrAssignmentPattern into zero or more bindings or assignments. * * @param flattenContext Options used to control flattening. * @param parent The parent element of the pattern. * @param pattern The ArrayBindingOrAssignmentPattern to flatten. * @param
(flattenContext, parent, pattern, value, location)
| 91292 | * @param location The location to use for source maps and comments. |
| 91293 | */ |
| 91294 | function flattenArrayBindingOrAssignmentPattern(flattenContext, parent, pattern, value, location) { |
| 91295 | var elements = ts.getElementsOfBindingOrAssignmentPattern(pattern); |
| 91296 | var numElements = elements.length; |
| 91297 | if (flattenContext.level < 1 /* FlattenLevel.ObjectRest */ && flattenContext.downlevelIteration) { |
| 91298 | // Read the elements of the iterable into an array |
| 91299 | value = ensureIdentifier(flattenContext, ts.setTextRange(flattenContext.context.getEmitHelperFactory().createReadHelper(value, numElements > 0 && ts.getRestIndicatorOfBindingOrAssignmentElement(elements[numElements - 1]) |
| 91300 | ? undefined |
| 91301 | : numElements), location), |
| 91302 | /*reuseIdentifierExpressions*/ false, location); |
| 91303 | } |
| 91304 | else if (numElements !== 1 && (flattenContext.level < 1 /* FlattenLevel.ObjectRest */ || numElements === 0) |
| 91305 | || ts.every(elements, ts.isOmittedExpression)) { |
| 91306 | // For anything other than a single-element destructuring we need to generate a temporary |
| 91307 | // to ensure value is evaluated exactly once. Additionally, if we have zero elements |
| 91308 | // we need to emit *something* to ensure that in case a 'var' keyword was already emitted, |
| 91309 | // so in that case, we'll intentionally create that temporary. |
| 91310 | // Or all the elements of the binding pattern are omitted expression such as "var [,] = [1,2]", |
| 91311 | // then we will create temporary variable. |
| 91312 | var reuseIdentifierExpressions = !ts.isDeclarationBindingElement(parent) || numElements !== 0; |
| 91313 | value = ensureIdentifier(flattenContext, value, reuseIdentifierExpressions, location); |
| 91314 | } |
| 91315 | var bindingElements; |
| 91316 | var restContainingElements; |
| 91317 | for (var i = 0; i < numElements; i++) { |
| 91318 | var element = elements[i]; |
| 91319 | if (flattenContext.level >= 1 /* FlattenLevel.ObjectRest */) { |
| 91320 | // If an array pattern contains an ObjectRest, we must cache the result so that we |
| 91321 | // can perform the ObjectRest destructuring in a different declaration |
| 91322 | if (element.transformFlags & 32768 /* TransformFlags.ContainsObjectRestOrSpread */ || flattenContext.hasTransformedPriorElement && !isSimpleBindingOrAssignmentElement(element)) { |
| 91323 | flattenContext.hasTransformedPriorElement = true; |
| 91324 | var temp = flattenContext.context.factory.createTempVariable(/*recordTempVariable*/ undefined); |
| 91325 | if (flattenContext.hoistTempVariables) { |
| 91326 | flattenContext.context.hoistVariableDeclaration(temp); |
| 91327 | } |
| 91328 | restContainingElements = ts.append(restContainingElements, [temp, element]); |
| 91329 | bindingElements = ts.append(bindingElements, flattenContext.createArrayBindingOrAssignmentElement(temp)); |
| 91330 | } |
| 91331 | else { |
| 91332 | bindingElements = ts.append(bindingElements, element); |
| 91333 | } |
| 91334 | } |
| 91335 | else if (ts.isOmittedExpression(element)) { |
| 91336 | continue; |
| 91337 | } |
| 91338 | else if (!ts.getRestIndicatorOfBindingOrAssignmentElement(element)) { |
| 91339 | var rhsValue = flattenContext.context.factory.createElementAccessExpression(value, i); |
| 91340 | flattenBindingOrAssignmentElement(flattenContext, element, rhsValue, /*location*/ element); |
| 91341 | } |
| 91342 | else if (i === numElements - 1) { |
| 91343 | var rhsValue = flattenContext.context.factory.createArraySliceCall(value, i); |
| 91344 | flattenBindingOrAssignmentElement(flattenContext, element, rhsValue, /*location*/ element); |
| 91345 | } |
| 91346 | } |
| 91347 | if (bindingElements) { |
| 91348 | flattenContext.emitBindingOrAssignment(flattenContext.createArrayBindingOrAssignmentPattern(bindingElements), value, location, pattern); |
| 91349 | } |
| 91350 | if (restContainingElements) { |
| 91351 | for (var _i = 0, restContainingElements_1 = restContainingElements; _i < restContainingElements_1.length; _i++) { |
no test coverage detected