fervid_transform/script/options_api/
setup.rs

1use fervid_core::BindingTypes;
2use swc_core::ecma::ast::{BlockStmt, Expr};
3
4use crate::{
5    script::utils::{collect_block_stmt_return_fields, collect_obj_fields, unroll_paren_seq},
6    OptionsApiBindings, SetupBinding,
7};
8
9/// Collects all the bindings from `setup`, e.g. `setup() { return { foo: 'bar', baz: 42 } }`
10///
11/// https://vuejs.org/api/composition-api-setup.html
12#[inline]
13pub fn collect_setup_bindings_block_stmt(
14    block_stmt: &BlockStmt,
15    options_api_bindings: &mut OptionsApiBindings,
16) {
17    // TODO Implement the algorithm
18    // But the current Vue SFC compiler is doing the same
19
20    let mut tmp = Vec::new();
21    collect_block_stmt_return_fields(block_stmt, &mut tmp);
22
23    options_api_bindings.setup.extend(
24        tmp.into_iter()
25            .map(|word| SetupBinding(word, BindingTypes::SetupMaybeRef)),
26    );
27}
28
29/// Collects all the bindings from `setup` expression, e.g. `setup: () => ({ foo: 'bar' })`
30///
31/// https://vuejs.org/api/composition-api-setup.html
32#[inline]
33pub fn collect_setup_bindings_expr(expr: &Expr, options_api_bindings: &mut OptionsApiBindings) {
34    let expr = unroll_paren_seq(expr);
35
36    // TODO The actual algorithm is much more complicated
37    // But the current Vue SFC compiler is doing the same
38
39    let Expr::Object(ref obj_lit) = *expr else {
40        return;
41    };
42
43    let mut tmp = Vec::new();
44    collect_obj_fields(obj_lit, &mut tmp);
45
46    options_api_bindings.setup.extend(
47        tmp.into_iter()
48            .map(|word| SetupBinding(word, BindingTypes::SetupMaybeRef)),
49    );
50}