diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 1655b749810eb..7be8f0833b2b6 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -6,7 +6,6 @@ use oxc_ast::ast::*; use oxc_span::GetSpan; use oxc_syntax::{ identifier::{LS, PS}, - keyword::is_reserved_keyword_or_global_object, operator::UnaryOperator, precedence::{GetPrecedence, Precedence}, }; @@ -1322,7 +1321,11 @@ impl<'a> GenExpr for MemberExpression<'a> { impl<'a> GenExpr for ComputedMemberExpression<'a> { fn gen_expr(&self, p: &mut Codegen, _precedence: Precedence, ctx: Context) { - self.object.print_expr(p, Precedence::Prefix, ctx.intersection(Context::FORBID_CALL)); + // `(let[0] = 100);` -> `(let)[0] = 100`; + let wrap = self.object.get_identifier_reference().is_some_and(|r| r.name == "let"); + p.wrap(wrap, |p| { + self.object.print_expr(p, Precedence::Prefix, ctx.intersection(Context::FORBID_CALL)); + }); if self.optional { p.print_str("?."); } @@ -1749,31 +1752,10 @@ impl<'a> GenExpr for ConditionalExpression<'a> { impl<'a> GenExpr for AssignmentExpression<'a> { fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { - // Destructuring assignment let n = p.code_len(); - - let identifier_is_keyword = match &self.left { - AssignmentTarget::AssignmentTargetIdentifier(target) => { - is_reserved_keyword_or_global_object(target.name.as_str()) - } - AssignmentTarget::ComputedMemberExpression(expression) => match &expression.object { - Expression::Identifier(ident) => { - is_reserved_keyword_or_global_object(ident.name.as_str()) - } - _ => false, - }, - AssignmentTarget::StaticMemberExpression(expression) => { - is_reserved_keyword_or_global_object(expression.property.name.as_str()) - } - AssignmentTarget::PrivateFieldExpression(expression) => { - is_reserved_keyword_or_global_object(expression.field.name.as_str()) - } - _ => false, - }; - - let wrap = ((p.start_of_stmt == n || p.start_of_arrow_expr == n) - && matches!(self.left, AssignmentTarget::ObjectAssignmentTarget(_))) - || identifier_is_keyword; + // Destructuring assignments must be parenthesized + let wrap = (p.start_of_stmt == n || p.start_of_arrow_expr == n) + && matches!(self.left, AssignmentTarget::ObjectAssignmentTarget(_)); p.wrap(wrap || precedence >= self.precedence(), |p| { self.left.print(p, ctx); p.print_soft_space(); diff --git a/crates/oxc_codegen/tests/integration/unit.rs b/crates/oxc_codegen/tests/integration/unit.rs index 3073fdf8b1d13..6db532f87210e 100644 --- a/crates/oxc_codegen/tests/integration/unit.rs +++ b/crates/oxc_codegen/tests/integration/unit.rs @@ -105,6 +105,7 @@ fn comma() { #[test] fn assignment() { + test("(let[0] = 100);", "(let)[0] = 100;\n"); test_minify("a = b ? c : d", "a=b?c:d;"); test_minify("[a,b] = (1, 2)", "[a,b]=(1,2);"); // `{a,b}` is a block, must wrap the whole expression to be an assignment expression diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 78a551bfcef68..37ffff5418b2b 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -6,13 +6,13 @@ Original | Minified | esbuild | Gzip | esbuild 287.63 kB | 92.91 kB | 90.07 kB | 32.33 kB | 31.95 kB | jquery.js -342.15 kB | 122.97 kB | 118.14 kB | 45.05 kB | 44.37 kB | vue.js +342.15 kB | 122.95 kB | 118.14 kB | 45.03 kB | 44.37 kB | vue.js 544.10 kB | 73.57 kB | 72.48 kB | 26.15 kB | 26.20 kB | lodash.js 555.77 kB | 274.81 kB | 270.13 kB | 91.40 kB | 90.80 kB | d3.js -1.01 MB | 471.70 kB | 458.89 kB | 127.56 kB | 126.71 kB | bundle.min.js +1.01 MB | 471.67 kB | 458.89 kB | 127.55 kB | 126.71 kB | bundle.min.js 1.25 MB | 673.75 kB | 646.76 kB | 166.76 kB | 163.73 kB | three.js @@ -22,5 +22,5 @@ Original | Minified | esbuild | Gzip | esbuild 6.69 MB | 2.42 MB | 2.31 MB | 503.16 kB | 488.28 kB | antd.js -10.95 MB | 3.57 MB | 3.49 MB | 912.37 kB | 915.50 kB | typescript.js +10.95 MB | 3.57 MB | 3.49 MB | 912.34 kB | 915.50 kB | typescript.js