Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/parser): Handle async in for...of #5646

Merged
merged 8 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
//!
//! x Expected '=>', got 'x'
//! ,----
//! 5 | for await (async of x) {}
//! : ^
//! `----
//!
//!
//!Caused by:
//! 0: failed to process input file
//! 1: Syntax Error
// @target: esnext
import _async_iterator from "@swc/helpers/src/_async_iterator.mjs";
import _async_to_generator from "@swc/helpers/src/_async_to_generator.mjs";
function foo(x) {
return _foo.apply(this, arguments);
}
function _foo() {
_foo = _async_to_generator(function*(x) {
var async;
{
var _iteratorAbruptCompletion = false, _didIteratorError = false, _iteratorError;
try {
for(var _iterator = _async_iterator(x), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false){
let _value = _step.value;
async = _value;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally{
try {
if (_iteratorAbruptCompletion && _iterator.return != null) {
yield _iterator.return();
}
} finally{
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
});
return _foo.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
//!
//! x Expected '=>', got 'x'
//! ,----
//! 5 | for await (async of x) {}
//! : ^
//! `----
//!
//!
//!Caused by:
//! 0: failed to process input file
//! 1: Syntax Error
import _async_iterator from "@swc/helpers/src/_async_iterator.mjs";
import _async_to_generator from "@swc/helpers/src/_async_to_generator.mjs";
Original file line number Diff line number Diff line change
@@ -1,11 +1,99 @@
//!
//! x Expected '=>', got 'x'
//! ,----
//! 5 | for await (async of x) {}
//! : ^
//! `----
//!
//!
//!Caused by:
//! 0: failed to process input file
//! 1: Syntax Error
// @target: esnext
import _async_iterator from "@swc/helpers/src/_async_iterator.mjs";
import _async_to_generator from "@swc/helpers/src/_async_to_generator.mjs";
import _ts_generator from "@swc/helpers/src/_ts_generator.mjs";
function foo(x) {
return _foo.apply(this, arguments);
}
function _foo() {
_foo = _async_to_generator(function(x) {
var async, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, err;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
_iteratorAbruptCompletion = false, _didIteratorError = false;
_state.label = 1;
case 1:
_state.trys.push([
1,
6,
7,
12
]);
_iterator = _async_iterator(x);
_state.label = 2;
case 2:
return [
4,
_iterator.next()
];
case 3:
if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
3,
5
];
_value = _step.value;
async = _value;
_state.label = 4;
case 4:
_iteratorAbruptCompletion = false;
return [
3,
2
];
case 5:
return [
3,
12
];
case 6:
err = _state.sent();
_didIteratorError = true;
_iteratorError = err;
return [
3,
12
];
case 7:
_state.trys.push([
7,
,
10,
11
]);
if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
3,
9
];
return [
4,
_iterator.return()
];
case 8:
_state.sent();
_state.label = 9;
case 9:
return [
3,
11
];
case 10:
if (_didIteratorError) {
throw _iteratorError;
}
return [
7
];
case 11:
return [
7
];
case 12:
return [
2
];
}
});
});
return _foo.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//!
//! x Expected '=>', got 'x'
//! ,----
//! 5 | for await (async of x) {}
//! : ^
//! `----
//!
//!
//!Caused by:
//! 0: failed to process input file
//! 1: Syntax Error
import _async_iterator from "@swc/helpers/src/_async_iterator.mjs";
import _async_to_generator from "@swc/helpers/src/_async_to_generator.mjs";
import _ts_generator from "@swc/helpers/src/_ts_generator.mjs";
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pub enum SyntaxError {
TS1100,
TS1102,
TS1105,
TS1106,
TS1107,
TS1109,
TS1110,
Expand Down Expand Up @@ -586,6 +587,9 @@ impl SyntaxError {
SyntaxError::TS1105 => "A 'break' statement can only be used within an enclosing \
iteration or switch statement"
.into(),
SyntaxError::TS1106 => {
"The left-hand side of a `for...of` statement may not be `async`".into()
}
SyntaxError::TS1107 => "Jump target cannot cross function boundary".into(),
SyntaxError::TS1109 => "Expression expected".into(),
SyntaxError::TS1114 => "Duplicate label".into(),
Expand Down
11 changes: 11 additions & 0 deletions crates/swc_ecma_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ pub struct Context {
module: bool,
can_be_module: bool,
strict: bool,

expr_ctx: ExpressionContext,

include_in_expr: bool,
/// If true, await expression is parsed, and "await" is treated as a
/// keyword.
Expand Down Expand Up @@ -389,6 +392,14 @@ pub struct Context {
disallow_conditional_types: bool,
}

#[derive(Debug, Clone, Copy, Default)]
struct ExpressionContext {
// TODO:
// - include_in
for_loop_init: bool,
for_await_loop_init: bool,
}

#[cfg(test)]
fn with_test_sess<F, Ret>(src: &str, f: F) -> Result<Ret, ::testing::StdErr>
where
Expand Down
19 changes: 19 additions & 0 deletions crates/swc_ecma_parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,25 @@ impl<I: Tokens> Parser<I> {
&& !self.input.had_line_break_before_cur()
&& is!(self, BindingIdent)
{
// see https://github.com/tc39/ecma262/issues/2034
// ```js
// for(async of
// for(async of x);
// for(async of =>{};;);
// ```
if ctx.expr_ctx.for_loop_init && is!(self, "of") && !peeked_is!(self, "=>") {
// ```spec https://tc39.es/ecma262/#prod-ForInOfStatement
// for ( [lookahead ∉ { let, async of }] LeftHandSideExpression[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]
// [+Await] for await ( [lookahead ≠ let] LeftHandSideExpression[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]
// ```

if !ctx.expr_ctx.for_await_loop_init {
self.emit_err(self.input.prev_span(), SyntaxError::TS1106);
}

return Ok(Box::new(Expr::Ident(id)));
}

let ident = self.parse_binding_ident()?;
if self.input.syntax().typescript()
&& ident.id.sym == js_word!("as")
Expand Down
19 changes: 18 additions & 1 deletion crates/swc_ecma_parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,12 @@ impl<'a, I: Tokens> Parser<I> {
None
};
expect!(self, '(');
let head = self.parse_for_head()?;

let mut ctx = self.ctx();
ctx.expr_ctx.for_loop_init = true;
ctx.expr_ctx.for_await_loop_init = await_token.is_some();

let head = self.with_ctx(ctx).parse_for_head()?;
expect!(self, ')');
let ctx = Context {
is_break_allowed: true,
Expand Down Expand Up @@ -2067,6 +2072,18 @@ export default function waitUntil(callback, options = {}) {
);
}

#[test]
fn for_of_head_lhs_async_dot() {
let src = "for (async.x of [1]) ;";
test_parser(src, Syntax::Es(Default::default()), |p| p.parse_module());
}

#[test]
fn for_head_init_async_of() {
let src = "for (async of => {}; i < 10; ++i) { ++counter; }";
test_parser(src, Syntax::Es(Default::default()), |p| p.parse_module());
}

#[test]
#[should_panic(expected = "await isn't allowed in non-async function")]
fn await_in_function_in_module() {
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_parser/tests/errors/ts1106/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let async;
for (async of [1]) ;
6 changes: 6 additions & 0 deletions crates/swc_ecma_parser/tests/errors/ts1106/input.js.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

x The left-hand side of a `for...of` statement may not be `async`
,-[$DIR/tests/errors/ts1106/input.js:2:1]
2 | for (async of [1]) ;
: ^^^^^
`----