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

feat(#23): add resolve_type tests and fix wrong behaviour #40

Merged
merged 10 commits into from
Oct 27, 2024
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
jobs:
coverage:
name: Coverage
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: Maximize build space
uses: easimon/maximize-build-space@master
Expand Down
42 changes: 39 additions & 3 deletions crates/fervid_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,50 @@ impl TransformSfcContext {
bindings_helper.template_generation_mode = TemplateGenerationMode::Inline;
}

let scope = Rc::new(RefCell::from(TypeScope::new(options.filename.to_string())));

TransformSfcContext {
filename: options.filename.to_string(),
is_ce: false, // todo
bindings_helper,
scope,
deps: Default::default(),
scopes: vec![],
}
}

pub fn root_scope(&mut self) -> TypeScopeContainer {
if let Some(root_scope) = self.scopes.first() {
return root_scope.clone();
}

let root_scope = Rc::new(RefCell::new(TypeScope::new(0, self.filename.to_owned())));
self.scopes.push(root_scope.clone());

root_scope
}

pub fn create_child_scope(&mut self, parent_scope: &TypeScope) -> TypeScopeContainer {
let id = self.scopes.len();
let child_scope = Rc::new(RefCell::new(TypeScope {
id,
// We unfortunately have to copy here
filename: parent_scope.filename.to_owned(),
imports: parent_scope.imports.to_owned(),
types: parent_scope.types.to_owned(),
declares: parent_scope.declares.to_owned(),
is_generic_scope: false,
exported_types: Default::default(),
exported_declares: Default::default(),
}));
self.scopes.push(child_scope.clone());

child_scope
}

#[inline]
pub fn get_scope(&self, id: usize) -> Option<TypeScopeContainer> {
self.scopes.get(id).cloned()
}

pub fn get_scope_or_root(&mut self, id: usize) -> TypeScopeContainer {
self.get_scope(id).unwrap_or_else(|| self.root_scope())
}
}
4 changes: 2 additions & 2 deletions crates/fervid_transform/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ pub fn transform_and_record_scripts(
);
}

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

Expand Down
9 changes: 6 additions & 3 deletions crates/fervid_transform/src/script/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub fn register_import(
errors: &mut Vec<TransformError>,
) -> bool {
let mut binding_type = BindingTypes::Imported;
let mut should_include_binding = true;

let (local, imported, span) = match import_specifier {
// e.g. `import * as foo from 'mod.js'`
Expand Down Expand Up @@ -126,7 +127,9 @@ pub fn register_import(
imported_as.to_id(),
&mut bindings_helper.vue_resolved_imports,
);
return true;

// Do not include as a binding (is it a correct decision though?)
should_include_binding = false;
} else if is_dot_vue_import && imported_word == "default" {
// Only `import { default as Smth }` is supported.
// `import { default }` is invalid, and SWC will catch that
Expand Down Expand Up @@ -154,11 +157,11 @@ pub fn register_import(
return false;
}

if is_from_setup {
if is_from_setup && should_include_binding {
bindings_helper
.setup_bindings
.push(SetupBinding(local.to_owned(), BindingTypes::Imported))
} else {
} else if should_include_binding {
let bindings = bindings_helper
.options_api_bindings
.get_or_insert_with(|| Default::default());
Expand Down
Loading
Loading