From 405b73d8e72b92fb329d777dd157c78decf4f5c2 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Thu, 16 Jan 2025 03:08:26 +0000 Subject: [PATCH] fix(minifier): do not change `delete undefined` to `delete void 0` (#8527) `delete undefined` returns `false` `delete void 0` returns `true` --- .../ast_passes/peephole_substitute_alternate_syntax.rs | 9 +++++++++ 1 file changed, 9 insertions(+) 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 a8f8939fac6d4..de73732b0a357 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 @@ -250,6 +250,12 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { if !ctx.is_identifier_undefined(ident) { return None; } + // `delete undefined` returns `false` + // `delete void 0` returns `true` + if matches!(ctx.parent(), Ancestor::UnaryExpressionArgument(e) if e.operator().is_delete()) + { + return None; + } Some(ctx.ast.void_0(ident.span)) } @@ -1297,6 +1303,9 @@ mod test { // destructuring throw error side effect test_same("var {} = void 0"); test_same("var [] = void 0"); + // `delete undefined` returns `false` + // `delete void 0` returns `true` + test_same("delete undefined"); } #[test]