Skip to content

Commit

Permalink
#[ts(type)] overrides #[ts(optional_fields)]
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-shigueo committed Dec 2, 2024
1 parent faa431f commit 53941b5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
41 changes: 29 additions & 12 deletions macros/src/types/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ fn format_field(
return Ok(());
}

if let Some(ref type_override) = field_attr.type_override {
let field_name = to_ts_ident(field.ident.as_ref().unwrap());
let name = match (field_attr.rename.as_ref(), rename_all) {
(Some(rn), _) => rn.to_owned(),
(None, Some(rn)) => rn.apply(&field_name),
(None, None) => field_name,
};
let valid_name = raw_name_to_ts_field(name);

// Start every doc string with a newline, because when other characters are in front, it is not "understood" by VSCode
let docs = match field_attr.docs.is_empty() {
true => "".to_string(),
false => format!("\n{}", &field_attr.docs),
};

formatted_fields.push(quote! {
format!("{}{}: {},", #docs, #valid_name, #type_override)
});

return Ok(());
}

let ty = field_attr.type_as(&field.ty);

let (optional_annotation, nullable) = match (struct_optional, field_attr.optional) {
Expand Down Expand Up @@ -139,18 +161,13 @@ fn format_field(
return Ok(());
}

let formatted_ty = field_attr
.type_override
.map(|t| quote!(#t))
.unwrap_or_else(|| {
if field_attr.inline {
dependencies.append_from(&ty);
quote!(<#ty as #crate_rename::TS>::inline())
} else {
dependencies.push(&ty);
quote!(<#ty as #crate_rename::TS>::name())
}
});
let formatted_ty = if field_attr.inline {
dependencies.append_from(&ty);
quote!(<#ty as #crate_rename::TS>::inline())
} else {
dependencies.push(&ty);
quote!(<#ty as #crate_rename::TS>::name())
};

let field_name = to_ts_ident(field.ident.as_ref().unwrap());
let name = match (field_attr.rename, rename_all) {
Expand Down
16 changes: 14 additions & 2 deletions ts-rs/tests/integration/optional_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,20 @@ struct OptionalStruct {

e: Foo,
f: Bar<i32>,

#[ts(type = "string")]
g: Option<i32>,

#[ts(as = "String")]
h: Option<i32>,
}

#[test]
fn struct_optional() {
assert_eq!(
OptionalStruct::inline(),
format!(
"{{ a?: number, b?: number, c?: number | null, d: number, e?: number, f?: number, }}"
"{{ a?: number, b?: number, c?: number | null, d: number, e?: number, f?: number, g: string, h: string, }}"
)
)
}
Expand All @@ -129,14 +135,20 @@ struct NullableStruct {

e: Foo,
f: Bar<i32>,

#[ts(type = "string")]
g: Option<i32>,

#[ts(as = "String")]
h: Option<i32>,
}

#[test]
fn struct_nullable() {
assert_eq!(
NullableStruct::inline(),
format!(
"{{ a?: number | null, b?: number | null, c?: number | null, d: number, e?: number | null, f?: number | null, }}"
"{{ a?: number | null, b?: number | null, c?: number | null, d: number, e?: number | null, f?: number | null, g: string, h: string, }}"
)
)
}

0 comments on commit 53941b5

Please sign in to comment.