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

The async keyword should not be followed by a newline #1515

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

## Bug fixes
* Runtime: fix Dom_html.onIE (#1493)
* Compiler: fix global flow analysis (#1494)
* Runtime: add conversion functions + strict equality for compatibility with Wasm_of_ocaml (#1492)
* Compiler: fix global flow analysis (#1494)
* COmpiler: fix js parser/printer wrt async functions (#1515)

# 5.4.0 (2023-07-06) - Lille

Expand Down
4 changes: 2 additions & 2 deletions compiler/lib/js_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ struct
(match k with
| { async = true; generator = false } ->
PP.string f "async";
PP.space f
PP.non_breaking_space f
| { async = false; generator = false } -> ()
| { async = true | false; generator = true } -> assert false);
PP.break f;
Expand Down Expand Up @@ -882,7 +882,7 @@ struct
PP.space f
| { async = true; generator = false } ->
PP.string f "async";
PP.space f
PP.non_breaking_space f
| { async = true; generator = true } ->
PP.string f "async*";
PP.space f);
Expand Down
16 changes: 16 additions & 0 deletions compiler/lib/js_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ decl:
{ let i,f = $1 in Function_declaration (i,f), p $symbolstartpos }
| generator_decl
{ let i,f = $1 in Function_declaration (i,f), p $symbolstartpos }
| async_generator_decl
{ let i,f = $1 in Function_declaration (i,f), p $symbolstartpos }
| async_decl
{ let i,f = $1 in Function_declaration (i,f), p $symbolstartpos }
| lexical_decl { $1, p $symbolstartpos }
Expand Down Expand Up @@ -432,6 +434,18 @@ async_function_expr:
| T_ASYNC T_FUNCTION name=ident? args=call_signature "{" b=function_body "}"
{ EFun (name, ({async = true; generator = false}, args, b, p $symbolstartpos)) }

(*************************************************************************)
(* async generators *)
(*************************************************************************)

async_generator_decl:
| T_ASYNC T_FUNCTION "*" name=ident args=call_signature "{" b=function_body "}"
{ (name, ({async = true; generator = true}, args, b, p $symbolstartpos)) }

async_generator_expr:
| T_ASYNC T_FUNCTION "*" name=ident? args=call_signature "{" b=function_body "}"
{ EFun (name, ({async = true; generator = true}, args, b, p $symbolstartpos)) }

(*************************************************************************)
(* Class declaration *)
(*************************************************************************)
Expand Down Expand Up @@ -766,6 +780,7 @@ primary_with_stmt:
| generator_expr { $1 }
(* es7: *)
| async_function_expr { $1 }
| async_generator_expr{ $1 }


primary_expr_no_braces:
Expand Down Expand Up @@ -988,6 +1003,7 @@ primary_for_consise_body:
| generator_expr { $1 }
(* es7: *)
| async_function_expr { $1 }
| async_generator_expr{ $1 }

assignment_expr_for_consise_body:
| conditional_expr(primary_for_consise_body) { $1 }
Expand Down
7 changes: 5 additions & 2 deletions compiler/lib/parse_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,12 @@ let rec offer_one t (lexbuf : Lexer.t) =
* one LineTerminator, then a semicolon is automatically inserted before the
* restricted token. *)
match State.Cursor.last_token h, tok with
| ( Some (((T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD), _, _), _)
| ( Some
(((T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD | T_ASYNC), _, _), _)
, (((T_SEMICOLON | T_VIRTUAL_SEMICOLON), _, _) as tok) ) -> tok
| Some (((T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD), _, _), _), _
| ( Some
(((T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD | T_ASYNC), _, _), _)
, _ )
when nl_separated h tok && acceptable t T_VIRTUAL_SEMICOLON ->
(* restricted token can also appear as regular identifier such
as in [x.return]. In such case, feeding a virtual semicolon
Expand Down
44 changes: 42 additions & 2 deletions compiler/tests-compiler/js_parser_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,20 @@ let%expect_test "async/await" =
const glslang = await glslangModule.default();
return glslang.compileGLSL(src, "compute");
}


async
function test() { }

async function test() { }

async
function* test() { }

async function * test() { }

1 + async function * test() { }

|};

[%expect
Expand All @@ -355,7 +369,14 @@ let%expect_test "async/await" =
glslang = await /*<<fake:7:33>>*/ glslangModule.default();
/*<<fake:8:11>>*/ return /*<<fake:8:18>>*/ glslang.compileGLSL
(src, "compute");
/*<<fake:2:9>>*/ } |}]
/*<<fake:2:9>>*/ }
/*<<fake:12:4>>*/ async;
/*<<fake:13:4>>*/ function test(){ /*<<fake:13:22>>*/ }
/*<<fake:15:4>>*/ async function test(){ /*<<fake:15:4>>*/ }
/*<<fake:17:4>>*/ async;
/*<<fake:18:4>>*/ function* test(){ /*<<fake:18:4>>*/ }
/*<<fake:20:4>>*/ async function* test(){ /*<<fake:20:4>>*/ }
/*<<fake:22:4>>*/ 1 + async function* test(){ /*<<fake:22:8>>*/ }; |}]

let%expect_test "get/set property" =
(* GH#1017 *)
Expand Down Expand Up @@ -948,6 +969,18 @@ a:while(true){
do { x } while (true) y
do ; while (true) y

async
function test() { }

async function test() { }

async
function* test() { }

async function * test() { }

1 + async function * test() { }

|};
[%expect
{|
Expand All @@ -974,7 +1007,14 @@ a:while(true){
26: 4:a (identifier), 6:=, 8:b (identifier), 10:+, 12:c (identifier),
27: 4:(, 5:d (identifier), 7:+, 9:e (identifier), 10:), 11:., 12:print (identifier), 17:(, 18:), 0:; (virtual),
29: 4:do, 7:{, 9:x (identifier), 0:; (virtual), 11:}, 13:while, 19:(, 20:true, 24:), 0:; (virtual), 26:y (identifier), 0:; (virtual),
30: 4:do, 7:;, 9:while, 15:(, 16:true, 20:), 0:; (virtual), 22:y (identifier), 0:; (virtual), |}]
30: 4:do, 7:;, 9:while, 15:(, 16:true, 20:), 0:; (virtual), 22:y (identifier), 0:; (virtual),
32: 4:async, 0:; (virtual),
33: 4:function, 13:test (identifier), 17:(, 18:), 20:{, 22:},
35: 4:async, 10:function, 19:test (identifier), 23:(, 24:), 26:{, 28:},
37: 4:async, 0:; (virtual),
38: 4:function, 12:*, 14:test (identifier), 18:(, 19:), 21:{, 23:},
40: 4:async, 10:function, 19:*, 21:test (identifier), 25:(, 26:), 28:{, 30:},
42: 4:1, 6:+, 8:async, 14:function, 23:*, 25:test (identifier), 29:(, 30:), 32:{, 34:}, 0:; (virtual), |}]

let%expect_test _ =
parse_print_token
Expand Down
Loading