oxc/crates/oxc_minifier/tests/mod.rs
camc314 a80460c6fe fix(minifier): correctly set self.changed when minimizing if stmts (#8420)
Before:

```
==== Input ====
require('./index.js')(function (e, os) {
  if (e) return console.log(e)
  return console.log(JSON.stringify(os))
})

==== First Minification
require("./index.js")(function(e, os) {
        return console.log(e ? e : JSON.stringify(os));
});

==== Second Minification ====
require("./index.js")(function(e, os) {
        return console.log(e || JSON.stringify(os));
});

same = false
```

After:
```
==== Input ====
require('./index.js')(function (e, os) {
  if (e) return console.log(e)
  return console.log(JSON.stringify(os))
})

==== First Minification ====
require("./index.js")(function(e, os) {
        return console.log(e || JSON.stringify(os));
});

==== Second Minification ====
require("./index.js")(function(e, os) {
        return console.log(e || JSON.stringify(os));
});

same = true
```
2025-01-11 01:12:09 +00:00

33 lines
1.1 KiB
Rust

mod ast_passes;
mod ecmascript;
mod mangler;
use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_minifier::{CompressOptions, Compressor};
use oxc_parser::Parser;
use oxc_span::SourceType;
pub(crate) fn test(source_text: &str, expected: &str, options: CompressOptions) {
let source_type = SourceType::default();
let result = run(source_text, source_type, Some(options));
let expected = run(expected, source_type, None);
assert_eq!(result, expected, "\nfor source\n{source_text}\nexpect\n{expected}\ngot\n{result}");
}
pub(crate) fn run(
source_text: &str,
source_type: SourceType,
options: Option<CompressOptions>,
) -> String {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let mut program = ret.program;
if let Some(options) = options {
Compressor::new(&allocator, options).build(&mut program);
}
CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(&program)
.code
}