Skip to content

Commit

Permalink
refactor(ast/estree): remove unused custom_ts_def (#9580)
Browse files Browse the repository at this point in the history
Following the suggestion #9574 (comment)
  • Loading branch information
hi-ogawa committed Mar 11, 2025
1 parent d303ba9 commit 1025b9f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
8 changes: 2 additions & 6 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> {
AttrPart::Tag("skip") => struct_def.estree.skip = true,
AttrPart::Tag("flatten") => struct_def.estree.flatten = true,
AttrPart::Tag("no_type") => struct_def.estree.no_type = true,
AttrPart::Tag("no_ts_def") => struct_def.estree.custom_ts_def = Some(String::new()),
AttrPart::Tag("no_ts_def") => struct_def.estree.no_ts_def = true,
AttrPart::List("add_fields", list) => {
for list_element in list {
let (name, value) = list_element.try_into_string()?;
Expand Down Expand Up @@ -114,9 +114,6 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> {
.collect::<Result<Vec<_>>>()?;
struct_def.estree.field_indices = Some(field_indices);
}
AttrPart::String("custom_ts_def", value) => {
struct_def.estree.custom_ts_def = Some(value);
}
AttrPart::String("ts_alias", value) => struct_def.estree.ts_alias = Some(value),
AttrPart::String("add_ts_def", value) => struct_def.estree.add_ts_def = Some(value),
AttrPart::String("rename", value) => struct_def.estree.rename = Some(value),
Expand All @@ -127,8 +124,7 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> {
AttrLocation::Enum(enum_def) => match part {
AttrPart::Tag("skip") => enum_def.estree.skip = true,
AttrPart::Tag("no_rename_variants") => enum_def.estree.no_rename_variants = true,
AttrPart::Tag("no_ts_def") => enum_def.estree.custom_ts_def = Some(String::new()),
AttrPart::String("custom_ts_def", value) => enum_def.estree.custom_ts_def = Some(value),
AttrPart::Tag("no_ts_def") => enum_def.estree.no_ts_def = true,
AttrPart::String("ts_alias", value) => enum_def.estree.ts_alias = Some(value),
AttrPart::String("add_ts_def", value) => {
enum_def.estree.add_ts_def = Some(value);
Expand Down
16 changes: 5 additions & 11 deletions tasks/ast_tools/src/generators/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,14 @@ impl Generator for TypescriptGenerator {
///
/// Push type defs to `code`.
fn generate_ts_type_def(type_def: &TypeDef, code: &mut String, schema: &Schema) {
// Use custom TS def if provided via `#[estree(custom_ts_def = "...")]` attribute
let custom_ts_def = match type_def {
TypeDef::Struct(struct_def) => &struct_def.estree.custom_ts_def,
TypeDef::Enum(enum_def) => &enum_def.estree.custom_ts_def,
// Skip TS def generation if `#[estree(no_ts_def)]` attribute
let no_ts_def = match type_def {
TypeDef::Struct(struct_def) => &struct_def.estree.no_ts_def,
TypeDef::Enum(enum_def) => &enum_def.estree.no_ts_def,
_ => unreachable!(),
};

if let Some(custom_ts_def) = custom_ts_def {
// Empty string means don't output any TS def at all for this type
if !custom_ts_def.is_empty() {
write_it!(code, "export {custom_ts_def};\n\n");
}
} else {
// No custom definition. Generate one.
if !no_ts_def {
let ts_def = match type_def {
TypeDef::Struct(struct_def) => generate_ts_type_def_for_struct(struct_def, schema),
TypeDef::Enum(enum_def) => generate_ts_type_def_for_enum(enum_def, schema),
Expand Down
10 changes: 4 additions & 6 deletions tasks/ast_tools/src/schema/extensions/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ pub struct ESTreeStruct {
/// e.g. `#[estree(ts_alias = "null")]` means this type won't have a type def generated,
/// and any struct / enum referencing it will substitute `null` as the type.
pub ts_alias: Option<String>,
/// Custom TS type definition. Does not include `export`.
/// Empty string if type should not have a TS type definition.
pub custom_ts_def: Option<String>,
/// Type should not have a TS type definition.
pub no_ts_def: bool,
/// Additional custom TS type definition to add along with the generated one.
/// Does not include `export`.
pub add_ts_def: Option<String>,
Expand All @@ -38,9 +37,8 @@ pub struct ESTreeEnum {
/// e.g. `#[estree(ts_alias = "null")]` means this type won't have a type def generated,
/// and any struct / enum referencing it will substitute `null` as the type.
pub ts_alias: Option<String>,
/// Custom TS type definition. Does not include `export`.
/// Empty string if type should not have a TS type definition.
pub custom_ts_def: Option<String>,
/// Type should not have a TS type definition.
pub no_ts_def: bool,
/// Additional custom TS type definition to add along with the generated one.
/// Does not include `export`.
pub add_ts_def: Option<String>,
Expand Down

0 comments on commit 1025b9f

Please sign in to comment.