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

refactor(minifier): use Ctx #8716

Merged
merged 1 commit into from
Jan 25, 2025
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_traverse::TraverseCtx;

use crate::ctx::Ctx;

use super::PeepholeOptimizations;

Expand All @@ -17,7 +18,7 @@ impl<'a> PeepholeOptimizations {
pub fn collapse_variable_declarations(
&mut self,
stmts: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
ctx: Ctx<'a, '_>,
) {
self.join_vars(stmts, ctx);
self.maybe_collapse_into_for_statements(stmts, ctx);
Expand All @@ -44,7 +45,7 @@ impl<'a> PeepholeOptimizations {
None
}

fn join_vars(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
fn join_vars(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: Ctx<'a, '_>) {
if stmts.len() < 2 {
return;
}
Expand Down Expand Up @@ -102,7 +103,7 @@ impl<'a> PeepholeOptimizations {
fn maybe_collapse_into_for_statements(
&mut self,
stmts: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
ctx: Ctx<'a, '_>,
) {
if stmts.len() <= 1 {
return;
Expand Down Expand Up @@ -142,7 +143,7 @@ impl<'a> PeepholeOptimizations {
&mut self,
i: usize,
stmts: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
ctx: Ctx<'a, '_>,
) {
if let Statement::ExpressionStatement(expr_stmt) = ctx.ast.move_statement(&mut stmts[i]) {
if let Statement::ForStatement(for_stmt) = &mut stmts[i + 1] {
Expand All @@ -156,7 +157,7 @@ impl<'a> PeepholeOptimizations {
&mut self,
i: usize,
stmts: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
ctx: Ctx<'a, '_>,
) {
if let Statement::VariableDeclaration(var) = ctx.ast.move_statement(&mut stmts[i]) {
if let Statement::ForStatement(for_stmt) = &mut stmts[i + 1] {
Expand All @@ -181,7 +182,7 @@ impl<'a> PeepholeOptimizations {
&mut self,
i: usize,
stmts: &mut Vec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
ctx: Ctx<'a, '_>,
) {
if let Statement::VariableDeclaration(decl) = &stmts[i] {
if decl.kind.is_var()
Expand Down
12 changes: 4 additions & 8 deletions crates/oxc_minifier/src/peephole/convert_to_dotted_properties.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use oxc_ast::ast::*;
use oxc_syntax::identifier::is_identifier_name;
use oxc_traverse::TraverseCtx;

use super::LatePeepholeOptimizations;
use crate::ctx::Ctx;

use super::LatePeepholeOptimizations;

impl<'a> LatePeepholeOptimizations {
/// Converts property accesses from quoted string or bracket access syntax to dot or unquoted string
/// syntax, where possible. Dot syntax is more compact.
Expand All @@ -13,10 +13,7 @@ impl<'a> LatePeepholeOptimizations {
///
/// `foo['bar']` -> `foo.bar`
/// `foo?.['bar']` -> `foo?.bar`
pub fn convert_to_dotted_properties(
expr: &mut MemberExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
pub fn convert_to_dotted_properties(expr: &mut MemberExpression<'a>, ctx: Ctx<'a, '_>) {
let MemberExpression::ComputedMemberExpression(e) = expr else { return };
let Expression::StringLiteral(s) = &e.expression else { return };
if is_identifier_name(&s.value) {
Expand All @@ -32,8 +29,7 @@ impl<'a> LatePeepholeOptimizations {
return;
}
if let Some(n) = Ctx::string_to_equivalent_number_value(v) {
e.expression =
Ctx(ctx).ast.expression_numeric_literal(s.span, n, None, NumberBase::Decimal);
e.expression = ctx.ast.expression_numeric_literal(s.span, n, None, NumberBase::Decimal);
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions crates/oxc_minifier/src/peephole/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use oxc_syntax::{
number::NumberBase,
operator::{BinaryOperator, LogicalOperator},
};
use oxc_traverse::{Ancestor, TraverseCtx};
use oxc_traverse::Ancestor;

use crate::ctx::Ctx;

Expand All @@ -18,12 +18,7 @@ impl<'a, 'b> PeepholeOptimizations {
/// Constant Folding
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeFoldConstants.java>
pub fn fold_constants_exit_expression(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let ctx = Ctx(ctx);
pub fn fold_constants_exit_expression(&mut self, expr: &mut Expression<'a>, ctx: Ctx<'a, '_>) {
if let Some(folded_expr) = match expr {
Expression::BinaryExpression(e) => Self::try_fold_binary_expr(e, ctx)
.or_else(|| Self::try_fold_binary_typeof_comparison(e, ctx)),
Expand Down
Loading
Loading