Skip to content

Commit 8a6b38d

Browse files
committed
Report invalid template escapes after earlier diagnostics
Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 39b3368 commit 8a6b38d

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

compiler/syntax/src/res_core.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,9 +1055,8 @@ let parse_template_constant ~start_pos ~prefix (p : Parser.t) =
10551055
match String_literal.decode_js_template_escapes txt with
10561056
| Some semantic -> Ast_helper.Const.string semantic
10571057
| None ->
1058-
if p.diagnostics = [] then
1059-
Parser.err ~start_pos ~end_pos:p.prev_end_pos p
1060-
(Diagnostics.message "Invalid string escape sequence");
1058+
Parser.err ~start_pos ~end_pos:p.prev_end_pos p
1059+
(Diagnostics.message "Invalid string escape sequence");
10611060
Ast_helper.Const.string "")
10621061
| Some _ ->
10631062
Parser.err ~start_pos ~end_pos:p.prev_end_pos p

tests/ounit_tests/ounit_string_literal_tests.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ let assert_invalid_backquoted_pattern encoded =
2121
in
2222
OUnit.assert_bool "expected an invalid string escape" result.invalid
2323

24+
let assert_invalid_backquoted_pattern_after_diagnostic () =
25+
let source =
26+
{|
27+
let invalidBigint = 0x1n
28+
let f = value => switch value { | `\uD800` => 1 }
29+
|}
30+
in
31+
let result =
32+
Res_driver.parse_implementation_from_source ~for_printer:false
33+
~display_filename:"StringLiteralTest.res" ~source
34+
in
35+
OUnit.assert_equal ~printer:string_of_int 2 (List.length result.diagnostics)
36+
2437
let assert_invalid_tagged_template_pattern tag =
2538
let source =
2639
"let f = value => switch value { | " ^ tag ^ "`literal` => 1 }"
@@ -383,6 +396,8 @@ let suites =
383396
( "backquoted patterns reject lone surrogate escapes" >:: fun _ ->
384397
assert_invalid_backquoted_pattern {|\uD800|};
385398
assert_invalid_backquoted_pattern {|\uDC00|} );
399+
( "invalid backquoted pattern after an earlier diagnostic" >:: fun _ ->
400+
assert_invalid_backquoted_pattern_after_diagnostic () );
386401
( "character literals retain source and semantic forms" >:: fun _ ->
387402
assert_parsed_char ~for_printer:false ~source:{|\u{61}|}
388403
~expected_semantic:0x61;

0 commit comments

Comments
 (0)