-
-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(minifier): merge similar if stmts
- Loading branch information
Showing
4 changed files
with
147 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
|
||
use oxc::span::SourceType; | ||
use similar_asserts::SimpleDiff; | ||
|
||
use crate::workspace_root; | ||
use crate::{ | ||
suite::{Case, TestResult}, | ||
test262::{Test262Case, TestFlag}, | ||
Driver, | ||
}; | ||
|
||
pub struct EsbuildTest262Case { | ||
base: Test262Case, | ||
workspace_root: PathBuf, | ||
} | ||
|
||
impl Case for EsbuildTest262Case { | ||
fn new(path: PathBuf, code: String) -> Self { | ||
Self { base: Test262Case::new(path, code), workspace_root: workspace_root() } | ||
} | ||
|
||
fn code(&self) -> &str { | ||
self.base.code() | ||
} | ||
|
||
fn path(&self) -> &Path { | ||
self.base.path() | ||
} | ||
|
||
fn test_result(&self) -> &TestResult { | ||
self.base.test_result() | ||
} | ||
|
||
fn skip_test_case(&self) -> bool { | ||
self.base.should_fail() || self.base.skip_test_case() | ||
} | ||
|
||
fn run(&mut self) { | ||
let source_text = self.base.code(); | ||
let is_module = self.base.meta().flags.contains(&TestFlag::Module); | ||
let source_type = SourceType::default().with_module(is_module); | ||
|
||
let mut driver = | ||
Driver { compress: true, codegen: true, remove_whitespace: true, ..Driver::default() }; | ||
driver.run(source_text, source_type); | ||
let oxc = driver.printed; | ||
|
||
if oxc.is_empty() { | ||
self.base.set_result(TestResult::Passed); | ||
return; | ||
} | ||
|
||
let path = self.workspace_root.join(self.path()); | ||
let esbuild = Command::new("esbuild") | ||
.arg("--minify-whitespace=true") | ||
.arg("--minify-syntax=true") | ||
.arg("--keep-names") | ||
.arg(&path) | ||
.output() | ||
.unwrap(); | ||
|
||
if !esbuild.stderr.is_empty() { | ||
self.base.set_result(TestResult::Passed); | ||
return; | ||
} | ||
let esbuild = String::from_utf8(esbuild.stdout).unwrap(); | ||
|
||
let esbuild_len = esbuild.len(); | ||
let oxc_len = oxc.len(); | ||
|
||
if esbuild_len >= oxc_len { | ||
self.base.set_result(TestResult::Passed); | ||
return; | ||
} | ||
|
||
let diff = oxc_len - esbuild_len; | ||
|
||
println!("\n{} {diff}\n", self.base.path().to_string_lossy()); | ||
println!("\n{}\n", SimpleDiff::from_str(&oxc, &esbuild, "oxc", "esbuild")); | ||
|
||
self.base.set_result(TestResult::GenericError(">> ", format!("{diff}"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters