Support binding patterns in Promise -> async/await refactor#31008
Conversation
| const transformationBody = getTransformationBody(func, prevArgName, argName, node, transformer); | ||
| const catchArg = argName ? argName.identifier.text : "e"; | ||
| const catchClause = createCatchClause(catchArg, createBlock(transformationBody)); | ||
| const catchArg = argName ? "identifier" in argName ? argName.identifier.text : argName.bindingPattern : "e"; |
There was a problem hiding this comment.
FWIW, there was at least the intent at one point to forbid using the in operator (see the tslint.json in the root). It just so happens that it's a custom rule that doesn't actually work, so that's why this didn't raise an error. I'm not sure if that's something we still don't want to use or not.
There was a problem hiding this comment.
👍🏽 Done, switched to using a const enum discriminated union and a helper function for discriminating between them.
| return transformThen(node, transformer, outermostParent, prevArgName); | ||
| } | ||
| else if (isCallExpression(node) && hasPropertyAccessExpressionWithName(node, "catch") && nodeType && !!transformer.checker.getPromisedTypeOfPromise(nodeType)) { | ||
| else if (isCallExpression(node) && hasPropertyAccessExpressionWithName(node, "catch") && nodeType && !!transformer.checker.getPromisedTypeOfPromise(nodeType) && (!prevArgName || "identifier" in prevArgName)) { |
There was a problem hiding this comment.
Is there a particular reason we fail if we see a catch but the prevArgName is a binding element?
There was a problem hiding this comment.
It doesn't actually fail, it falls through to
else if (nodeType && transformer.checker.getPromisedTypeOfPromise(nodeType)) {
return transformPromiseCall(node, transformer, prevArgName);
}and basically becomes a no-op. This happens only in some really specific structures where a then and a catch both return a value and then another then destructures that value. The result is that the outer promise gets transformed and the inner, nested one doesn't. (And really, the resulting code is more concise than the fully transformed async version in the examples I've been able to track down.) But strictly speaking, this is an incomplete solution. Let me give it another hour or two and see if I can come up with something better.
There was a problem hiding this comment.
Looks like we're missing the baseline file for it?
There was a problem hiding this comment.
Hm, so we are. Updated.
Fixes #29358—the original refactor unsafely cast the params in callbacks in
.then()and.catch()fromBindingNametoIdentifier, which meant if they were in fact a binding pattern, the refactor would crash.