Skip to content

Commit ba2a942

Browse files
CamWasskdy1
andauthoredOct 24, 2024··
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>
1 parent 8f45eaf commit ba2a942

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed
 

‎.changeset/silly-wolves-suffer.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_minifier: patch
4+
---
5+
6+
fix(es/codegen): Improve EndsWithAlphaNum

‎crates/swc_ecma_codegen/src/expr.rs

+12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ mod tests {
6565
assert_min("2 >>> 2", "2>>>2");
6666
assert_min("foo in bar", "foo in bar");
6767
assert_min("foo instanceof Foo", "foo instanceof Foo");
68+
69+
assert_min("foo() in b", "foo()in b");
70+
assert_min("typeof foo() in b", "typeof foo()in b");
71+
assert_min("`` in b", "``in b");
72+
assert_min("a?.foo() in b", "a?.foo()in b");
73+
assert_min("++a[1] in b", "++a[1]in b");
74+
assert_min("a++ in foo", "a++in foo");
75+
assert_min("``+`` in b", "``+``in b");
76+
assert_min("new Foo(a) in b", "new Foo(a)in b");
77+
78+
assert_min("new Foo() in b", "new Foo in b");
79+
assert_min("++a in b", "++a in b");
6880
}
6981

7082
#[test]

‎crates/swc_ecma_codegen/src/util.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,36 @@ impl EndsWithAlphaNum for UsingDecl {
5252

5353
impl EndsWithAlphaNum for Expr {
5454
fn ends_with_alpha_num(&self) -> bool {
55-
!matches!(
56-
self,
55+
match self {
5756
Expr::Array(..)
58-
| Expr::Object(..)
59-
| Expr::Lit(Lit::Str(..))
60-
| Expr::Paren(..)
61-
| Expr::Member(MemberExpr {
62-
prop: MemberProp::Computed(..),
63-
..
64-
})
65-
)
57+
| Expr::Object(..)
58+
| Expr::Lit(Lit::Str(..))
59+
| Expr::Paren(..)
60+
| Expr::Member(MemberExpr {
61+
prop: MemberProp::Computed(..),
62+
..
63+
})
64+
| Expr::Tpl(..)
65+
| Expr::TaggedTpl(..)
66+
| Expr::Call(..) => false,
67+
68+
Expr::Unary(n) => n.arg.ends_with_alpha_num(),
69+
70+
Expr::Update(n) => n.prefix && n.arg.ends_with_alpha_num(),
71+
72+
Expr::OptChain(n) => match n.base.as_ref() {
73+
OptChainBase::Member(base) => base.prop.is_computed(),
74+
OptChainBase::Call(_) => false,
75+
},
76+
77+
Expr::Bin(n) => n.right.ends_with_alpha_num(),
78+
79+
Expr::New(NewExpr {
80+
args: Some(args), ..
81+
}) => args.is_empty(),
82+
83+
_ => true,
84+
}
6685
}
6786
}
6887

‎crates/swc_ecma_minifier/tests/full/issue-5912-bigdecimal/output.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.