Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wasm): remove type defs for ArrayExpressionElement and Elision #6683

Open
wants to merge 1 commit into
base: 10-19-refactor_ast_tools_use_spaces_not_tabs_in_ts_type_defs
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 15 additions & 6 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ impl Derive for DeriveESTree {
fn derive(&mut self, def: &TypeDef, _: &LateCtx) -> TokenStream {
let ts_type_def = match def {
TypeDef::Enum(def) => typescript_enum(def),
TypeDef::Struct(def) => typescript_struct(def),
TypeDef::Struct(def) => Some(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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Option::map_or would make this easier to read? I think the implementation is fine especially since it'll be temporary.

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
Expand Down Expand Up @@ -165,14 +170,18 @@ 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<String> {
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 {
Expand Down
11 changes: 10 additions & 1 deletion tasks/ast_tools/src/markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,14 @@ impl Parse for ESTreeStructAttribute {
pub struct ESTreeEnumAttribute {
pub rename_all: Option<String>,
pub untagged: bool,
pub custom_ts_def: bool,
}

impl Parse for ESTreeEnumAttribute {
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
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();
Expand All @@ -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![,]);
Expand All @@ -160,7 +169,7 @@ impl Parse for ESTreeEnumAttribute {
break;
}
}
Ok(Self { rename_all, untagged })
Ok(Self { rename_all, untagged, custom_ts_def })
}
}

Expand Down
Loading