Skip to content

Commit

Permalink
feat(mangler): add debug mode (#4314)
Browse files Browse the repository at this point in the history
closes #4303
  • Loading branch information
Boshen committed Jul 17, 2024
1 parent c362bf7 commit 5d17675
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/oxc_mangler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ oxc_ast = { workspace = true }
oxc_semantic = { workspace = true }
oxc_index = { workspace = true }
itertools = { workspace = true }

[dev-dependencies]
oxc_allocator = { workspace = true }
oxc_parser = { workspace = true }
oxc_codegen = { workspace = true }
pico-args = { workspace = true }
43 changes: 43 additions & 0 deletions crates/oxc_mangler/examples/mangler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![allow(clippy::print_stdout)]
use std::path::Path;

use oxc_allocator::Allocator;
use oxc_codegen::CodeGenerator;
use oxc_mangler::ManglerBuilder;
use oxc_parser::Parser;
use oxc_span::SourceType;
use pico_args::Arguments;

// Instruction:
// create a `test.js`,
// run `cargo run -p oxc_mangler --example mangler` or `just example mangler`

fn main() -> std::io::Result<()> {
let mut args = Arguments::from_env();

let name = args.subcommand().ok().flatten().unwrap_or_else(|| String::from("test.js"));
let debug = args.contains("--debug");
let twice = args.contains("--twice");

let path = Path::new(&name);
let source_text = std::fs::read_to_string(path)?;
let source_type = SourceType::from_path(path).unwrap();

let printed = mangler(&source_text, source_type, debug);
println!("{printed}");

if twice {
let printed = mangler(&printed, source_type, debug);
println!("{printed}");
}

Ok(())
}

fn mangler(source_text: &str, source_type: SourceType, debug: bool) -> String {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let mangler = ManglerBuilder::default().debug(debug).build(program);
CodeGenerator::new().with_mangler(Some(mangler)).build(program).source_text
}
18 changes: 16 additions & 2 deletions crates/oxc_mangler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ impl Mangler {
/// }
/// }
/// ```
pub struct ManglerBuilder;
#[derive(Debug, Default)]
pub struct ManglerBuilder {
debug: bool,
}

impl ManglerBuilder {
#[must_use]
pub fn debug(mut self, yes: bool) -> Self {
self.debug = yes;
self
}

#[must_use]
pub fn build<'a>(self, program: &'a Program<'a>) -> Mangler {
let semantic_ret = SemanticBuilder::new("", program.source_type).build(program);
Expand Down Expand Up @@ -119,10 +128,11 @@ impl ManglerBuilder {

let mut names = Vec::with_capacity(total_number_of_slots);

let generate_name = if self.debug { debug_name } else { base54 };
let mut count = 0;
for _ in 0..total_number_of_slots {
names.push(loop {
let name = base54(count);
let name = generate_name(count);
count += 1;
// Do not mangle keywords and unresolved references
if !is_keyword(&name) && !unresolved_references.iter().any(|n| **n == name) {
Expand Down Expand Up @@ -236,3 +246,7 @@ fn base54(n: usize) -> CompactStr {
}
CompactStr::new(&ret)
}

fn debug_name(n: usize) -> CompactStr {
CompactStr::from(format!("slot_{n}"))
}
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Minifier {

pub fn build<'a>(self, allocator: &'a Allocator, program: &mut Program<'a>) -> MinifierReturn {
Compressor::new(allocator, self.options.compress).build(program);
let mangler = self.options.mangle.then(|| ManglerBuilder.build(program));
let mangler = self.options.mangle.then(|| ManglerBuilder::default().build(program));
MinifierReturn { mangler }
}
}

0 comments on commit 5d17675

Please sign in to comment.