oxc-project / oxc

⚓ A collection of JavaScript tools written in Rust.
https://oxc.rs
MIT License
12.22k stars 442 forks source link

[Compressor|RemoveDeadCode]: Wrong simplified expression in the tagged template #4341

Closed hyf0 closed 1 month ago

hyf0 commented 3 months ago

For input

let o = {
    f() {
        assert.ok(this !== o);
    }
};
(1, o.f)();
(1, o.f)``;

(true && o.f)();
(true && o.f)``;

(true ? o.f : false)();
(true ? o.f : false)``;

Esbuild's output

let o={f(){assert.ok(this!==o)}};(0,o.f)(),(0,o.f)``,(0,o.f)(),(0,o.f)``,(0,o.f)(),(0,o.f)``;

https://esbuild.github.io/try/#dAAwLjIzLjAALS1taW5pZnkAbGV0IG8gPSB7CglmKCkgewoJCWFzc2VydC5vayh0aGlzICE9PSBvKTsKCX0KfTsKKDEsIG8uZikoKTsKKDEsIG8uZilgYDsKCih0cnVlICYmIG8uZikoKTsKKHRydWUgJiYgby5mKWBgOwoKKHRydWUgPyBvLmYgOiBmYWxzZSkoKTsKKHRydWUgPyBvLmYgOiBmYWxzZSlgYDs

Oxc's output

let o = {f() {
    assert.ok(this !== o);
}};
(1, o.f)();
(1, o.f)``;
o.f();
o.f``;
(!0 ? o.f : !1)();
(!0 ? o.f : !1)``;

https://oxc-project.github.io/oxc/playground/?code=3YCAAIAbgICAgICAgIC2mcpqpUVBuJ3I%2BHiR31d004qj5Bgli8IXp4uXuyULHD9UOmdTBRJYluRQbf8p3kvgYoXFzfbW6%2BJ5XvxWlXKndcO6VfFc%2BARAdtxe7ZllDHqLf%2F0dYTa9eCxUx6PSbiS%2FfcMYwA%3D%3D


In short, (true && o.f)``; is simplify to o.f`` , which causes this pointing to a wrong object..

Related:

Boshen commented 2 months ago

Oh this is harder than I thought ... the above PR doesn't fix call expressions :-/ I'll revisit this sometime in the future.