From a798ae66ce4d6f5e2e2e320cc5fbedf3cd6004ac Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Sat, 19 Oct 2024 10:14:01 +0100 Subject: [PATCH] fix(wasm): remove type def for `ArrayExpressionElement` --- crates/oxc_ast/src/ast/js.rs | 2 +- crates/oxc_ast/src/generated/derive_estree.rs | 3 --- crates/oxc_ast/src/serialize.rs | 3 --- tasks/ast_tools/src/derives/estree.rs | 23 +++++++++++++------ tasks/ast_tools/src/markers.rs | 11 ++++++++- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 2ee58141e8d616..dfe7a91596934c 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -310,7 +310,7 @@ inherit_variants! { #[ast(visit)] #[derive(Debug)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)] -#[estree(untagged)] +#[estree(untagged, custom_ts_def)] pub enum ArrayExpressionElement<'a> { /// `...[3, 4]` in `const array = [1, 2, ...[3, 4], null];` SpreadElement(Box<'a, SpreadElement<'a>>) = 64, diff --git a/crates/oxc_ast/src/generated/derive_estree.rs b/crates/oxc_ast/src/generated/derive_estree.rs index 3a908bd6065b1f..9d3b56019782f0 100644 --- a/crates/oxc_ast/src/generated/derive_estree.rs +++ b/crates/oxc_ast/src/generated/derive_estree.rs @@ -349,9 +349,6 @@ impl<'a> Serialize for ArrayExpressionElement<'a> { } } -#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)] -const TS_APPEND_CONTENT: &'static str = "export type ArrayExpressionElement = SpreadElement | Elision | BooleanLiteral | NullLiteral | NumericLiteral | BigIntLiteral | RegExpLiteral | StringLiteral | TemplateLiteral | IdentifierReference | MetaProperty | Super | ArrayExpression | ArrowFunctionExpression | AssignmentExpression | AwaitExpression | BinaryExpression | CallExpression | ChainExpression | Class | ConditionalExpression | Function | ImportExpression | LogicalExpression | NewExpression | ObjectExpression | ParenthesizedExpression | SequenceExpression | TaggedTemplateExpression | ThisExpression | UnaryExpression | UpdateExpression | YieldExpression | PrivateInExpression | JSXElement | JSXFragment | TSAsExpression | TSSatisfiesExpression | TSTypeAssertion | TSNonNullExpression | TSInstantiationExpression | ComputedMemberExpression | StaticMemberExpression | PrivateFieldExpression;"; - impl<'a> Serialize for ObjectExpression<'a> { fn serialize(&self, serializer: S) -> Result { let mut map = serializer.serialize_map(None)?; diff --git a/crates/oxc_ast/src/serialize.rs b/crates/oxc_ast/src/serialize.rs index a797dfdd2c05c8..257c9a2936aac7 100644 --- a/crates/oxc_ast/src/serialize.rs +++ b/crates/oxc_ast/src/serialize.rs @@ -60,9 +60,6 @@ impl Serialize for Elision { serializer.serialize_none() } } -#[cfg(feature = "serialize")] -#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)] -const TS_APPEND_CONTENT: &'static str = "export type Elision = null;"; /// Serialize `ArrayAssignmentTarget`, `ObjectAssignmentTarget`, `ObjectPattern`, `ArrayPattern` /// to be estree compatible, with `elements`/`properties` and `rest` fields combined. diff --git a/tasks/ast_tools/src/derives/estree.rs b/tasks/ast_tools/src/derives/estree.rs index 44153b4a07238c..8e758edd8e1dcc 100644 --- a/tasks/ast_tools/src/derives/estree.rs +++ b/tasks/ast_tools/src/derives/estree.rs @@ -27,10 +27,15 @@ impl Derive for DeriveESTree { TypeDef::Enum(def) => typescript_enum(def), TypeDef::Struct(def) => typescript_struct(def), }; - let ts_type_def = quote! { - #[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)] - const TS_APPEND_CONTENT: &'static str = #ts_type_def; + let ts_type_def = if let Some(ts_type_def) = ts_type_def { + quote! { + #[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)] + const TS_APPEND_CONTENT: &'static str = #ts_type_def; + } + } else { + TokenStream::new() }; + if let TypeDef::Struct(def) = def { if def .markers @@ -165,17 +170,21 @@ fn serialize_enum(def: &EnumDef) -> TokenStream { // Untagged enums: `type Expression = BooleanLiteral | NullLiteral;` // Tagged enums: `type PropertyKind = "init" | "get" | "set";` -fn typescript_enum(def: &EnumDef) -> String { +fn typescript_enum(def: &EnumDef) -> Option { + if def.markers.estree.custom_ts_def { + return None; + } + let union = if def.markers.estree.untagged { def.all_variants().map(|var| type_to_string(var.fields[0].typ.name())).join(" | ") } else { def.all_variants().map(|var| format!("\"{}\"", enum_variant_name(var, def))).join(" | ") }; let ident = def.ident(); - format!("export type {ident} = {union};") + Some(format!("export type {ident} = {union};")) } -fn typescript_struct(def: &StructDef) -> String { +fn typescript_struct(def: &StructDef) -> Option { let ident = def.ident(); let mut fields = String::new(); let mut extends = vec![]; @@ -207,7 +216,7 @@ fn typescript_struct(def: &StructDef) -> String { } let extends = if extends.is_empty() { String::new() } else { format!(" & {}", extends.join(" & ")) }; - format!("export type {ident} = ({{{fields}\n}}){extends};") + Some(format!("export type {ident} = ({{{fields}\n}}){extends};")) } fn type_to_string(ty: &TypeName) -> String { diff --git a/tasks/ast_tools/src/markers.rs b/tasks/ast_tools/src/markers.rs index 9b3996383fcf90..d81cc48c2f7f58 100644 --- a/tasks/ast_tools/src/markers.rs +++ b/tasks/ast_tools/src/markers.rs @@ -127,12 +127,14 @@ impl Parse for ESTreeStructAttribute { pub struct ESTreeEnumAttribute { pub rename_all: Option, pub untagged: bool, + pub custom_ts_def: bool, } impl Parse for ESTreeEnumAttribute { fn parse(input: ParseStream) -> Result { let mut rename_all = None; let mut untagged = false; + let mut custom_ts_def = false; loop { let ident = input.call(Ident::parse_any).unwrap().to_string(); @@ -151,6 +153,13 @@ impl Parse for ESTreeEnumAttribute { untagged = true; } } + "custom_ts_def" => { + if custom_ts_def { + panic!("Duplicate estree(custom_ts_def)"); + } else { + custom_ts_def = true; + } + } arg => panic!("Unsupported #[estree(...)] argument: {arg}"), } let comma = input.peek(Token![,]); @@ -160,7 +169,7 @@ impl Parse for ESTreeEnumAttribute { break; } } - Ok(Self { rename_all, untagged }) + Ok(Self { rename_all, untagged, custom_ts_def }) } }