Skip to content

[Treeshaking] Dead branches of OR logical expressions leave a mapped empty space in the bundle #5578

@ericmorand

Description

@ericmorand

Rollup Version

v4.6.1

Operating System (or Browser)

Ubuntu

Node Version (if applicable)

No response

Link To Reproduction

https://rollupjs.org/repl/?version=4.18.1&shareable=JTdCJTIyZXhhbXBsZSUyMiUzQW51bGwlMkMlMjJtb2R1bGVzJTIyJTNBJTVCJTdCJTIyY29kZSUyMiUzQSUyMmV4cG9ydCUyMGNvbnN0JTIwZm9vJTIwJTNEJTIwdHJ1ZSUyMCU3QyU3QyUyMDUlMjAlN0MlN0MlMjAxMCUyMCU3QyU3QyUyMDIwJTNCJTIyJTJDJTIyaXNFbnRyeSUyMiUzQXRydWUlMkMlMjJuYW1lJTIyJTNBJTIybWFpbi5qcyUyMiU3RCU1RCUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlMjJvdXRwdXQlMjIlM0ElN0IlMjJmb3JtYXQlMjIlM0ElMjJlcyUyMiUyQyUyMnNvdXJjZW1hcCUyMiUzQXRydWUlN0QlMkMlMjJ0cmVlc2hha2UlMjIlM0F0cnVlJTdEJTdE

Expected Behaviour

When using treeshaking, the dead branches of OR logical expressions are totally absent from the bundle, and the source map.

Actual Behaviour

When using treeshaking, the dead branches of OR logical expressions are generated as an empty space that the source map maps back to them.

The fact that an empty space is generated in the bundle is not a big thing. But the fact that rollup declares this space as generated by the removed operands render impossible to detect treeshaked operands when parsing the source file. Typically, a code coverage tool would be incapable of knowing that the branches || 5 || 10 || 20 of the provided sample are uncovered because querying the source map about them would lead to a valid generated position.

Additionaly, this behavior is inconsistent with how rollup behaves with functions, that are not present neither in the bundle nor in the source map.

For covenience, a link to the source map visualization of the provided sample:

https://evanw.github.io/source-map-visualization/#NzQAY29uc3QgZm9vID0gdHJ1ZSAgIDsKCmV4cG9ydCB7IGZvbyB9OwovLyMgc291cmNlTWFwcGluZ1VSTD1idW5kbGUubWpzLm1hcAoxODMAeyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVuZGxlLm1qcyIsInNvdXJjZXMiOlsiaW5kZXgubWpzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBjb25zdCBmb28gPSB0cnVlIHx8IDUgfHwgMTAgfHwgMjA7Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFZLE1BQUMsR0FBRyxHQUFHLElBQUksQ0FBSyxDQUFNOzs7OyJ9

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions