Skip to content

Compiler: fix js parser with semi-keyword #1395

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

Merged
merged 1 commit into from
Jan 25, 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
14 changes: 14 additions & 0 deletions compiler/lib/js_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,20 @@ variable_with_loc:
let name, _raw = i in
var (p $symbolstartpos) name
}
| ident_semi_keyword { var (p $symbolstartpos) (utf8_s (Js_token.to_string $1)) }

(* add here keywords which are not considered reserved by ECMA *)
ident_semi_keyword:
| T_OF { T_OF }
| T_TYPE { T_TYPE }
| T_DECLARE { T_DECLARE }
| T_PUBLIC { T_PUBLIC } | T_PRIVATE { T_PRIVATE } | T_PROTECTED { T_PROTECTED }
(* can have AS and ASYNC here but need to restrict arrow_function then *)
| T_ASYNC { T_ASYNC }
(* TODO: would like to add T_IMPORT here, but cause conflicts *)
| T_PACKAGE { T_PACKAGE }
| T_IMPLEMENTS { T_IMPLEMENTS }
| T_OPAQUE { T_OPAQUE }

label:
| T_IDENTIFIER {
Expand Down
18 changes: 18 additions & 0 deletions compiler/tests-compiler/js_parser_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,21 @@ c
11: 0:},
12: 0://Provides: test(annot),
13: 0:c (identifier), 0:; (virtual), |}]

let%expect_test _ =
parse_print_token
~extra:true
{|
Event.prototype.initEvent = function _Event_initEvent(type, bubbles, cancelable) {
this.type = type
this.bubbles = bubbles
this.cancelable = cancelable
}
|};
[%expect
{|
2: 0:Event (identifier), 5:., 6:prototype (identifier), 15:., 16:initEvent (identifier), 26:=, 28:function, 37:_Event_initEvent (identifier), 53:(, 54:type, 58:,, 60:bubbles (identifier), 67:,, 69:cancelable (identifier), 79:), 81:{,
3: 4:this, 8:., 9:type, 14:=, 16:type, 0:; (virtual),
4: 4:this, 8:., 9:bubbles (identifier), 17:=, 19:bubbles (identifier), 0:; (virtual),
5: 4:this, 8:., 9:cancelable (identifier), 20:=, 22:cancelable (identifier), 0:; (virtual),
6: 0:}, 0:; (virtual), |}]