Skip to content

Commit

Permalink
Fix for #1371/#1372 (#1374)
Browse files Browse the repository at this point in the history
* Fixes #1372

* Fixes #1371
  • Loading branch information
iccir committed Apr 10, 2023
1 parent 5e1b2ba commit 73c669e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/compress/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
AST_Arrow,
AST_BlockStatement,
AST_Call,
AST_Chain,
AST_Class,
AST_Const,
AST_Constant,
Expand Down Expand Up @@ -226,7 +227,8 @@ export function maintain_this_binding(parent, orig, val) {
parent instanceof AST_UnaryPrefix && parent.operator == "delete"
|| parent instanceof AST_Call && parent.expression === orig
&& (
val instanceof AST_PropAccess
val instanceof AST_Chain
|| val instanceof AST_PropAccess
|| val instanceof AST_SymbolRef && val.name == "eval"
)
) {
Expand Down
6 changes: 6 additions & 0 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,12 @@ function OutputStream(options) {
return true;
});

PARENS(AST_Chain, function(output) {
var p = output.parent();
if (!(p instanceof AST_Call || p instanceof AST_PropAccess)) return false;
return p.expression === this;
});

PARENS(AST_PropAccess, function(output) {
var p = output.parent();
if (p instanceof AST_New && p.expression === this) {
Expand Down
53 changes: 53 additions & 0 deletions test/compress/issue-t1371.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
issue_t1321: {
options = {
}
input: {
(foo?.bar()).baz = true;
((foo?.bar())).baz = true;
(foo?.bar)();
(foo?.bar).baz;
new (foo?.bar)();
(foo?.())();
(foo?.()).bar;
new (foo?.())();

(foo?.bar());
(foo?.bar) + 42;
(foo?.bar), 42;
}
expect: {
(foo?.bar()).baz = true;
(foo?.bar()).baz = true;
(foo?.bar)();
(foo?.bar).baz;
new (foo?.bar)();
(foo?.())();
(foo?.()).bar;
new (foo?.())();

foo?.bar();
foo?.bar + 42;
foo?.bar, 42;
}
}

issue_t1321_call_parentheses: {
options = {
}
input: {
(function(o) {
console.log(o.f("FAIL"), (o.f)("FAIL"), (0, o.f)(42));
console.log(o?.f("FAIL"), (o?.f)("FAIL"), (0, o?.f)(42));
})({
a: "PASS",
f(b) {
return this.a || b;
},
});
}
expect_exact: '(function(o){console.log(o.f("FAIL"),o.f("FAIL"),(0,o.f)(42));console.log(o?.f("FAIL"),(o?.f)("FAIL"),(0,o?.f)(42))})({a:"PASS",f(b){return this.a||b}});'
expect_stdout: [
"PASS PASS 42",
"PASS PASS 42",
]
}
29 changes: 29 additions & 0 deletions test/compress/issue-t1372.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

issue_t1372_maintain_this_binding: {
options = {
side_effects: true,
}
input: {
(function(o) {
console.log((0, o.f)("PASS"), (0, o?.f)("PASS"));
})({
a: "FAIL",
f(b) {
return this.a || b;
},
});
}
expect: {
(function(o) {
console.log((0, o.f)("PASS"), (0, o?.f)("PASS"));
})({
a: "FAIL",
f(b) {
return this.a || b;
},
});
}
expect_stdout: [
"PASS PASS",
]
}

0 comments on commit 73c669e

Please sign in to comment.