Skip to content

Commit

Permalink
Support patterns in default argument assignments by writing to a temp…
Browse files Browse the repository at this point in the history
…orary variable (closes #29)
  • Loading branch information
rpetrich committed May 25, 2019
1 parent 58855e8 commit ce67368
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
15 changes: 13 additions & 2 deletions async-to-promises.ts
@@ -1,4 +1,4 @@
import { ArrowFunctionExpression, AwaitExpression, BlockStatement, CallExpression, ClassMethod, File, LabeledStatement, Node, Expression, FunctionDeclaration, Statement, Identifier, ForStatement, ForInStatement, SpreadElement, ReturnStatement, ForOfStatement, Function, FunctionExpression, MemberExpression, NumericLiteral, ThisExpression, SwitchCase, Program, VariableDeclaration, VariableDeclarator, StringLiteral, BooleanLiteral, Pattern, LVal, YieldExpression } from "babel-types";
import { ArrowFunctionExpression, AwaitExpression, BlockStatement, CallExpression, ClassMethod, File, LabeledStatement, Node, Expression, FunctionDeclaration, Statement, Identifier, ForStatement, ForInStatement, SpreadElement, ReturnStatement, ForOfStatement, Function, FunctionExpression, MemberExpression, NumericLiteral, ThisExpression, SwitchCase, Program, VariableDeclaration, VariableDeclarator, StringLiteral, BooleanLiteral, Pattern, LVal, ObjectPattern, YieldExpression } from "babel-types";
import { NodePath, Scope, Visitor } from "babel-traverse";
import { code as helperCode } from "./helpers-string";

Expand Down Expand Up @@ -3312,12 +3312,23 @@ export default function({ types, template, traverse, transformFromAst, version }
if (param.isAssignmentPattern()) {
const init = param.get("right");
if (!isExpressionOfLiterals(init, literals)) {
const id = param.get("left").node;
const left = param.get("left") as NodePath<Identifier | ObjectPattern>;
let id: Identifier;
let after: Statement | undefined;
if (left.isIdentifier()) {
id = left.node;
} else {
id = left.scope.generateUidIdentifier(`arg${i}`);
after = types.variableDeclaration("let", [types.variableDeclarator(left.node, id)]);
}
const initNode = init.node;
param.replaceWith(id);
const isMissing = types.binaryExpression("===", id, types.identifier("undefined"));
const assignment = types.expressionStatement(types.assignmentExpression("=", id, initNode));
statements.push(types.ifStatement(isMissing, assignment));
if (after) {
statements.push(after);
}
}
} else if (param.isIdentifier()) {
literals.push(param.node.name);
Expand Down
1 change: 1 addition & 0 deletions tests/default argument pattern/hoisted.js
@@ -0,0 +1 @@
const f=_async(function(_arg){if(_arg===undefined)_arg=(x++,{});let{y=(z++,{})}=_arg;});let x=0,z=0;
1 change: 1 addition & 0 deletions tests/default argument pattern/inlined.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/default argument pattern/input.js
@@ -0,0 +1,4 @@
let x = 0, z = 0;

async function f({y = (z++, {})} = (x++, {})) {
}
4 changes: 4 additions & 0 deletions tests/default argument pattern/options.json
@@ -0,0 +1,4 @@
{
"checkSyntax": false,
"module": true
}
1 change: 1 addition & 0 deletions tests/default argument pattern/output.js
@@ -0,0 +1 @@
const f=_async((_arg)=>{if(_arg===undefined)_arg=(x++,{});let{y=(z++,{})}=_arg;});let x=0,z=0;

0 comments on commit ce67368

Please sign in to comment.