Skip to content

Commit

Permalink
fix: support more ExtractPropType tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix-ru committed Oct 26, 2024
1 parent 43c3511 commit 8583b7e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
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
98 changes: 48 additions & 50 deletions crates/fervid_transform/src/script/resolve_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3734,33 +3734,32 @@ mod tests {
);
}

// TODO How to support this?
// #[test]
// fn extract_prop_types_element_plus() {
// let resolved = resolve(
// "
// import { ExtractPropTypes } from 'vue'
// declare const props: {
// foo: StringConstructor,
// bar: {
// type: import('foo').EpPropFinalized<BooleanConstructor>,
// required: true
// }
// }
// type Props = ExtractPropTypes<typeof props>
// defineProps<Props>()",
// );
#[test]
fn extract_prop_types_element_plus() {
let resolved = resolve(
"
import { ExtractPropTypes } from 'vue'
declare const props: {
foo: StringConstructor,
bar: {
type: import('foo').EpPropFinalized<BooleanConstructor>,
required: true
}
}
type Props = ExtractPropTypes<typeof props>
defineProps<Props>()",
);

// assert_eq!(resolved.props.len(), 2);
// assert_eq!(
// resolved.props.get(&fervid_atom!("foo")),
// Some(&FlagSet::from(Types::String))
// );
// assert_eq!(
// resolved.props.get(&fervid_atom!("bar")),
// Some(&FlagSet::from(Types::Boolean))
// );
// }
assert_eq!(resolved.props.len(), 2);
assert_eq!(
resolved.props.get(&fervid_atom!("foo")),
Some(&FlagSet::from(Types::String))
);
assert_eq!(
resolved.props.get(&fervid_atom!("bar")),
Some(&FlagSet::from(Types::Boolean))
);
}

#[test]
fn extract_prop_types_antd() {
Expand All @@ -3785,31 +3784,30 @@ mod tests {
);
}

// TODO Support
// #[test]
// fn correctly_parse_type_annotation_for_declared_function() {
// let resolved = resolve(
// "
// import { ExtractPropTypes } from 'vue'
// interface UploadFile<T = any> {
// xhr?: T
// }
// declare function uploadProps<T = any>(): {
// fileList: {
// type: PropType<UploadFile<T>[]>
// default: UploadFile<T>[]
// }
// }
// type UploadProps = ExtractPropTypes<ReturnType<typeof uploadProps>>
// defineProps<UploadProps>()",
// );
#[test]
fn correctly_parse_type_annotation_for_declared_function() {
let resolved = resolve(
"
import { ExtractPropTypes } from 'vue'
interface UploadFile<T = any> {
xhr?: T
}
declare function uploadProps<T = any>(): {
fileList: {
type: PropType<UploadFile<T>[]>
default: UploadFile<T>[]
}
}
type UploadProps = ExtractPropTypes<ReturnType<typeof uploadProps>>
defineProps<UploadProps>()",
);

// assert_eq!(resolved.props.len(), 1);
// assert_eq!(
// resolved.props.get(&fervid_atom!("fileList")),
// Some(&FlagSet::from(Types::Array))
// );
// }
assert_eq!(resolved.props.len(), 1);
assert_eq!(
resolved.props.get(&fervid_atom!("fileList")),
Some(&FlagSet::from(Types::Array))
);
}

// TODO Other types
// TODO Remove all dbg!
Expand Down

0 comments on commit 8583b7e

Please sign in to comment.