Skip to content

Commit 644dd6e

Browse files
committed
perf: maybe_foldable_expr
1 parent e6caa6f commit 644dd6e

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

crates/jsshaker/src/folding/mod.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ mod dep;
33
use std::mem;
44

55
use dep::{FoldableDep, UnFoldableDep};
6-
use oxc::{allocator, ast::ast::Expression, span::GetSpan};
6+
use oxc::{
7+
allocator,
8+
ast::{ast::Expression, match_member_expression},
9+
span::GetSpan,
10+
};
711
use rustc_hash::FxHashMap;
812

913
use crate::{
@@ -112,3 +116,53 @@ impl<'a> Transformer<'a> {
112116
if let FoldingData::Foldable { literal, .. } = data { Some(*literal) } else { None }
113117
}
114118
}
119+
120+
pub fn maybe_foldable_expr(node: &Expression) -> bool {
121+
match node {
122+
match_member_expression!(Expression) => true,
123+
124+
Expression::StringLiteral(_)
125+
| Expression::NumericLiteral(_)
126+
| Expression::BigIntLiteral(_)
127+
| Expression::BooleanLiteral(_)
128+
| Expression::NullLiteral(_)
129+
| Expression::RegExpLiteral(_)
130+
| Expression::TemplateLiteral(_) => false,
131+
132+
Expression::FunctionExpression(_)
133+
| Expression::ArrowFunctionExpression(_)
134+
| Expression::ObjectExpression(_)
135+
| Expression::ParenthesizedExpression(_)
136+
| Expression::SequenceExpression(_)
137+
| Expression::ImportExpression(_)
138+
| Expression::NewExpression(_)
139+
| Expression::ClassExpression(_) => false,
140+
141+
Expression::Identifier(_)
142+
| Expression::UnaryExpression(_)
143+
| Expression::UpdateExpression(_)
144+
| Expression::BinaryExpression(_)
145+
| Expression::LogicalExpression(_)
146+
| Expression::ConditionalExpression(_)
147+
| Expression::CallExpression(_)
148+
| Expression::TaggedTemplateExpression(_)
149+
| Expression::AwaitExpression(_)
150+
| Expression::YieldExpression(_)
151+
| Expression::ArrayExpression(_)
152+
| Expression::AssignmentExpression(_)
153+
| Expression::ChainExpression(_)
154+
| Expression::MetaProperty(_)
155+
| Expression::PrivateInExpression(_)
156+
| Expression::ThisExpression(_)
157+
| Expression::Super(_) => true,
158+
159+
Expression::JSXElement(_) | Expression::JSXFragment(_) => true,
160+
161+
Expression::V8IntrinsicExpression(_)
162+
| Expression::TSAsExpression(_)
163+
| Expression::TSInstantiationExpression(_)
164+
| Expression::TSTypeAssertion(_)
165+
| Expression::TSNonNullExpression(_)
166+
| Expression::TSSatisfiesExpression(_) => unreachable!(),
167+
}
168+
}

crates/jsshaker/src/nodes/expr/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use oxc::{
3131
use oxc_syntax::operator::{AssignmentOperator, UnaryOperator};
3232

3333
use crate::{
34-
analyzer::Analyzer, build_effect, dep::DepTrait, entity::Entity, transformer::Transformer,
35-
utils::ast::AstKind2,
34+
analyzer::Analyzer, build_effect, dep::DepTrait, entity::Entity, folding::maybe_foldable_expr,
35+
transformer::Transformer, utils::ast::AstKind2,
3636
};
3737

3838
impl<'a> Analyzer<'a> {
@@ -86,7 +86,11 @@ impl<'a> Analyzer<'a> {
8686
| Expression::TSSatisfiesExpression(_) => unreachable!(),
8787
};
8888
self.pop_span();
89-
self.try_fold_node(AstKind2::Expression(node), value)
89+
if maybe_foldable_expr(node) {
90+
self.try_fold_node(AstKind2::Expression(node), value)
91+
} else {
92+
value
93+
}
9094
}
9195

9296
pub fn exec_expression_with_dependency(

0 commit comments

Comments
 (0)