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
```
This commit is contained in:
camc314 2025-01-11 01:12:09 +00:00
parent b29655f7d9
commit a80460c6fe
3 changed files with 27 additions and 1 deletions

View file

@ -34,6 +34,7 @@ impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
ctx: &mut TraverseCtx<'a>,
) {
self.try_replace_if(stmts, ctx);
let changed = self.changed;
while self.changed {
self.changed = false;
self.try_replace_if(stmts, ctx);
@ -41,6 +42,7 @@ impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
stmts.retain(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
}
}
self.changed = self.changed || changed;
}
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {

View file

@ -1,6 +1,7 @@
mod dead_code_elimination;
use oxc_minifier::CompressOptions;
use oxc_span::SourceType;
fn test(source_text: &str, expected: &str) {
let options = CompressOptions::default();
@ -11,6 +12,15 @@ fn test_same(source_text: &str) {
test(source_text, source_text);
}
fn test_idempotent(source: &str, expected: &str) {
let expected = crate::run(expected, SourceType::default(), None);
let first = crate::run(source, SourceType::default(), Some(CompressOptions::default()));
let second = crate::run(&first, SourceType::default(), Some(CompressOptions::default()));
assert_eq!(second, expected, "\nfor source\n{source}\nexpect\n{expected}\ngot\n{second}");
assert_eq!(first, second);
}
// Oxc Integration Tests
#[test]
@ -30,6 +40,16 @@ fn integration() {
}
}",
);
test_idempotent(
"require('./index.js')(function (e, os) {
if (e) return console.log(e)
return console.log(JSON.stringify(os))
})",
r#"require("./index.js")(function(e, os) {
return console.log(e || JSON.stringify(os));
});"#,
);
}
#[test] // https://github.com/oxc-project/oxc/issues/4341

View file

@ -15,7 +15,11 @@ pub(crate) fn test(source_text: &str, expected: &str, options: CompressOptions)
assert_eq!(result, expected, "\nfor source\n{source_text}\nexpect\n{expected}\ngot\n{result}");
}
fn run(source_text: &str, source_type: SourceType, options: Option<CompressOptions>) -> String {
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;