Skip to content

Commit a284ced

Browse files
committed
fix dce
1 parent 0efec10 commit a284ced

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/dce.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use oxc::minifier::CompressOptions;
2-
31
use crate::{Case, Driver, Source};
42

53
pub struct DceRunner;
@@ -14,6 +12,6 @@ impl Case for DceRunner {
1412
}
1513

1614
fn driver(&self) -> Driver {
17-
Driver { compress: Some(CompressOptions::dead_code_elimination()), ..Driver::default() }
15+
Driver { dce: true, ..Driver::default() }
1816
}
1917
}

src/driver.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ use std::{
55
};
66

77
use oxc::{
8+
allocator::Allocator,
9+
codegen::Codegen,
810
codegen::{CodegenOptions, CodegenReturn},
911
diagnostics::OxcDiagnostic,
1012
mangler::MangleOptions,
11-
minifier::CompressOptions,
12-
parser::{ParseOptions, ParserReturn},
13+
minifier::{CompressOptions, Compressor},
14+
parser::{ParseOptions, Parser, ParserReturn},
1315
span::SourceType,
1416
transformer::TransformOptions,
1517
CompilerInterface,
@@ -23,6 +25,7 @@ pub struct Driver {
2325
// options
2426
pub transform: Option<TransformOptions>,
2527
pub compress: Option<CompressOptions>,
28+
pub dce: bool,
2629
pub mangle: bool,
2730
pub remove_whitespace: bool,
2831
// states
@@ -93,6 +96,9 @@ impl Driver {
9396
source_text: &str,
9497
source_type: SourceType,
9598
) -> Result<String, Vec<Diagnostic>> {
99+
if self.dce {
100+
return self.dce(source_text, source_type);
101+
}
96102
self.path = source_path.to_path_buf();
97103
let mut source_type = source_type;
98104
if source_path.extension().unwrap() == "js" {
@@ -114,4 +120,16 @@ impl Driver {
114120
Err(errors)
115121
}
116122
}
123+
124+
pub fn dce(
125+
&mut self,
126+
source_text: &str,
127+
source_type: SourceType,
128+
) -> Result<String, Vec<Diagnostic>> {
129+
let allocator = Allocator::default();
130+
let mut ret = Parser::new(&allocator, source_text, source_type).parse();
131+
let program = &mut ret.program;
132+
Compressor::new(&allocator, CompressOptions::default()).dead_code_elimination(program);
133+
Ok(Codegen::new().build(program).code)
134+
}
117135
}

0 commit comments

Comments
 (0)