Skip to content

Commit

Permalink
fix(minifier): fix keep_var keeping vars from arrow functions (#4680)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Aug 6, 2024
1 parent 451ac4d commit bf48c7f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ oxc_macros.opt-level = 1
oxc_ast_macros.opt-level = 1
# Compile insta and its dependencies in release mode for faster snapshot tests
# See: https://insta.rs/docs/quickstart/#optional-faster-runs
insta.opt-level = 3
similar.opt-level = 3
insta.opt-level = 3
similar.opt-level = 3

[profile.release.package.oxc_wasm]
opt-level = 'z'
Expand Down
16 changes: 5 additions & 11 deletions crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::path::Path;

use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, WhitespaceRemover};
use oxc_codegen::CodeGenerator;
use oxc_minifier::{CompressOptions, Minifier, MinifierOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
Expand All @@ -17,34 +17,28 @@ fn main() -> std::io::Result<()> {

let name = args.subcommand().ok().flatten().unwrap_or_else(|| String::from("test.js"));
let mangle = args.contains("--mangle");
let whitespace = args.contains("--whitespace");
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 = minify(&source_text, source_type, mangle, whitespace);
let printed = minify(&source_text, source_type, mangle);
println!("{printed}");

if twice {
let printed = minify(&printed, source_type, mangle, whitespace);
let printed = minify(&printed, source_type, mangle);
println!("{printed}");
}

Ok(())
}

fn minify(source_text: &str, source_type: SourceType, mangle: bool, whitespace: bool) -> String {
fn minify(source_text: &str, source_type: SourceType, mangle: bool) -> String {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let options = MinifierOptions { mangle, compress: CompressOptions::all_true() };
let ret = Minifier::new(options).build(&allocator, program);
if whitespace {
CodeGenerator::new().with_mangler(ret.mangler).build(program)
} else {
WhitespaceRemover::new().with_mangler(ret.mangler).build(program)
}
.source_text
CodeGenerator::new().with_mangler(ret.mangler).build(program).source_text
}
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/ast_passes/remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'a> RemoveDeadCode<'a> {

let mut keep_var = KeepVar::new(self.ast);

for stmt in stmts.iter().skip(index) {
for stmt in stmts.iter().skip(index + 1) {
keep_var.visit_statement(stmt);
}

Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_minifier/src/keep_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ impl<'a> Visit<'a> for KeepVar<'a> {
/* skip functions */
}

fn visit_arrow_function_expression(&mut self, _it: &ArrowFunctionExpression<'a>) {}

fn visit_class(&mut self, _it: &Class<'a>) {
/* skip classes */
}
Expand Down
26 changes: 25 additions & 1 deletion crates/oxc_minifier/tests/oxc/remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ fn print(source_text: &str, remove_dead_code: bool) -> String {
.source_text
}

pub(crate) fn test(source_text: &str, expected: &str) {
fn test(source_text: &str, expected: &str) {
let minified = print(source_text, true);
let expected = print(expected, false);
assert_eq!(minified, expected, "for source {source_text}");
}

fn test_same(source_text: &str) {
let minified = print(source_text, true);
let expected = print(source_text, false);
assert_eq!(minified, expected, "for source {source_text}");
}

#[test]
fn dce_if_statement() {
test("if (true) { foo }", "{ foo }");
Expand Down Expand Up @@ -119,6 +125,24 @@ fn dce_logical_expression() {
test("const foo = true && bar()", "const foo = bar()");
}

#[test]
fn dce_var_hoisting() {
test_same(
"function f() {
return () => {
var x;
}
}",
);
test_same(
"function f() {
return function g() {
var x;
}
}",
);
}

// https://github.com/terser/terser/blob/master/test/compress/dead-code.js
#[test]
fn dce_from_terser() {
Expand Down
8 changes: 1 addition & 7 deletions tasks/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,7 @@ lexer = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_c
parser = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"]
transformer = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common", "dep:oxc_transformer"]
semantic = ["dep:oxc_allocator", "dep:oxc_parser", "dep:oxc_semantic", "dep:oxc_span", "dep:oxc_tasks_common"]
minifier = [
"dep:oxc_allocator",
"dep:oxc_minifier",
"dep:oxc_parser",
"dep:oxc_span",
"dep:oxc_tasks_common",
]
minifier = ["dep:oxc_allocator", "dep:oxc_minifier", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"]
codegen = ["dep:oxc_allocator", "dep:oxc_codegen", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"]
sourcemap = [
"dep:oxc_allocator",
Expand Down

0 comments on commit bf48c7f

Please sign in to comment.