From 6e5868763b08eb80f4164a926abb92fa5c71117f Mon Sep 17 00:00:00 2001 From: Aakash Patel Date: Thu, 2 Sep 2021 13:09:21 -0700 Subject: [PATCH] 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 --- unsupported/juno/src/gen_js.rs | 42 ++++++++++++++++++++------------ unsupported/juno/tests/gen_js.rs | 12 +++++++++ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/unsupported/juno/src/gen_js.rs b/unsupported/juno/src/gen_js.rs index 4afac2a97f3..373e624817b 100644 --- a/unsupported/juno/src/gen_js.rs +++ b/unsupported/juno/src/gen_js.rs @@ -881,23 +881,33 @@ impl GenJS { } 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::() + ); + 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); diff --git a/unsupported/juno/tests/gen_js.rs b/unsupported/juno/tests/gen_js.rs index 10e18daf0d2..888938239aa 100644 --- a/unsupported/juno/tests/gen_js.rs +++ b/unsupported/juno/tests/gen_js.rs @@ -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]