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 for #1371/#1372 #1374

Merged
merged 2 commits into from
Apr 10, 2023
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
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",
]
}