From 36ece653f007ef6cd237d8f1b831fd4fda7b01bb Mon Sep 17 00:00:00 2001 From: Maxim Date: Tue, 24 May 2022 06:59:59 +0200 Subject: [PATCH] Transform "NonEscapeCharacter" in Ast_utf8_string correct. Fixes https://github.com/rescript-lang/syntax/issues/473 The [ecmascript spec](https://tc39.es/ecma262/#prod-CharacterEscapeSequence) states that both `SingleEscapeCharacter` and `NonEscapeCharacter` are valid escape sequences. Previously escape sequences containing a `NonEscapeCharacter`, any regular char like `a` in`"\a"`, would throw the "Invalid escape code" error. ReScript strings should have the same semantics as JS. --- jscomp/frontend/ast_utf8_string.ml | 8 +++++--- jscomp/ounit_tests/ounit_unicode_tests.ml | 3 +++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/jscomp/frontend/ast_utf8_string.ml b/jscomp/frontend/ast_utf8_string.ml index bf754ad89d..8d62246efd 100644 --- a/jscomp/frontend/ast_utf8_string.ml +++ b/jscomp/frontend/ast_utf8_string.ml @@ -25,7 +25,6 @@ type error = | Invalid_code_point | Unterminated_backslash - | Invalid_escape_code of char | Invalid_hex_escape | Invalid_unicode_escape | Invalid_unicode_codepoint_escape @@ -36,7 +35,6 @@ let pp_error fmt err = match err with | Invalid_code_point -> "Invalid code point" | Unterminated_backslash -> "\\ ended unexpectedly" - | Invalid_escape_code c -> "Invalid escape code: " ^ String.make 1 c | Invalid_hex_escape -> "Invalid \\x escape" | Invalid_unicode_escape -> "Invalid \\u escape" | Invalid_unicode_codepoint_escape -> @@ -109,7 +107,11 @@ and escape_code loc buf s offset s_len = | 'x' -> Buffer.add_char buf cur_char; two_hex (loc + 1) buf s (offset + 1) s_len - | _ -> error ~loc (Invalid_escape_code cur_char) + | _ -> + (* Regular characters, like `a` in `\a`, + * are valid escape sequences *) + Buffer.add_char buf cur_char; + check_and_transform (loc + 1) buf s (offset + 1) s_len and two_hex loc buf s offset s_len = if offset + 1 >= s_len then error ~loc Invalid_hex_escape; diff --git a/jscomp/ounit_tests/ounit_unicode_tests.ml b/jscomp/ounit_tests/ounit_unicode_tests.ml index 97edf17f88..65826252e5 100644 --- a/jscomp/ounit_tests/ounit_unicode_tests.ml +++ b/jscomp/ounit_tests/ounit_unicode_tests.ml @@ -47,6 +47,9 @@ let suites = Ast_utf8_string.transform_test "\\n" =~ "\\n" end; + __LOC__ >:: begin fun _ -> + Ast_utf8_string.transform_test {|\h\e\l\lo \"world\"!|} =~ {|\h\e\l\lo \"world\"!|} + end; __LOC__ >:: begin fun _ -> Ast_utf8_string.transform_test "\\u{1d306}" =~ "\\u{1d306}" end;