use fervid_core::{ElementNode, VueImports};
use swc_core::ecma::ast::{ArrayLit, Expr, ExprOrSpread, Ident};
use crate::CodegenContext;
impl CodegenContext {
pub fn generate_teleport(&mut self, element_node: &ElementNode) -> Expr {
let span = element_node.span;
let teleport_identifier = Expr::Ident(Ident {
span,
sym: self.get_and_add_import_ident(VueImports::Teleport),
optional: false,
});
let teleport_attrs =
self.generate_builtin_attrs(&element_node.starting_tag.attributes, span);
let generated_children = self.generate_element_children(element_node, false);
let teleport_children = if generated_children.0.len() != 0 {
Some(Expr::Array(ArrayLit {
span,
elems: generated_children
.0
.into_iter()
.map(|c| {
Some(ExprOrSpread {
spread: None,
expr: Box::new(c),
})
})
.collect(),
}))
} else {
None
};
self.generate_componentlike(
teleport_identifier,
teleport_attrs,
teleport_children,
&element_node.patch_hints,
true,
span,
)
}
}
#[cfg(test)]
mod tests {
use fervid_core::{BuiltinType, ElementKind, Node, StartingTag};
use swc_core::common::DUMMY_SP;
use crate::test_utils::{regular_attribute, v_bind_attribute};
use super::*;
#[test]
fn it_generates_empty_teleport() {
test_out(
ElementNode {
kind: ElementKind::Builtin(BuiltinType::Teleport),
starting_tag: StartingTag {
tag_name: "teleport".into(),
attributes: vec![],
directives: None,
},
children: vec![],
template_scope: 0,
patch_hints: Default::default(),
span: DUMMY_SP,
},
r#"(_openBlock(),_createBlock(_Teleport))"#,
)
}
#[test]
fn it_generates_teleport_attrs() {
test_out(
ElementNode {
kind: ElementKind::Builtin(BuiltinType::Teleport),
starting_tag: StartingTag {
tag_name: "teleport".into(),
attributes: vec![
regular_attribute("foo", "bar"),
v_bind_attribute("baz", "qux"),
],
directives: None,
},
children: vec![],
template_scope: 0,
patch_hints: Default::default(),
span: DUMMY_SP,
},
r#"(_openBlock(),_createBlock(_Teleport,{foo:"bar",baz:qux}))"#,
)
}
#[test]
fn it_generates_teleport_children() {
test_out(
ElementNode {
kind: ElementKind::Builtin(BuiltinType::Teleport),
starting_tag: StartingTag {
tag_name: "teleport".into(),
attributes: vec![],
directives: None,
},
children: vec![Node::Text("foobar".into(), DUMMY_SP)],
template_scope: 0,
patch_hints: Default::default(),
span: DUMMY_SP,
},
r#"(_openBlock(),_createBlock(_Teleport,null,[_createTextVNode("foobar")]))"#,
)
}
#[test]
fn it_generates_full_teleport() {
test_out(
ElementNode {
kind: ElementKind::Builtin(BuiltinType::Teleport),
starting_tag: StartingTag {
tag_name: "teleport".into(),
attributes: vec![
regular_attribute("foo", "bar"),
v_bind_attribute("baz", "qux"),
],
directives: None,
},
children: vec![Node::Text("foobar".into(), DUMMY_SP)],
template_scope: 0,
patch_hints: Default::default(),
span: DUMMY_SP,
},
r#"(_openBlock(),_createBlock(_Teleport,{foo:"bar",baz:qux},[_createTextVNode("foobar")]))"#,
)
}
fn test_out(input: ElementNode, expected: &str) {
let mut ctx = CodegenContext::default();
let out = ctx.generate_teleport(&input);
assert_eq!(crate::test_utils::to_str(out), expected)
}
}