test(minifier): fix broken tests (#8722)

This commit is contained in:
Boshen 2025-01-26 03:47:07 +00:00
parent 37909337bd
commit 03229c5367
6 changed files with 802 additions and 787 deletions

View file

@ -4,14 +4,15 @@ use oxc_ast::ast::*;
///
/// Ported from [closure-compiler](https://github.com/google/closure-compiler/blob/f3ce5ed8b630428e311fe9aa2e20d36560d975e2/src/com/google/javascript/jscomp/AstAnalyzer.java#L94)
pub trait MayHaveSideEffects {
fn is_global_reference(&self, ident: &IdentifierReference<'_>) -> bool {
matches!(ident.name.as_str(), "undefined" | "NaN" | "Infinity")
}
fn is_global_reference(&self, ident: &IdentifierReference<'_>) -> bool;
fn expression_may_have_side_efffects(&self, e: &Expression<'_>) -> bool {
match e {
// Reference read can have a side effect.
Expression::Identifier(ident) => self.is_global_reference(ident),
Expression::Identifier(ident) => match ident.name.as_str() {
"NaN" | "Infinity" | "undefined" => !self.is_global_reference(ident),
_ => true,
},
Expression::NumericLiteral(_)
| Expression::BooleanLiteral(_)
| Expression::StringLiteral(_)

File diff suppressed because it is too large Load diff

View file

@ -751,7 +751,7 @@ mod test {
test("try {} catch (e) { foo() } finally { var x = bar() }", "{ var x = bar() }");
test("try {} finally { let x = foo() }", "{ let x = foo() }");
test("try {} catch (e) { foo() } finally { let x = bar() }", "{ let x = bar();}");
test("try {} catch () { } finally {}", "");
test("try {} catch (e) { } finally {}", "");
}
#[test]
@ -800,7 +800,7 @@ mod test {
#[test]
fn test_remove_empty_static_block() {
test("class Foo { static {}; foo }", "class Foo { foo }");
test_same("class Foo { static { foo() }");
test_same("class Foo { static { foo() } }");
}
#[test]

View file

@ -290,7 +290,7 @@ mod test {
// Never fuse a statement into a block that contains let/const/class declarations, or you risk
// colliding variable names. (unless the AST is normalized).
test("a; {b;}", "a,b");
test("a; {b; var a = 1;}", "{b; var a = 1;}");
test("a; {b; var a = 1;}", "{a, b; var a = 1;}");
test_same("a; { b; let a = 1; }");
test_same("a; { b; const a = 1; }");
test_same("a; { b; class a {} }");

View file

@ -940,7 +940,7 @@ mod test {
test("function f(){return undefined;}", "function f(){}");
test("function f(){if(a()){return undefined;}}", "function f(){if(a())return}");
test_same("function a(undefined) { return undefined; }");
test_same("function f(){return foo()");
test_same("function f(){return foo()}");
// `return undefined` has a different semantic in async generator function.
test("function foo() { return undefined }", "function foo() { }");
@ -1414,7 +1414,7 @@ mod test {
#[test]
fn test_fold_arrow_function_return() {
test("const foo = () => { return 'baz' }", "const foo = () => 'baz'");
test("const foo = () => { foo; return 'baz' }", "const foo = () => 'baz'");
test("const foo = () => { foo; return 'baz' }", "const foo = () => (foo, 'baz')");
}
#[test]

View file

@ -1,6 +1,6 @@
use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_parser::Parser;
use oxc_parser::{ParseOptions, Parser};
use oxc_span::SourceType;
use crate::{CompressOptions, Compressor};
@ -18,7 +18,15 @@ pub fn test(source_text: &str, expected: &str) {
pub fn run(source_text: &str, options: Option<CompressOptions>) -> String {
let allocator = Allocator::default();
let source_type = SourceType::mjs();
let mut program = Parser::new(&allocator, source_text, source_type).parse().program;
let ret = Parser::new(&allocator, source_text, source_type)
.with_options(ParseOptions {
allow_return_outside_function: true,
..ParseOptions::default()
})
.parse();
assert!(!ret.panicked, "{source_text}");
assert!(ret.errors.is_empty(), "{source_text}");
let mut program = ret.program;
if let Some(options) = options {
Compressor::new(&allocator, options).build(&mut program);
}