swc-project / swc

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

[`preserveAllComments`] Pure annotations are placed in the wrong position #7735

Open JSerFeng opened 1 year ago

JSerFeng commented 1 year ago

Describe the bug

SWC injects pure annotation right before the async polyfill call, however if async arrow function is been assigned, the PURE annotation appear in the wrong position.

Why does this matter

Because if Pure appears before the import statements, when this code is processed by webpack, if this module's dependencies are all concatenated, then the import statements are gone, and the code will be like this

/*#__PURE__*/ 
setSomeEffect()

As you can see the PURE annotation points that setSomeEffect call is pure which is not, and removes this call expression after minimization.

Input code

setSomeEffect()

// 123
const a = async () => {
};

function rightPlace() {
    // 123
    async () => {
    };
}

Config

{
  "jsc": {
    "externalHelpers": true,
    "parser": {
      "tsx": true,
      "syntax": "typescript",
    },
    "preserveAllComments": true,
    "target": "es5"
  },
}

Playground link

https://play.swc.rs/?version=1.3.71&code=H4sIAAAAAAAAAytOLQnOz011TUtLTS7R0OTi0tdXMDQy5krOzysuUUhUsFVILK7MS1bQ0FSwtVOo5qq15uJKK81LLsnMz1MoykzPKAnISUxOBUpXcykAAVQ7iImqESQC1FwLAKBlQRFxAAAA&config=H4sIAAAAAAAAA12NwQqDQAxE737FkrNXL95KL%2F2NRQaxrNslGUUR%2F72xUqG9JW8mL1sVgjytkzZsPvqChdAc0wOpQM0D6oT6DEtUg15lJ7Tlp%2BLI1sx4UOFaYJ0OhXKm%2B9ejcM%2BMW0r31zgi8%2B8Ro%2Fbg4YA1Un0u9zcWcwxJrQAAAA%3D%3D

Expected behavior

Pure annotation should appear inside the assignment expr, like the one inside rightPlace function body

Actual behavior

Pure annotation appears at the top of the module

Version

1.3.71

Additional context

No response

danieltroger commented 1 year ago

For me (using SWC as part of parcel) pure comments are not respected at all right now. Not sure if it's this issue or https://github.com/parcel-bundler/parcel/issues/9111?

EDIT: Some actually are but some are "eaten up" even when building with --no-optimize and checking the output, weird. I found a good workaround though in the meantime - wrote a custom babel transformer that changes (0, FUNCTION)(...) back to FUNCTION(...) because my functions don't care about this anyways and now my output bundle is 50KB smaller