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

Function flattenDestructuringBinding

test/fixtures/snapshot/typescript.js:91120–91184  ·  view source on GitHub ↗

* Flattens a VariableDeclaration or ParameterDeclaration to one or more variable declarations. * * @param node The node to flatten. * @param visitor An optional visitor used to visit initializers. * @param context The transformation context. * @param boundValue The value bou

(node, visitor, context, level, rval, hoistTempVariables, skipInitializer)

Source from the content-addressed store, hash-verified

91118 * @param level Indicates the extent to which flattening should occur.
91119 */
91120 function flattenDestructuringBinding(node, visitor, context, level, rval, hoistTempVariables, skipInitializer) {
91121 if (hoistTempVariables === void 0) { hoistTempVariables = false; }
91122 var pendingExpressions;
91123 var pendingDeclarations = [];
91124 var declarations = [];
91125 var flattenContext = {
91126 context: context,
91127 level: level,
91128 downlevelIteration: !!context.getCompilerOptions().downlevelIteration,
91129 hoistTempVariables: hoistTempVariables,
91130 emitExpression: emitExpression,
91131 emitBindingOrAssignment: emitBindingOrAssignment,
91132 createArrayBindingOrAssignmentPattern: function (elements) { return makeArrayBindingPattern(context.factory, elements); },
91133 createObjectBindingOrAssignmentPattern: function (elements) { return makeObjectBindingPattern(context.factory, elements); },
91134 createArrayBindingOrAssignmentElement: function (name) { return makeBindingElement(context.factory, name); },
91135 visitor: visitor
91136 };
91137 if (ts.isVariableDeclaration(node)) {
91138 var initializer = ts.getInitializerOfBindingOrAssignmentElement(node);
91139 if (initializer && (ts.isIdentifier(initializer) && bindingOrAssignmentElementAssignsToName(node, initializer.escapedText) ||
91140 bindingOrAssignmentElementContainsNonLiteralComputedName(node))) {
91141 // If the right-hand value of the assignment is also an assignment target then
91142 // we need to cache the right-hand value.
91143 initializer = ensureIdentifier(flattenContext, ts.visitNode(initializer, flattenContext.visitor), /*reuseIdentifierExpressions*/ false, initializer);
91144 node = context.factory.updateVariableDeclaration(node, node.name, /*exclamationToken*/ undefined, /*type*/ undefined, initializer);
91145 }
91146 }
91147 flattenBindingOrAssignmentElement(flattenContext, node, rval, node, skipInitializer);
91148 if (pendingExpressions) {
91149 var temp = context.factory.createTempVariable(/*recordTempVariable*/ undefined);
91150 if (hoistTempVariables) {
91151 var value = context.factory.inlineExpressions(pendingExpressions);
91152 pendingExpressions = undefined;
91153 emitBindingOrAssignment(temp, value, /*location*/ undefined, /*original*/ undefined);
91154 }
91155 else {
91156 context.hoistVariableDeclaration(temp);
91157 var pendingDeclaration = ts.last(pendingDeclarations);
91158 pendingDeclaration.pendingExpressions = ts.append(pendingDeclaration.pendingExpressions, context.factory.createAssignment(temp, pendingDeclaration.value));
91159 ts.addRange(pendingDeclaration.pendingExpressions, pendingExpressions);
91160 pendingDeclaration.value = temp;
91161 }
91162 }
91163 for (var _i = 0, pendingDeclarations_1 = pendingDeclarations; _i < pendingDeclarations_1.length; _i++) {
91164 var _a = pendingDeclarations_1[_i], pendingExpressions_1 = _a.pendingExpressions, name = _a.name, value = _a.value, location = _a.location, original = _a.original;
91165 var variable = context.factory.createVariableDeclaration(name,
91166 /*exclamationToken*/ undefined,
91167 /*type*/ undefined, pendingExpressions_1 ? context.factory.inlineExpressions(ts.append(pendingExpressions_1, value)) : value);
91168 variable.original = original;
91169 ts.setTextRange(variable, location);
91170 declarations.push(variable);
91171 }
91172 return declarations;
91173 function emitExpression(value) {
91174 pendingExpressions = ts.append(pendingExpressions, value);
91175 }
91176 function emitBindingOrAssignment(target, value, location, original) {
91177 ts.Debug.assertNode(target, ts.isBindingName);

Callers

nothing calls this directly

Calls 12

makeArrayBindingPatternFunction · 0.85
makeObjectBindingPatternFunction · 0.85
makeBindingElementFunction · 0.85
ensureIdentifierFunction · 0.85
emitBindingOrAssignmentFunction · 0.85
addRangeMethod · 0.80
lastMethod · 0.45
appendMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected