Skip to content

Commit

Permalink
feat(minifier): reenable mangler
Browse files Browse the repository at this point in the history
closes #952
closes #955
  • Loading branch information
Boshen committed Oct 8, 2023
1 parent 96aba4f commit c5ce2ae
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
14 changes: 7 additions & 7 deletions crates/oxc_minifier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! ECMAScript Minifier
mod compressor;
// mod mangler;
mod mangler;
mod printer;

use oxc_allocator::Allocator;
Expand All @@ -10,7 +10,7 @@ use oxc_span::SourceType;

pub use crate::{
compressor::{CompressOptions, Compressor},
// mangler::ManglerBuilder,
mangler::ManglerBuilder,
printer::{Printer, PrinterOptions},
};

Expand Down Expand Up @@ -43,11 +43,11 @@ impl<'a> Minifier<'a> {
let ret = Parser::new(&allocator, self.source_text, self.source_type).parse();
let program = allocator.alloc(ret.program);
Compressor::new(&allocator, self.options.compress).build(program);
let printer = Printer::new(self.source_text.len(), self.options.print);
// if self.options.mangle {
// let mangler = ManglerBuilder::new(self.source_text, self.source_type).build(program);
// printer.with_mangler(mangler);
// }
let mut printer = Printer::new(self.source_text.len(), self.options.print);
if self.options.mangle {
let mangler = ManglerBuilder::new(self.source_text, self.source_type).build(program);
printer.with_mangler(mangler);
}
printer.build(program)
}
}
23 changes: 11 additions & 12 deletions crates/oxc_minifier/src/mangler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use itertools::Itertools;
#[allow(clippy::wildcard_imports)]
use oxc_hir::hir::*;
use oxc_hir::Visit;
use oxc_ast::ast::*;
use oxc_ast::Visit;
use oxc_index::{index_vec, IndexVec};
use oxc_semantic::{Reference, ReferenceFlag, ReferenceId, SemanticBuilder, SymbolId, SymbolTable};
use oxc_span::{Atom, SourceType};
Expand Down Expand Up @@ -78,15 +78,14 @@ impl<'a> Visit<'a> for ManglerBuilder<'a> {
self.semantic.leave_scope();
}

fn visit_binding_identifier(
&mut self,
ident: &'a BindingIdentifier,
includes: SymbolFlags,
excludes: SymbolFlags,
) {
let symbol_id =
self.semantic.declare_symbol_for_mangler(ident.span, &ident.name, includes, excludes);
ident.symbol_id.replace(symbol_id);
fn visit_binding_identifier(&mut self, ident: &'a BindingIdentifier) {
let symbol_id = self.semantic.declare_symbol_for_mangler(
ident.span,
&ident.name,
/* TODO: add symbol flags */ SymbolFlags::empty(),
/* TODO: add symbol flags */ SymbolFlags::empty(),
);
ident.symbol_id.replace(Some(symbol_id));
}

fn visit_identifier_reference(&mut self, ident: &'a IdentifierReference) {
Expand All @@ -97,7 +96,7 @@ impl<'a> Visit<'a> for ManglerBuilder<'a> {
ReferenceFlag::read(),
);
let reference_id = self.semantic.declare_reference(reference);
ident.reference_id.replace(reference_id);
ident.reference_id.replace(Some(reference_id));
}
}

Expand Down
14 changes: 8 additions & 6 deletions crates/oxc_minifier/src/printer/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,12 +775,14 @@ impl<'a> GenExpr for Expression<'a> {

impl Gen for IdentifierReference {
fn gen(&self, p: &mut Printer, ctx: Context) {
// if let Some(mangler) = &p.mangler {
// if let Some(name) = mangler.get_reference_name(self.reference_id.clone().into_inner()) {
// p.print_str(name.clone().as_bytes());
// return;
// }
// }
if let Some(mangler) = &p.mangler {
if let Some(reference_id) = self.reference_id.clone().into_inner() {
if let Some(name) = mangler.get_reference_name(reference_id) {
p.print_str(name.clone().as_bytes());
return;
}
}
}
p.print_str(self.name.as_bytes());
}
}
Expand Down
27 changes: 15 additions & 12 deletions crates/oxc_minifier/src/printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ use self::{
gen::{Gen, GenExpr},
operator::Operator,
};
// use crate::mangler::Mangler;
use crate::mangler::Mangler;

#[derive(Debug, Default, Clone, Copy)]
pub struct PrinterOptions;

pub struct Printer {
options: PrinterOptions,

// mangler: Option<Mangler>,
mangler: Option<Mangler>,

/// Output Code
code: Vec<u8>,

Expand Down Expand Up @@ -70,7 +71,7 @@ impl Printer {
let capacity = source_len / 2;
Self {
options,
// mangler: None,
mangler: None,
code: Vec::with_capacity(capacity),
needs_semicolon: false,
need_space_before_dot: 0,
Expand All @@ -83,9 +84,9 @@ impl Printer {
}
}

// pub fn with_mangler(&mut self, mangler: Mangler) {
// self.mangler = Some(mangler);
// }
pub fn with_mangler(&mut self, mangler: Mangler) {
self.mangler = Some(mangler);
}

pub fn build(mut self, program: &Program<'_>) -> String {
program.gen(&mut self, Context::default());
Expand Down Expand Up @@ -245,13 +246,15 @@ impl Printer {
}
}

fn print_symbol(&mut self, _symbol_id: Option<SymbolId>, fallback: &Atom) {
// if let Some(mangler) = &self.mangler {
// let name = mangler.get_symbol_name(symbol_id);
// self.print_str(name.clone().as_bytes());
// } else {
fn print_symbol(&mut self, symbol_id: Option<SymbolId>, fallback: &Atom) {
if let Some(mangler) = &self.mangler {
if let Some(symbol_id) = symbol_id {
let name = mangler.get_symbol_name(symbol_id);
self.print_str(name.clone().as_bytes());
return;
}
}
self.print_str(fallback.as_bytes());
// }
}

fn wrap<F: FnMut(&mut Self)>(&mut self, wrap: bool, mut f: F) {
Expand Down

0 comments on commit c5ce2ae

Please sign in to comment.