Skip to content

Commit 36ece65

Browse files
author
Maxim
committed
Transform "NonEscapeCharacter" in Ast_utf8_string correct.
Fixes rescript-lang/syntax#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.
1 parent 4fb1a44 commit 36ece65

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

jscomp/frontend/ast_utf8_string.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
type error =
2626
| Invalid_code_point
2727
| Unterminated_backslash
28-
| Invalid_escape_code of char
2928
| Invalid_hex_escape
3029
| Invalid_unicode_escape
3130
| Invalid_unicode_codepoint_escape
@@ -36,7 +35,6 @@ let pp_error fmt err =
3635
match err with
3736
| Invalid_code_point -> "Invalid code point"
3837
| Unterminated_backslash -> "\\ ended unexpectedly"
39-
| Invalid_escape_code c -> "Invalid escape code: " ^ String.make 1 c
4038
| Invalid_hex_escape -> "Invalid \\x escape"
4139
| Invalid_unicode_escape -> "Invalid \\u escape"
4240
| Invalid_unicode_codepoint_escape ->
@@ -109,7 +107,11 @@ and escape_code loc buf s offset s_len =
109107
| 'x' ->
110108
Buffer.add_char buf cur_char;
111109
two_hex (loc + 1) buf s (offset + 1) s_len
112-
| _ -> error ~loc (Invalid_escape_code cur_char)
110+
| _ ->
111+
(* Regular characters, like `a` in `\a`,
112+
* are valid escape sequences *)
113+
Buffer.add_char buf cur_char;
114+
check_and_transform (loc + 1) buf s (offset + 1) s_len
113115

114116
and two_hex loc buf s offset s_len =
115117
if offset + 1 >= s_len then error ~loc Invalid_hex_escape;

jscomp/ounit_tests/ounit_unicode_tests.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ let suites =
4747
Ast_utf8_string.transform_test
4848
"\\n" =~ "\\n"
4949
end;
50+
__LOC__ >:: begin fun _ ->
51+
Ast_utf8_string.transform_test {|\h\e\l\lo \"world\"!|} =~ {|\h\e\l\lo \"world\"!|}
52+
end;
5053
__LOC__ >:: begin fun _ ->
5154
Ast_utf8_string.transform_test "\\u{1d306}" =~ "\\u{1d306}"
5255
end;

0 commit comments

Comments
 (0)