Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codegen): print let[0] as (let)[0] #5947

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 8 additions & 26 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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("?.");
}
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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