From aea81d3cbfff775f01c30902575c9c1d464f3961 Mon Sep 17 00:00:00 2001 From: Marsel Shaikhin Date: Fri, 8 Nov 2024 09:15:45 +0100 Subject: [PATCH] enh: move `define_emits` and `define_model` to own files, support testing binding types --- crates/fervid/src/lib.rs | 6 +- .../fervid_napi/__tests__/defineEmits.spec.ts | 13 +- crates/fervid_napi/__tests__/utils.ts | 5 +- crates/fervid_napi/index.d.ts | 50 +++ crates/fervid_napi/index.js | 3 +- crates/fervid_napi/src/lib.rs | 70 ++-- crates/fervid_napi/src/structs.rs | 76 +++- crates/fervid_transform/src/script/setup.rs | 6 + .../src/script/setup/define_emits.rs | 192 ++++++++++ .../src/script/setup/define_model.rs | 176 +++++++++ .../src/script/setup/macros.rs | 357 ++---------------- 11 files changed, 596 insertions(+), 358 deletions(-) create mode 100644 crates/fervid_transform/src/script/setup/define_emits.rs create mode 100644 crates/fervid_transform/src/script/setup/define_model.rs diff --git a/crates/fervid/src/lib.rs b/crates/fervid/src/lib.rs index f1b388c..ef6bf27 100644 --- a/crates/fervid/src/lib.rs +++ b/crates/fervid/src/lib.rs @@ -57,7 +57,9 @@ use errors::CompileError; use fervid_codegen::CodegenContext; pub use fervid_core::*; use fervid_parser::SfcParser; -use fervid_transform::{style::should_transform_style_block, transform_sfc, TransformSfcOptions}; +use fervid_transform::{ + style::should_transform_style_block, transform_sfc, SetupBinding, TransformSfcOptions, +}; use fxhash::FxHasher32; use std::{ borrow::Cow, @@ -105,6 +107,7 @@ pub struct CompileResult { pub styles: Vec, pub other_assets: Vec, pub source_map: Option, + pub setup_bindings: Vec, } pub struct CompileEmittedStyle { @@ -209,6 +212,7 @@ pub fn compile(source: &str, options: CompileOptions) -> Result { test('basic usage', () => { - const { content } = compile(` + const { content, bindings } = compile(` - `) + `, { outputSetupBindings: true }) + assertCode(content) - // expect(bindings).toStrictEqual({ - // myEmit: BindingTypes.SETUP_CONST, - // }) + expect(bindings).toStrictEqual({ + myEmit: BindingTypes.SetupConst, + }) // should remove defineEmits import and call expect(content).not.toMatch('defineEmits') diff --git a/crates/fervid_napi/__tests__/utils.ts b/crates/fervid_napi/__tests__/utils.ts index dfecbdf..0666b8a 100644 --- a/crates/fervid_napi/__tests__/utils.ts +++ b/crates/fervid_napi/__tests__/utils.ts @@ -18,10 +18,11 @@ export function compile(src: string, options?: Partial, lo if (result.errors.length && logErrors) { console.warn(result.errors[0]) } - + return { content: result.code, - errors: result.errors + errors: result.errors, + bindings: result.setupBindings, } } diff --git a/crates/fervid_napi/index.d.ts b/crates/fervid_napi/index.d.ts index b7247b3..d258282 100644 --- a/crates/fervid_napi/index.d.ts +++ b/crates/fervid_napi/index.d.ts @@ -57,6 +57,8 @@ export interface FervidCompileOptions { filename: string /** Generate a const instead of default export */ genDefaultAs?: string + /** Whether setup bindings need to be serialized */ + outputSetupBindings?: boolean } export interface CompileResult { code: string @@ -64,6 +66,7 @@ export interface CompileResult { errors: Array customBlocks: Array sourceMap?: string + setupBindings?: Record | undefined } export interface Style { code: string @@ -82,6 +85,53 @@ export interface SerializedError { hi: number message: string } +/** + * This is a copied enum from `fervid_core` with `napi` implementation to avoid littering the core crate. + * + * The type of a binding (or identifier) which is used to show where this binding came from, + * e.g. `Data` is for Options API `data()`, `SetupRef` if for `ref`s and `computed`s in Composition API. + * + * + */ +export const enum BindingTypes { + /** returned from data() */ + Data = 0, + /** declared as a prop */ + Props = 1, + /** + * a local alias of a `