From b0b746601af12dd83ed96b998588ec39af4df44b Mon Sep 17 00:00:00 2001 From: Marsel Shaikhin Date: Wed, 22 Nov 2023 13:44:18 +0100 Subject: [PATCH] enh: add span to Interpolation --- crates/fervid/src/parser/core.rs | 1 + crates/fervid_codegen/src/components/mod.rs | 1 + crates/fervid_codegen/src/elements/mod.rs | 2 ++ .../fervid_codegen/src/interpolation/mod.rs | 32 ++----------------- crates/fervid_core/src/structs.rs | 1 + crates/fervid_parser/src/template.rs | 1 + 6 files changed, 9 insertions(+), 29 deletions(-) diff --git a/crates/fervid/src/parser/core.rs b/crates/fervid/src/parser/core.rs index c95bac0..13a5f4f 100644 --- a/crates/fervid/src/parser/core.rs +++ b/crates/fervid/src/parser/core.rs @@ -285,6 +285,7 @@ fn parse_interpolation_node(input: &str) -> IResult<&str, Node> { value: parsed, template_scope: 0, patch_flag: false, + span: DUMMY_SP }), )) } diff --git a/crates/fervid_codegen/src/components/mod.rs b/crates/fervid_codegen/src/components/mod.rs index 8ec91fd..f19e1c6 100644 --- a/crates/fervid_codegen/src/components/mod.rs +++ b/crates/fervid_codegen/src/components/mod.rs @@ -885,6 +885,7 @@ mod tests { value: js("one"), template_scope: 0, patch_flag: true, + span: DUMMY_SP }), ], template_scope: 0, diff --git a/crates/fervid_codegen/src/elements/mod.rs b/crates/fervid_codegen/src/elements/mod.rs index 271339c..7d8e8ef 100644 --- a/crates/fervid_codegen/src/elements/mod.rs +++ b/crates/fervid_codegen/src/elements/mod.rs @@ -528,6 +528,7 @@ mod tests { value: js("true"), template_scope: 0, patch_flag: false, + span: DUMMY_SP }), Node::Text(" bye!".into(), DUMMY_SP), ], @@ -557,6 +558,7 @@ mod tests { value: js("true"), template_scope: 0, patch_flag: false, + span: DUMMY_SP }), Node::Element(ElementNode { starting_tag: StartingTag { diff --git a/crates/fervid_codegen/src/interpolation/mod.rs b/crates/fervid_codegen/src/interpolation/mod.rs index 6847cf7..fbbadfb 100644 --- a/crates/fervid_codegen/src/interpolation/mod.rs +++ b/crates/fervid_codegen/src/interpolation/mod.rs @@ -1,37 +1,11 @@ use fervid_core::{Interpolation, VueImports}; -use swc_core::{ - common::DUMMY_SP, - ecma::ast::{CallExpr, Callee, Expr, ExprOrSpread, Ident}, -}; +use swc_core::ecma::ast::{CallExpr, Callee, Expr, ExprOrSpread, Ident}; use crate::context::CodegenContext; impl CodegenContext { - pub fn generate_interpolation( - &mut self, - interpolation: &Interpolation - ) -> Expr { - // This is using a string with value if transformation failed - // let (transformed, has_js_bindings) = - // // Polyfill - // parse_js(value) - // .and_then(|mut expr| { - // let has_js = transform_scoped(&mut expr, &self.scope_helper, scope_to_use); - // Ok((expr, has_js)) - // }) - // .unwrap_or_else(|_| { - // ( - // Box::new(Expr::Lit(Lit::Str(Str { - // span, - // value: FervidAtom::from(value), - // raw: None, - // }))), - // false, - // ) - // }); - - // TODO Span - let span = DUMMY_SP; + pub fn generate_interpolation(&mut self, interpolation: &Interpolation) -> Expr { + let span = interpolation.span; // toDisplayString(transformed) Expr::Call(CallExpr { diff --git a/crates/fervid_core/src/structs.rs b/crates/fervid_core/src/structs.rs index 475ab20..424da62 100644 --- a/crates/fervid_core/src/structs.rs +++ b/crates/fervid_core/src/structs.rs @@ -103,6 +103,7 @@ pub struct Interpolation { pub value: Box, pub template_scope: u32, pub patch_flag: bool, + pub span: Span } /// Starting tag represents [`ElementNode`]'s tag name and attributes diff --git a/crates/fervid_parser/src/template.rs b/crates/fervid_parser/src/template.rs index ee5a08d..ea819cb 100644 --- a/crates/fervid_parser/src/template.rs +++ b/crates/fervid_parser/src/template.rs @@ -163,6 +163,7 @@ impl SfcParser<'_, '_, '_> { value: parsed_interpolation, template_scope: 0, patch_flag: false, + span: interpolation_span })), Err(expr_err) => self.errors.push(expr_err.into()), }