* Flattens a DestructuringAssignment or a VariableDeclaration to an expression. * * @param node The node to flatten. * @param visitor An optional visitor used to visit initializers. * @param context The transformation context. * @param level Indicates the extent to which fla
(node, visitor, context, level, needsValue, createAssignmentCallback)
| 91000 | * @param createAssignmentCallback An optional callback used to create the assignment expression. |
| 91001 | */ |
| 91002 | function flattenDestructuringAssignment(node, visitor, context, level, needsValue, createAssignmentCallback) { |
| 91003 | var location = node; |
| 91004 | var value; |
| 91005 | if (ts.isDestructuringAssignment(node)) { |
| 91006 | value = node.right; |
| 91007 | while (ts.isEmptyArrayLiteral(node.left) || ts.isEmptyObjectLiteral(node.left)) { |
| 91008 | if (ts.isDestructuringAssignment(value)) { |
| 91009 | location = node = value; |
| 91010 | value = node.right; |
| 91011 | } |
| 91012 | else { |
| 91013 | return ts.visitNode(value, visitor, ts.isExpression); |
| 91014 | } |
| 91015 | } |
| 91016 | } |
| 91017 | var expressions; |
| 91018 | var flattenContext = { |
| 91019 | context: context, |
| 91020 | level: level, |
| 91021 | downlevelIteration: !!context.getCompilerOptions().downlevelIteration, |
| 91022 | hoistTempVariables: true, |
| 91023 | emitExpression: emitExpression, |
| 91024 | emitBindingOrAssignment: emitBindingOrAssignment, |
| 91025 | createArrayBindingOrAssignmentPattern: function (elements) { return makeArrayAssignmentPattern(context.factory, elements); }, |
| 91026 | createObjectBindingOrAssignmentPattern: function (elements) { return makeObjectAssignmentPattern(context.factory, elements); }, |
| 91027 | createArrayBindingOrAssignmentElement: makeAssignmentElement, |
| 91028 | visitor: visitor |
| 91029 | }; |
| 91030 | if (value) { |
| 91031 | value = ts.visitNode(value, visitor, ts.isExpression); |
| 91032 | if (ts.isIdentifier(value) && bindingOrAssignmentElementAssignsToName(node, value.escapedText) || |
| 91033 | bindingOrAssignmentElementContainsNonLiteralComputedName(node)) { |
| 91034 | // If the right-hand value of the assignment is also an assignment target then |
| 91035 | // we need to cache the right-hand value. |
| 91036 | value = ensureIdentifier(flattenContext, value, /*reuseIdentifierExpressions*/ false, location); |
| 91037 | } |
| 91038 | else if (needsValue) { |
| 91039 | // If the right-hand value of the destructuring assignment needs to be preserved (as |
| 91040 | // is the case when the destructuring assignment is part of a larger expression), |
| 91041 | // then we need to cache the right-hand value. |
| 91042 | // |
| 91043 | // The source map location for the assignment should point to the entire binary |
| 91044 | // expression. |
| 91045 | value = ensureIdentifier(flattenContext, value, /*reuseIdentifierExpressions*/ true, location); |
| 91046 | } |
| 91047 | else if (ts.nodeIsSynthesized(node)) { |
| 91048 | // Generally, the source map location for a destructuring assignment is the root |
| 91049 | // expression. |
| 91050 | // |
| 91051 | // However, if the root expression is synthesized (as in the case |
| 91052 | // of the initializer when transforming a ForOfStatement), then the source map |
| 91053 | // location should point to the right-hand value of the expression. |
| 91054 | location = value; |
| 91055 | } |
| 91056 | } |
| 91057 | flattenBindingOrAssignmentElement(flattenContext, node, value, location, /*skipInitializer*/ ts.isDestructuringAssignment(node)); |
| 91058 | if (value && needsValue) { |
| 91059 | if (!ts.some(expressions)) { |
nothing calls this directly
no test coverage detected