From 22355f73f39f6186339b4e1c8f41f30862cdbd58 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:42:04 +0000 Subject: [PATCH] fix(minifier): do not remove `undefined` for destructuring patterns (#6828) --- .../peephole_substitute_alternate_syntax.rs | 7 ++++++- tasks/coverage/snapshots/runtime.snap | 14 +------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs index f67905f03..5d78943b9 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs @@ -298,7 +298,8 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { decl: &mut VariableDeclarator<'a>, ctx: Ctx<'a, 'b>, ) { - if decl.kind.is_const() { + // Destructuring Pattern has error throwing side effect. + if decl.kind.is_const() || decl.id.kind.is_destructuring_pattern() { return; } if decl.init.as_ref().is_some_and(|init| ctx.is_expression_undefined(init)) { @@ -592,6 +593,10 @@ mod test { // shadowd test_same("(function(undefined) { let x = typeof undefined; })()"); + + // destructuring throw error side effect + test_same("var {} = void 0"); + test_same("var [] = void 0"); } #[test] diff --git a/tasks/coverage/snapshots/runtime.snap b/tasks/coverage/snapshots/runtime.snap index c42f65690..542927ad0 100644 --- a/tasks/coverage/snapshots/runtime.snap +++ b/tasks/coverage/snapshots/runtime.snap @@ -2,7 +2,7 @@ commit: 06454619 runtime Summary: AST Parsed : 18444/18444 (100.00%) -Positive Passed: 18194/18444 (98.64%) +Positive Passed: 18198/18444 (98.67%) tasks/coverage/test262/test/annexB/language/function-code/if-decl-else-decl-a-func-existing-block-fn-no-init.js minify error: Test262Error: Expected SameValue(«function f(){}», «undefined») to be true @@ -735,21 +735,9 @@ codegen error: Test262Error: Expected SameValue(«"B"», «"A"») to be true tasks/coverage/test262/test/language/statements/async-generator/return-undefined-implicit-and-explicit.js minify error: Test262Error: Actual [tick 1, g1 ret, g2 ret, g3 ret, g4 ret, tick 2] and expected [tick 1, g1 ret, g2 ret, tick 2, g3 ret, g4 ret] should have the same contents. Ticks for implicit and explicit return undefined -tasks/coverage/test262/test/language/statements/for/dstr/let-obj-init-undefined.js -minify error: SyntaxError: Missing initializer in destructuring declaration - -tasks/coverage/test262/test/language/statements/for/dstr/var-obj-init-undefined.js -minify error: SyntaxError: Missing initializer in destructuring declaration - tasks/coverage/test262/test/language/statements/for-of/string-astral-truncated.js codegen error: Test262Error: Expected SameValue(«"\\"», «"\\ud801"») to be true -tasks/coverage/test262/test/language/statements/let/dstr/obj-init-undefined.js -minify error: SyntaxError: Missing initializer in destructuring declaration - -tasks/coverage/test262/test/language/statements/variable/dstr/obj-init-undefined.js -minify error: SyntaxError: Missing initializer in destructuring declaration - tasks/coverage/test262/test/language/statements/with/has-binding-call-with-proxy-env.js minify error: Test262Error: Actual [] and expected [has:Object] should have the same contents.