fix(es/codegen): Improve EndsWithAlphaNum (#9675)

**Description:**

This PR makes EndsWithAlphaNum smarter, improving codegen of in/instanceof binary expressions, and for in/of heads. For example:

Before: 
```js
foo() in b;
```
After:
```js
foo()in b;
```

---------

Co-authored-by: Donny/강동윤 <kdy1997.dev@gmail.com>
This commit is contained in:
CamWass 2024-10-24 20:34:18 +13:00 committed by GitHub
parent 8f45eaf837
commit ba2a942f56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 11 deletions

View File

@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---
fix(es/codegen): Improve EndsWithAlphaNum

View File

@ -65,6 +65,18 @@ mod tests {
assert_min("2 >>> 2", "2>>>2");
assert_min("foo in bar", "foo in bar");
assert_min("foo instanceof Foo", "foo instanceof Foo");
assert_min("foo() in b", "foo()in b");
assert_min("typeof foo() in b", "typeof foo()in b");
assert_min("`` in b", "``in b");
assert_min("a?.foo() in b", "a?.foo()in b");
assert_min("++a[1] in b", "++a[1]in b");
assert_min("a++ in foo", "a++in foo");
assert_min("``+`` in b", "``+``in b");
assert_min("new Foo(a) in b", "new Foo(a)in b");
assert_min("new Foo() in b", "new Foo in b");
assert_min("++a in b", "++a in b");
}
#[test]

View File

@ -52,8 +52,7 @@ impl EndsWithAlphaNum for UsingDecl {
impl EndsWithAlphaNum for Expr {
fn ends_with_alpha_num(&self) -> bool {
!matches!(
self,
match self {
Expr::Array(..)
| Expr::Object(..)
| Expr::Lit(Lit::Str(..))
@ -62,7 +61,27 @@ impl EndsWithAlphaNum for Expr {
prop: MemberProp::Computed(..),
..
})
)
| Expr::Tpl(..)
| Expr::TaggedTpl(..)
| Expr::Call(..) => false,
Expr::Unary(n) => n.arg.ends_with_alpha_num(),
Expr::Update(n) => n.prefix && n.arg.ends_with_alpha_num(),
Expr::OptChain(n) => match n.base.as_ref() {
OptChainBase::Member(base) => base.prop.is_computed(),
OptChainBase::Call(_) => false,
},
Expr::Bin(n) => n.right.ends_with_alpha_num(),
Expr::New(NewExpr {
args: Some(args), ..
}) => args.is_empty(),
_ => true,
}
}
}