diff --git a/crates/fervid/benches/codegen_bench.rs b/crates/fervid/benches/codegen_bench.rs index 91ac838..fd0e17d 100644 --- a/crates/fervid/benches/codegen_bench.rs +++ b/crates/fervid/benches/codegen_bench.rs @@ -15,8 +15,8 @@ fn codegen_benchmark(c: &mut Criterion) { panic!("Test component has no template block"); }; - let mut scope_helper = fervid_transform::structs::ScopeHelper::default(); - fervid_transform::template::transform_and_record_template(template_block, &mut scope_helper); + let mut bindings_helper = fervid_transform::structs::BindingsHelper::default(); + fervid_transform::template::transform_and_record_template(template_block, &mut bindings_helper); b.iter_batched( || template_block.clone(), diff --git a/crates/fervid_codegen/src/imports/mod.rs b/crates/fervid_codegen/src/imports/mod.rs index 81817a1..b2ef00f 100644 --- a/crates/fervid_codegen/src/imports/mod.rs +++ b/crates/fervid_codegen/src/imports/mod.rs @@ -17,55 +17,14 @@ impl CodegenContext { self.used_imports |= vue_import; } - pub fn get_import_str(vue_import: VueImports) -> &'static str { - match vue_import { - VueImports::CreateBlock => "_createBlock", - VueImports::CreateCommentVNode => "_createCommentVNode", - VueImports::CreateElementBlock => "_createElementBlock", - VueImports::CreateElementVNode => "_createElementVNode", - VueImports::CreateTextVNode => "_createTextVNode", - VueImports::CreateVNode => "_createVNode", - VueImports::Fragment => "_Fragment", - VueImports::KeepAlive => "_KeepAlive", - VueImports::MergeModels => "_mergeModels", - VueImports::NormalizeClass => "_normalizeClass", - VueImports::NormalizeStyle => "_normalizeStyle", - VueImports::OpenBlock => "_openBlock", - VueImports::RenderList => "_renderList", - VueImports::RenderSlot => "_renderSlot", - VueImports::ResolveComponent => "_resolveComponent", - VueImports::ResolveDirective => "_resolveDirective", - VueImports::ResolveDynamicComponent => "_resolveDynamicComponent", - VueImports::Suspense => "_Suspense", - VueImports::Teleport => "_Teleport", - VueImports::ToDisplayString => "_toDisplayString", - VueImports::Transition => "_Transition", - VueImports::TransitionGroup => "_TransitionGroup", - VueImports::UseModel => "_useModel", - VueImports::VModelCheckbox => "_vModelCheckbox", - VueImports::VModelDynamic => "_vModelDynamic", - VueImports::VModelRadio => "_vModelRadio", - VueImports::VModelSelect => "_vModelSelect", - VueImports::VModelText => "_vModelText", - VueImports::VShow => "_vShow", - VueImports::WithCtx => "_withCtx", - VueImports::WithDirectives => "_withDirectives", - VueImports::WithModifiers => "_withModifiers", - } - } - - pub fn get_import_ident(vue_import: VueImports) -> JsWord { - JsWord::from(Self::get_import_str(vue_import)) - } - pub fn get_and_add_import_str(&mut self, vue_import: VueImports) -> &'static str { self.add_to_imports(vue_import); - Self::get_import_str(vue_import) + vue_import.as_str() } pub fn get_and_add_import_ident(&mut self, vue_import: VueImports) -> JsWord { self.add_to_imports(vue_import); - Self::get_import_ident(vue_import) + vue_import.as_atom() } /// Generates all the imports used by template generation. @@ -73,11 +32,11 @@ impl CodegenContext { pub fn generate_imports(&self) -> Vec { let mut result = Vec::new(); for import in self.used_imports.into_iter() { - let import_raw = Self::get_import_str(import); + let import_raw = import.as_str(); let import_local = Ident { span: DUMMY_SP, - sym: JsWord::from(import_raw), + sym: import.as_atom(), optional: false, }; diff --git a/crates/fervid_core/src/vue_imports.rs b/crates/fervid_core/src/vue_imports.rs index 222abe8..624116a 100644 --- a/crates/fervid_core/src/vue_imports.rs +++ b/crates/fervid_core/src/vue_imports.rs @@ -1,5 +1,7 @@ use flagset::{flags, FlagSet}; +use crate::FervidAtom; + flags! { // #[derive(Clone, Copy)] pub enum VueImports: u64 { @@ -25,6 +27,7 @@ flags! { ToDisplayString, Transition, TransitionGroup, + Unref, UseModel, VModelCheckbox, VModelDynamic, @@ -38,4 +41,50 @@ flags! { } } +impl VueImports { + #[inline] + pub fn as_str(self) -> &'static str { + match self { + VueImports::CreateBlock => "_createBlock", + VueImports::CreateCommentVNode => "_createCommentVNode", + VueImports::CreateElementBlock => "_createElementBlock", + VueImports::CreateElementVNode => "_createElementVNode", + VueImports::CreateTextVNode => "_createTextVNode", + VueImports::CreateVNode => "_createVNode", + VueImports::Fragment => "_Fragment", + VueImports::KeepAlive => "_KeepAlive", + VueImports::MergeModels => "_mergeModels", + VueImports::NormalizeClass => "_normalizeClass", + VueImports::NormalizeStyle => "_normalizeStyle", + VueImports::OpenBlock => "_openBlock", + VueImports::RenderList => "_renderList", + VueImports::RenderSlot => "_renderSlot", + VueImports::ResolveComponent => "_resolveComponent", + VueImports::ResolveDirective => "_resolveDirective", + VueImports::ResolveDynamicComponent => "_resolveDynamicComponent", + VueImports::Suspense => "_Suspense", + VueImports::Teleport => "_Teleport", + VueImports::ToDisplayString => "_toDisplayString", + VueImports::Transition => "_Transition", + VueImports::TransitionGroup => "_TransitionGroup", + VueImports::Unref => "_unref", + VueImports::UseModel => "_useModel", + VueImports::VModelCheckbox => "_vModelCheckbox", + VueImports::VModelDynamic => "_vModelDynamic", + VueImports::VModelRadio => "_vModelRadio", + VueImports::VModelSelect => "_vModelSelect", + VueImports::VModelText => "_vModelText", + VueImports::VShow => "_vShow", + VueImports::WithCtx => "_withCtx", + VueImports::WithDirectives => "_withDirectives", + VueImports::WithModifiers => "_withModifiers", + } + } + + #[inline] + pub fn as_atom(self) -> FervidAtom { + self.as_str().into() + } +} + pub type VueImportsSet = FlagSet; diff --git a/crates/fervid_transform/src/lib.rs b/crates/fervid_transform/src/lib.rs index 597739c..0edd9de 100644 --- a/crates/fervid_transform/src/lib.rs +++ b/crates/fervid_transform/src/lib.rs @@ -1,7 +1,7 @@ use fervid_core::{SfcDescriptor, SfcTemplateBlock, VueImportsSet, BindingTypes, FervidAtom, TemplateGenerationMode}; use fxhash::FxHashMap as HashMap; use script::transform_and_record_scripts; -use structs::ScopeHelper; +use structs::BindingsHelper; use swc_core::ecma::ast::{Module, ObjectLit, Function}; use template::transform_and_record_template; @@ -41,15 +41,15 @@ pub struct TransformSfcResult { pub fn transform_sfc(sfc_descriptor: SfcDescriptor) -> TransformSfcResult { let mut template_block = None; - let mut scope_helper = ScopeHelper::default(); + let mut bindings_helper = BindingsHelper::default(); let transform_result = transform_and_record_scripts( sfc_descriptor.script_setup, sfc_descriptor.script_legacy, - &mut scope_helper, + &mut bindings_helper, ); if let Some(mut template) = sfc_descriptor.template { - transform_and_record_template(&mut template, &mut scope_helper); + transform_and_record_template(&mut template, &mut bindings_helper); template_block = Some(template); } @@ -58,8 +58,8 @@ pub fn transform_sfc(sfc_descriptor: SfcDescriptor) -> TransformSfcResult { module: transform_result.module, setup_fn: transform_result.setup_fn, template_block, - template_generation_mode: scope_helper.template_generation_mode, - used_idents: scope_helper.used_idents, - used_vue_imports: transform_result.added_imports, + template_generation_mode: bindings_helper.template_generation_mode, + used_idents: bindings_helper.used_bindings, + used_vue_imports: bindings_helper.vue_imports, } } diff --git a/crates/fervid_transform/src/script.rs b/crates/fervid_transform/src/script.rs index 70cc92e..c9846cd 100644 --- a/crates/fervid_transform/src/script.rs +++ b/crates/fervid_transform/src/script.rs @@ -1,6 +1,6 @@ //! Responsible for `