Skip to content

[Bug] Legal comments are not respected when adjacent statements are eliminated #7257

@logaretm

Description

@logaretm

While using legal comments to preserve placeholders for later processing by JS plugins, I noticed that some legal comments gets removed based on the state of the proceeding statement, if the statement is preserved then it gets preserved otherwise it gets removed which breaks the block marking use-case.

Example

For example eliminating code blocks based on flags passed during build, or for start-marker/end-marker use-cases:

function doStuff() {
  /*! dev-only-start */
  if (someCondition) {
    doSomething();
  }
  /*! dev-only-end */

  const clientOptions = {
    enabled: true
  };

  return wrap(clientOptions);
}


export { doStuff }

Output

//#region main.ts
function doStuff() {
	/*! dev-only-start */
	if (someCondition) doSomething();
    // ⚠️ Missing end marker here
	return wrap({ enabled: true });
}

//#endregion
export { doStuff };

Ideally the legal marker should be preserved regardless of the next statement status.

Workarounds

Putting the end marker right before a statement works as long as the statement itself doesn't get "optimized", in my example the clientOptions gets inlined into the wrap call, which would remove the end marker if it was above it.

Only way for this to work is to put it above the return wrap(...) call since it doesn't get removed, but this is flimsy and assumes a lot about the output that may get changed under the hood at anytime.

Reproduction

https://repl.rolldown.rs/#eNp9UcFOg0AQ/ZVxL6VNC9XECwYv/QAT9ehlC7PtxmWXLIOtIfy7syxojY0QAjsz7817j14okfeiltqm1IZPK/Lv41qUfFKdLUk7C5V7oU6pZAn9mwXIVjdg8CDNpnR1jZY2LUlPsMpCVytIWlfjztlKB/iEgkDDdTpqe0iWD6E2XKdDW01k4SmdbQlKo7n11ATGFoqZE63cG6xyIN/hSMnE4e2ROm/h5GWT/MKGzbw33HhuHOvuZ4MwsHMUeeAa1sI7Yyp3sikrUPpwkdOVToxM1yMjfTbItM/T2Kx6AOVdDYsZvrgQUaGSnSFIepCNhmGZ/4EXj9H1ZG1KgDxie5TvGDNYx6rrqOkon4cAlPO15MIC23oxDUHMfRdjb7mprdEWWVdoDuMYZzXGlWXwqhseYnmpthWeIwVAURQgT1ITRPvJj8HliItXwLFZ9Dpsk+YfXHY5F0jEwP/jgwO+TbfpdrNHkun9nRi+AKi07Ow=

Alternatives

If this cannot be done with legal comments then we need a way to preserve certain comments, of course with exceptions regarding if their container/associated statement got tree-shaken out or not. in other words, I guess we need a way to preserve legal comments that precedes an empty line.

Or maybe introduce a block comment structure for marking regions/lines

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions