Handle foo && yield bar (#218)

Fix #216.
This commit is contained in:
강동윤 2019-02-15 13:58:10 +09:00 committed by GitHub
parent 1d865b9979
commit ad49de35b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View File

@ -582,12 +582,19 @@ impl Actual {
fn make_fn_ref(mut expr: FnExpr) -> Expr {
struct AwaitToYield;
impl Fold<Function> for AwaitToYield {
macro_rules! noop {
($T:path) => {
impl Fold<$T> for AwaitToYield {
/// Don't recurse into function.
fn fold(&mut self, f: Function) -> Function {
fn fold(&mut self, f: $T) -> $T {
f
}
}
};
}
noop!(Function);
noop!(Constructor);
noop!(ArrowExpr);
impl Fold<Expr> for AwaitToYield {
fn fold(&mut self, expr: Expr) -> Expr {

View File

@ -27,6 +27,28 @@ fn tr() -> impl Fold<Module> {
)
}
test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
issue_216,
r#"
async function foo(bar) {
bar && await bar();
}
"#,
r#"
function _foo() {
_foo = _asyncToGenerator(function*(bar) {
bar && (yield bar());
});
return _foo.apply(this, arguments);
}
function foo(bar) {
return _foo.apply(this, arguments);
}
"#
);
test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),

View File

@ -309,7 +309,9 @@ impl Fold<Expr> for Fixer {
Expr::Bin(mut expr) => {
expr.right = match *expr.right {
e @ Expr::Assign(..) | e @ Expr::Seq(..) => box e.wrap_with_paren(),
e @ Expr::Assign(..) | e @ Expr::Seq(..) | e @ Expr::Yield(..) => {
box e.wrap_with_paren()
}
_ => expr.right,
};