fix(unstable): wrong node with shorthand ObjectPattern + AssignPattern (#28402)

We did not serialize the `AssignmentPattern` node inside `ObjectPattern`
properties.

```ts
({ a = b } = {})
```

This is a bit different in SWC and looks like I got confused with the
different AST formats.

Fixes https://github.com/denoland/deno/issues/28399
This commit is contained in:
Marvin Hagemeister 2025-03-05 21:12:19 +01:00 committed by GitHub
parent 731a238d34
commit 7f7b51c414
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 145 additions and 32 deletions

View file

@ -1898,24 +1898,23 @@ fn serialize_pat(ctx: &mut TsEsTreeBuilder, pat: &Pat) -> NodeRef {
)
}
ObjectPatProp::Assign(assign_pat_prop) => {
let ident = serialize_binding_ident(ctx, &assign_pat_prop.key);
let key = serialize_binding_ident(ctx, &assign_pat_prop.key);
let mut value = serialize_binding_ident(ctx, &assign_pat_prop.key);
let shorthand = assign_pat_prop.value.is_none();
let value = assign_pat_prop.value.as_ref().map_or(
// SWC has value as optional with shorthand properties,
// but TSESTree expects the value to be a duplicate of
// the binding ident.
serialize_binding_ident(ctx, &assign_pat_prop.key),
|value| serialize_expr(ctx, value),
);
if let Some(assign) = &assign_pat_prop.value {
let expr = serialize_expr(ctx, assign);
value = ctx.write_assign_pat(&assign_pat_prop.span, value, expr);
}
ctx.write_property(
&assign_pat_prop.span,
&node.span,
shorthand,
false,
false,
PropertyKind::Init,
ident,
key,
value,
)
}