Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit f6a714f

Browse files
escape LineTerminator in escape sequences template literal characters (#118)
Interprets escape sequences in template characters as specified in: https://tc39.es/ecma262/#prod-TemplateCharacter The "LineTerminator" is part of "NonEscapeCharacter" in a "EscapeSequence" If you want to use line breaks to make your source code easier to read, but you don’t want the line breaks to be part of the string’s value, write a backslash () at the end of those line.
1 parent bef5628 commit f6a714f

File tree

6 files changed

+12
-2
lines changed

6 files changed

+12
-2
lines changed

.depend

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ src/res_comments_table.cmx : src/res_parsetree_viewer.cmx src/res_doc.cmx \
1313
src/res_comment.cmx
1414
src/res_core.cmx : src/res_token.cmx src/res_scanner.cmx src/res_printer.cmx \
1515
src/res_parser.cmx src/res_js_ffi.cmx src/res_grammar.cmx src/res_doc.cmx \
16-
src/res_diagnostics.cmx src/res_comments_table.cmx src/res_core.cmi
16+
src/res_diagnostics.cmx src/res_comments_table.cmx \
17+
src/res_character_codes.cmx src/res_core.cmi
1718
src/res_core.cmi : src/res_parser.cmi
1819
src/res_diagnostics.cmx : src/res_token.cmx src/res_grammar.cmx \
1920
src/res_diagnostics_printing_utils.cmx src/res_diagnostics.cmi

src/res_character_codes.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,4 @@ let digitValue ch =
157157
else if Lower.a <= (lower ch) && (lower ch) <= Lower.f then
158158
(lower ch) - Lower.a + 10
159159
else
160-
16 (* larger than any legal value *)
160+
16 (* larger than any legal value *)

src/res_core.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,8 @@ let parseTemplateStringLiteral s =
838838
| '`' as c ->
839839
Buffer.add_char b c;
840840
loop (i + 2)
841+
| c when Res_character_codes.isLineBreak (Char.code c) ->
842+
loop (i + 2)
841843
| c ->
842844
Buffer.add_char b '\\';
843845
Buffer.add_char b c;

src/res_scanner.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ let scanTemplateLiteralToken scanner =
432432
if scanner.ch == CharacterCodes.backtick
433433
|| scanner.ch == CharacterCodes.backslash
434434
|| scanner.ch == CharacterCodes.dollar
435+
|| CharacterCodes.isLineBreak scanner.ch
435436
then next scanner;
436437
scan()
437438
) else (

tests/conversion/reason/__snapshots__/render.spec.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,9 @@ let x = 1
12761276
12771277
exports[`string.re 1`] = `
12781278
"%%bs.raw(\\"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})\\")
1279+
1280+
let x = \`This is a long string with a slash and line break \\\\\\\\
1281+
carriage return\`
12791282
"
12801283
`;
12811284

tests/conversion/reason/string.re

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
%bs.raw
22
"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})";
3+
4+
let x = {js|This is a long string with a slash and line break \
5+
carriage return|js}

0 commit comments

Comments
 (0)