diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index bc570be88d6ad..303b6a95a456f 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -281,7 +281,6 @@ pub struct ThisExpression { #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)] pub struct ArrayExpression<'a> { pub span: Span, - #[estree(ts_type = "Array")] pub elements: Vec<'a, ArrayExpressionElement<'a>>, /// Array trailing comma /// @@ -298,7 +297,6 @@ inherit_variants! { #[ast(visit)] #[derive(Debug)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)] -#[estree(no_ts_def)] pub enum ArrayExpressionElement<'a> { /// `...[3, 4]` in `const array = [1, 2, ...[3, 4], null];` SpreadElement(Box<'a, SpreadElement<'a>>) = 64, @@ -318,7 +316,8 @@ pub enum ArrayExpressionElement<'a> { /// Serialized as `null` in JSON AST. See `serialize.rs`. #[ast(visit)] #[derive(Debug, Clone)] -#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq)] +#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)] +#[estree(custom_serialize, ts_alias = "null")] pub struct Elision { pub span: Span, } diff --git a/npm/oxc-types/types.d.ts b/npm/oxc-types/types.d.ts index 67d2436a22bd0..69a8ed6d7fb46 100644 --- a/npm/oxc-types/types.d.ts +++ b/npm/oxc-types/types.d.ts @@ -77,9 +77,11 @@ export interface ThisExpression extends Span { export interface ArrayExpression extends Span { type: 'ArrayExpression'; - elements: Array; + elements: Array; } +export type ArrayExpressionElement = SpreadElement | null | Expression; + export interface ObjectExpression extends Span { type: 'ObjectExpression'; properties: Array; diff --git a/tasks/ast_tools/src/derives/estree.rs b/tasks/ast_tools/src/derives/estree.rs index 9c46a449430a5..3aca3f9730f0e 100644 --- a/tasks/ast_tools/src/derives/estree.rs +++ b/tasks/ast_tools/src/derives/estree.rs @@ -115,9 +115,8 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> { AttrPart::String("custom_ts_def", value) => { struct_def.estree.custom_ts_def = Some(value); } - AttrPart::String("add_ts_def", value) => { - struct_def.estree.add_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), AttrPart::String("via", value) => struct_def.estree.via = Some(value), _ => return Err(()), diff --git a/tasks/ast_tools/src/generators/typescript.rs b/tasks/ast_tools/src/generators/typescript.rs index 19876cc09646e..c70909088debb 100644 --- a/tasks/ast_tools/src/generators/typescript.rs +++ b/tasks/ast_tools/src/generators/typescript.rs @@ -108,6 +108,11 @@ fn generate_ts_type_def(type_def: &TypeDef, code: &mut String, schema: &Schema) /// Generate Typescript type definition for a struct. fn generate_ts_type_def_for_struct(struct_def: &StructDef, schema: &Schema) -> Option { + // If struct marked with `#[estree(ts_alias = "...")]`, then it needs no type def + if struct_def.estree.ts_alias.is_some() { + return None; + } + // If struct is marked as `#[estree(flatten)]`, and only has a single field which isn't skipped, // don't generate a type def. That single field will be inserted inline into structs which include // this one rather than them extending this type. @@ -252,7 +257,13 @@ fn generate_ts_type_def_for_enum(enum_def: &EnumDef, schema: &Schema) -> String /// Get TS type name for a type. fn ts_type_name<'s>(type_def: &'s TypeDef, schema: &'s Schema) -> Cow<'s, str> { match type_def { - TypeDef::Struct(struct_def) => Cow::Borrowed(struct_def.name()), + TypeDef::Struct(struct_def) => { + if let Some(ts_alias) = &struct_def.estree.ts_alias { + Cow::Borrowed(ts_alias) + } else { + Cow::Borrowed(struct_def.name()) + } + } TypeDef::Enum(enum_def) => Cow::Borrowed(enum_def.name()), TypeDef::Primitive(primitive_def) => Cow::Borrowed(match primitive_def.name() { #[rustfmt::skip] diff --git a/tasks/ast_tools/src/schema/extensions/estree.rs b/tasks/ast_tools/src/schema/extensions/estree.rs index 79a42e19f46fe..a5f21ab39a0d0 100644 --- a/tasks/ast_tools/src/schema/extensions/estree.rs +++ b/tasks/ast_tools/src/schema/extensions/estree.rs @@ -16,6 +16,10 @@ pub struct ESTreeStruct { /// Custom TS type definition. Does not include `export`. /// Empty string if type should not have a TS type definition. pub custom_ts_def: Option, + /// TS alias. + /// 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, /// Additional custom TS type definition to add along with the generated one. /// Does not include `export`. pub add_ts_def: Option,