Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec(#46): definePropsDestructure #61

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 51 additions & 39 deletions crates/fervid_transform/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use swc_core::{
ecma::ast::{Function, Module, ObjectLit},
};

use crate::{
error::TransformError, structs::TransformScriptsResult, TransformSfcContext,
};
use crate::{error::TransformError, structs::TransformScriptsResult, TransformSfcContext};

use self::{
imports::process_imports,
Expand Down Expand Up @@ -51,44 +49,10 @@ pub fn transform_and_record_scripts(
// Official compiler does lazy type recording using the source AST,
// but we are modifying the source AST and thus cannot use it at a later stage.
// Therefore, types are eagerly recorded.

// 1.1. Imports in `<script>`
if let Some(ref mut script_options) = script_options {
process_imports(
&mut script_options.content,
&mut ctx.bindings_helper,
false,
errors,
);
}

// 1.2. Imports in `<script setup>`
if let Some(ref mut script_setup) = script_setup {
process_imports(
&mut script_setup.content,
&mut ctx.bindings_helper,
true,
errors,
);
}

// 1.3. Record types to support type-only `defineProps` and `defineEmits`
if ctx.bindings_helper.is_ts {
let scope = ctx.root_scope();
let mut scope = (*scope).borrow_mut();
scope.imports = ctx.bindings_helper.user_imports.clone();

record_types(
ctx,
script_setup.as_mut(),
script_options.as_mut(),
&mut scope,
false,
);
}
pretransform(ctx, script_setup.as_mut(), script_options.as_mut(), errors);

//
// STEP 1: Transform Options API `<script>`.
// STEP 2: Transform Options API `<script>`.
//
let mut script_module: Option<Box<Module>> = None;
let mut script_default_export: Option<ObjectLit> = None;
Expand Down Expand Up @@ -158,6 +122,54 @@ pub fn transform_and_record_scripts(
}
}

/// Records the imports and collects information for macro transforms,
/// such as types, variables for destructure, etc.
fn pretransform(
ctx: &mut TransformSfcContext,
mut script_setup: Option<&mut SfcScriptBlock>,
mut script_options: Option<&mut SfcScriptBlock>,
errors: &mut Vec<TransformError>,
) {
// Imports in `<script>`
if let Some(ref mut script_options) = script_options {
process_imports(
&mut script_options.content,
&mut ctx.bindings_helper,
false,
errors,
);
}

let mut has_type_only_macros = false;
let has_define_props_destructure = false;

// Imports in `<script setup>`
if let Some(ref mut script_setup) = script_setup {
process_imports(
&mut script_setup.content,
&mut ctx.bindings_helper,
true,
errors,
);

// Walk the `<script setup>` to find macro usages
// TODO Think of a way to apply multiple different AST transformations
// Can it be done

// TODO Remove it and actually find the macros
has_type_only_macros = ctx.bindings_helper.is_ts;
}

// 1.3. Record types to support type-only `defineProps` and `defineEmits`
if has_type_only_macros {
let scope = ctx.root_scope();
let mut scope = (*scope).borrow_mut();
scope.imports = ctx.bindings_helper.user_imports.clone();

record_types(ctx, script_setup, script_options, &mut scope, false);
}
}

#[cfg(test)]
mod tests {
use swc_core::common::{sync::Lrc, SourceMap, Span};
Expand Down
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@
"keyof",
"xfoo",
"xbar",
"vuejs"
"vuejs",
"pretransform"
],
"ignorePaths": [
".github/**",
Expand Down