Skip to content

Incorrect treeshaking when passing detail to global CustomEvent #5122

Closed
@bluwy

Description

Rollup Version

3.28.1

Operating System (or Browser)

macos

Node Version (if applicable)

No response

Link To Reproduction

https://rollupjs.org/repl/?version=3.28.1&shareable=JTdCJTIyZXhhbXBsZSUyMiUzQSUyMiUyMiUyQyUyMm1vZHVsZXMlMjIlM0ElNUIlN0IlMjJjb2RlJTIyJTNBJTIyZXhwb3J0JTIwZnVuY3Rpb24lMjB0ZXN0KCklMjAlN0IlNUNuJTIwJTIwY29uc3QlMjBkZXRhaWwlMjAlM0QlMjAlN0J2YWx1ZSUzQSUyMG51bGwlN0QlNUNuJTIwJTIwY29uc3QlMjBjYW5jZWxlZCUyMCUzRCUyMGRpc3BhdGNoRXZlbnQobmV3JTIwQ3VzdG9tRXZlbnQoJ3Rlc3QnJTJDJTIwJTdCY2FuY2VsYWJsZSUzQSUyMHRydWUlMkMlMjBkZXRhaWwlN0QpKSU1Q24lMjAlMjBpZiUyMCghZGV0YWlsLnZhbHVlKSUyMHJldHVybiU1Q24lMjAlMjBjb25zb2xlLmxvZygndGhpcyUyMHNob3VsZCUyMG5vdCUyMGJlJTIwbWlzc2luZycpJTVDbiU3RCUyMiUyQyUyMmlzRW50cnklMjIlM0F0cnVlJTJDJTIybmFtZSUyMiUzQSUyMm1haW4uanMlMjIlN0QlNUQlMkMlMjJvcHRpb25zJTIyJTNBJTdCJTIyb3V0cHV0JTIyJTNBJTdCJTIyZm9ybWF0JTIyJTNBJTIyZXMlMjIlN0QlN0QlN0Q=

Expected Behaviour

When dispatching a CustomEvent like so:

const detail = {value: null}
const canceled = dispatchEvent(new CustomEvent('test', {cancelable: true, detail}))
if (!detail.value) return

The event listeners can mutate the detail causing detail.value to be truthy instead. Meaning the following if (!detail.value) return we can't know for sure if detail.value is falsy to treeshake away.

The console.log('this should not be missing') in the repro should not be treeshaken away.

Actual Behaviour

The console.log('this should not be missing') is treeshaken away incorrectly.

This was originally found in vitejs/vite#14298 and affects the package - https://github.com/github/text-expander-element/blob/f970a70434a53b3d1005617e2db57a31b72f2196/src/text-expander-element.ts#L123-L127

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions