swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
30.93k stars 1.21k forks source link

preserveAllComments does not place comment close to original position #4686

Open smcenlly opened 2 years ago

smcenlly commented 2 years ago

Describe the bug

I understand that preserveAllComments follow a heuristic but it seems quite wrong in this case and leads to some weird behavior in our application that relies on comments to be close to the AST node that they were placed against. We were hoping you might be able to do something about this for us.

Input code

export function a() {
    globalThis //?
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": false,
      "dynamicImport": true
    },
    "transform": {
      "react": {
          "runtime": "automatic"
      }
    },
    "target": "es2018",
    "preserveAllComments": true
  },
  "sourceMaps": true
}

Playground link

https://play.swc.rs/?version=1.2.185&code=H4sIAAAAAAAAA0utKMgvKlFIK81LLsnMz1NI1NBUqOZSAIL0nPykxJyQjMxiBX19e65aAI2m0qAqAAAA&config=H4sIAAAAAAAAAz2OMQ7CMAxF7%2BK5AzChboiJgUNYxkVBdRzZDqKqencSEGxfT8%2F%2Fe4WHE4wrFDRn68mXHPiCEWIp7GSpBAwQ3lBY5QFuTGoYag7jhLN3tGSURBcpavH1tnZjmH1Sk15rjBSfUHMk4TaANVQwEsHWbbQ7NwPYD7v9sW0W4%2FbTk0%2FzfFYRzuH%2FbtdqxFcsP%2FQGyaRUqsoAAAA%3D

Expected behavior

export function a() {
    globalThis; //?
}

//# sourceMappingURL=file.js.map

Actual behavior

export function a() {
    globalThis;
} //?

//# sourceMappingURL=file.js.map

Version

@swc/cli@0.1.57, @swc/core@1.2.185

Additional context

No response

danr-za commented 1 year ago

And that's probably the reason /* istanbul ignore next */ and similar comments do not work