mirror of
https://github.com/swc-project/swc.git
synced 2024-11-22 06:46:41 +03:00
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:
parent
8f45eaf837
commit
ba2a942f56
6
.changeset/silly-wolves-suffer.md
Normal file
6
.changeset/silly-wolves-suffer.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
swc_core: patch
|
||||
swc_ecma_minifier: patch
|
||||
---
|
||||
|
||||
fix(es/codegen): Improve EndsWithAlphaNum
|
@ -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]
|
||||
|
@ -52,17 +52,36 @@ 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(..))
|
||||
| Expr::Paren(..)
|
||||
| Expr::Member(MemberExpr {
|
||||
prop: MemberProp::Computed(..),
|
||||
..
|
||||
})
|
||||
)
|
||||
| Expr::Object(..)
|
||||
| Expr::Lit(Lit::Str(..))
|
||||
| Expr::Paren(..)
|
||||
| Expr::Member(MemberExpr {
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user