Skip to content

Commit 6e58687

Browse files
avpfacebook-github-bot
authored andcommitted
Implement template literal generation
Summary: Use the `raw` property that template literals are required to keep around by the spec to print out template literals. Reviewed By: tmikov Differential Revision: D30196556 fbshipit-source-id: 26f4d76c4db87f58e52733d5f0ce711bc2408772
1 parent b36023f commit 6e58687

2 files changed

Lines changed: 38 additions & 16 deletions

File tree

unsupported/juno/src/gen_js.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -881,23 +881,33 @@ impl<W: Write> GenJS<W> {
881881
}
882882

883883
TemplateLiteral {
884-
quasis: _,
885-
expressions: _,
884+
quasis,
885+
expressions,
886886
} => {
887-
unimplemented!("TemplateLiteral");
888-
// out!(self, "`");
889-
// let mut it_expr = expressions.iter();
890-
// for quasi in quasis {
891-
// if let TemplateElement { raw, .. } = &quasi.kind {
892-
// out!(self, "{}", raw.str);
893-
// if let Some(expr) = it_expr.next() {
894-
// out!(self, "${{");
895-
// expr.visit(self, Some(node));
896-
// out!(self, "}}");
897-
// }
898-
// }
899-
// }
900-
// out!(self, "`");
887+
out!(self, "`");
888+
let mut it_expr = expressions.iter();
889+
for quasi in quasis {
890+
if let TemplateElement {
891+
raw,
892+
tail: _,
893+
cooked: _,
894+
} = &quasi.kind
895+
{
896+
out!(
897+
self,
898+
"{}",
899+
char::decode_utf16(raw.str.iter().cloned())
900+
.map(|r| r.expect("Template element raw must be valid UTF-16"))
901+
.collect::<String>()
902+
);
903+
if let Some(expr) = it_expr.next() {
904+
out!(self, "${{");
905+
expr.visit(self, Some(node));
906+
out!(self, "}}");
907+
}
908+
}
909+
}
910+
out!(self, "`");
901911
}
902912
TaggedTemplateExpression { tag, quasi } => {
903913
self.print_child(Some(tag), node, ChildPos::Left);

unsupported/juno/tests/gen_js.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ fn test_literals() {
8787
test_roundtrip("/abc/");
8888
test_roundtrip("/abc/gi");
8989
test_roundtrip("/abc/gi");
90+
91+
test_roundtrip(r#" `abc` "#);
92+
test_roundtrip(r#" `abc\ndef` "#);
93+
test_roundtrip(
94+
r#" `abc
95+
def` "#,
96+
);
97+
test_roundtrip(r#" `abc \ud800 def` "#);
98+
test_roundtrip(r#" `abc \ud800 def` "#);
99+
test_roundtrip(r#" `\ud83d\udcd5` "#);
100+
test_roundtrip(r#" `escape backtick: \` should work` "#);
101+
test_roundtrip(r#" `😹` "#);
90102
}
91103

92104
#[test]

0 commit comments

Comments
 (0)