Skip to content

Commit

Permalink
Implement template literal generation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
avp authored and facebook-github-bot committed Sep 2, 2021
1 parent b36023f commit 6e58687
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
42 changes: 26 additions & 16 deletions unsupported/juno/src/gen_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,23 +881,33 @@ impl<W: Write> GenJS<W> {
}

TemplateLiteral {
quasis: _,
expressions: _,
quasis,
expressions,
} => {
unimplemented!("TemplateLiteral");
// out!(self, "`");
// let mut it_expr = expressions.iter();
// for quasi in quasis {
// if let TemplateElement { raw, .. } = &quasi.kind {
// out!(self, "{}", raw.str);
// if let Some(expr) = it_expr.next() {
// out!(self, "${{");
// expr.visit(self, Some(node));
// out!(self, "}}");
// }
// }
// }
// out!(self, "`");
out!(self, "`");
let mut it_expr = expressions.iter();
for quasi in quasis {
if let TemplateElement {
raw,
tail: _,
cooked: _,
} = &quasi.kind
{
out!(
self,
"{}",
char::decode_utf16(raw.str.iter().cloned())
.map(|r| r.expect("Template element raw must be valid UTF-16"))
.collect::<String>()
);
if let Some(expr) = it_expr.next() {
out!(self, "${{");
expr.visit(self, Some(node));
out!(self, "}}");
}
}
}
out!(self, "`");
}
TaggedTemplateExpression { tag, quasi } => {
self.print_child(Some(tag), node, ChildPos::Left);
Expand Down
12 changes: 12 additions & 0 deletions unsupported/juno/tests/gen_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ fn test_literals() {
test_roundtrip("/abc/");
test_roundtrip("/abc/gi");
test_roundtrip("/abc/gi");

test_roundtrip(r#" `abc` "#);
test_roundtrip(r#" `abc\ndef` "#);
test_roundtrip(
r#" `abc
def` "#,
);
test_roundtrip(r#" `abc \ud800 def` "#);
test_roundtrip(r#" `abc \ud800 def` "#);
test_roundtrip(r#" `\ud83d\udcd5` "#);
test_roundtrip(r#" `escape backtick: \` should work` "#);
test_roundtrip(r#" `😹` "#);
}

#[test]
Expand Down

0 comments on commit 6e58687

Please sign in to comment.