Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(minifier): change foo?.['bar'] to foo?.bar #8176

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/oxc_minifier/src/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ impl<'a> Traverse<'a> for PeepholeOptimizations {
self.x4_peephole_substitute_alternate_syntax.exit_property_key(key, ctx);
}

fn exit_member_expression(
&mut self,
expr: &mut MemberExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.x4_peephole_substitute_alternate_syntax.exit_member_expression(expr, ctx);
}

fn exit_catch_clause(&mut self, catch: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {
self.x4_peephole_substitute_alternate_syntax.exit_catch_clause(catch, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
self.try_compress_property_key(key, ctx);
}

fn exit_member_expression(
&mut self,
expr: &mut MemberExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.try_compress_computed_member_expression(expr, Ctx(ctx));
}

fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let ctx = Ctx(ctx);

Expand All @@ -108,9 +116,6 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
Expression::NewExpression(e) => Self::try_fold_new_expression(e, ctx),
Expression::TemplateLiteral(t) => Self::try_fold_template_literal(t, ctx),
Expression::BinaryExpression(e) => Self::try_compress_typeof_undefined(e, ctx),
Expression::ComputedMemberExpression(e) => {
self.try_compress_computed_member_expression(e, ctx)
}
Expression::CallExpression(e) => {
Self::try_fold_literal_constructor_call_expression(e, ctx)
.or_else(|| Self::try_fold_simple_function_call(e, ctx))
Expand Down Expand Up @@ -736,23 +741,28 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
}

/// `foo['bar']` -> `foo.bar`
/// `foo?.['bar']` -> `foo?.bar`
fn try_compress_computed_member_expression(
&self,
e: &mut ComputedMemberExpression<'a>,
&mut self,
expr: &mut MemberExpression<'a>,
ctx: Ctx<'a, 'b>,
) -> Option<Expression<'a>> {
) {
if self.in_fixed_loop {
return None;
return;
}
let Expression::StringLiteral(s) = &e.expression else { return None };
if !is_identifier_name(&s.value) {
return None;

if let MemberExpression::ComputedMemberExpression(e) = expr {
let Expression::StringLiteral(s) = &e.expression else { return };
if !is_identifier_name(&s.value) {
return;
}
let property = ctx.ast.identifier_name(s.span, s.value.clone());
let object = ctx.ast.move_expression(&mut e.object);
*expr = MemberExpression::StaticMemberExpression(
ctx.ast.alloc_static_member_expression(e.span, object, property, e.optional),
);
self.changed = true;
}
let property = ctx.ast.identifier_name(s.span, s.value.clone());
let object = ctx.ast.move_expression(&mut e.object);
Some(Expression::StaticMemberExpression(
ctx.ast.alloc_static_member_expression(e.span, object, property, false),
))
}

fn compress_catch_clause(&mut self, catch: &mut CatchClause<'a>) {
Expand Down Expand Up @@ -1354,7 +1364,6 @@ mod test {
}

#[test]
#[ignore]
fn test_convert_to_dotted_properties_optional_chaining() {
test("data?.['name']", "data?.name");
test("data?.['name']?.['first']", "data?.name?.first");
Expand Down Expand Up @@ -1482,7 +1491,6 @@ mod test {
}

#[test]
#[ignore]
fn test_convert_to_dotted_properties_continue_optional_chaining() {
test("const opt1 = window?.a?.['b'];", "const opt1 = window?.a?.b;");

Expand Down
16 changes: 8 additions & 8 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ Original | minified | minified | gzip | gzip | Fixture

173.90 kB | 60.15 kB | 59.82 kB | 19.50 kB | 19.33 kB | moment.js

287.63 kB | 90.56 kB | 90.07 kB | 32.18 kB | 31.95 kB | jquery.js
287.63 kB | 90.54 kB | 90.07 kB | 32.17 kB | 31.95 kB | jquery.js

342.15 kB | 118.53 kB | 118.14 kB | 44.56 kB | 44.37 kB | vue.js
342.15 kB | 118.53 kB | 118.14 kB | 44.55 kB | 44.37 kB | vue.js

544.10 kB | 71.98 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js
544.10 kB | 71.97 kB | 72.48 kB | 26.19 kB | 26.20 kB | lodash.js

555.77 kB | 273.85 kB | 270.13 kB | 91.17 kB | 90.80 kB | d3.js

1.01 MB | 460.99 kB | 458.89 kB | 126.92 kB | 126.71 kB | bundle.min.js
1.01 MB | 460.99 kB | 458.89 kB | 126.91 kB | 126.71 kB | bundle.min.js

1.25 MB | 653.54 kB | 646.76 kB | 164.04 kB | 163.73 kB | three.js

2.14 MB | 727.90 kB | 724.14 kB | 180.39 kB | 181.07 kB | victory.js
2.14 MB | 727.17 kB | 724.14 kB | 180.34 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 332.29 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 332.27 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 493.24 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 493.10 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.88 kB | 915.50 kB | typescript.js
10.95 MB | 3.51 MB | 3.49 MB | 910.40 kB | 915.50 kB | typescript.js

Loading