oxc-project / oxc

⚓ A collection of JavaScript tools written in Rust.
https://oxc.rs
MIT License
12.39k stars 451 forks source link

codegen: print esbuild trailing legal comments #7190

Open hyf0 opened 6 days ago

hyf0 commented 6 days ago
//! <script>foo</script>
export let x

console.log('in a') //! Copyright notice 1

console.log('in b') //! Copyright notice 1

console.log('in c') //! Copyright notice 2

export default () => {
    /**
     * @preserve
     */
}

output(pure codeggen, without bundling)


//! <script>foo</script>
export let x;
console.log("in a");
console.log("in b");
console.log("in c");
export default () => {};

esbuild

// entry.js
//! <script>foo<\/script>
var x;
console.log("in a");
//! Copyright notice 1
console.log("in b");
//! Copyright notice 1
console.log("in c");
//! Copyright notice 2
var entry_default = () => {
  /**
   * @preserve
   */
};
export {
  entry_default as default,
  x
};
hyf0 commented 6 days ago

It would be nice if the codegen part of playground support comment-related options.

Boshen commented 2 days ago

I fixed the empty body case.

The console.log('in a') //! Copyright notice 1 case contradicts to the documentation https://esbuild.github.io/api/#legal-comments

Note that "statement-level" for JS and "rule-level" for CSS means the comment must appear in a context where multiple statements or rules are allowed such as in the top-level scope or in a statement or rule block. So comments inside expressions or at the declaration level are not considered legal comments.

esbuild's parser saved these trailing comments as statement level comments

https://github.com/evanw/esbuild/blob/9eca46464ed5615cb36a3beb3f7a7b9a8ffbe7cf/internal/js_parser/js_parser.go#L8154-L8166

I'm undecided whether we should handle such cases. It takes a lot more work to handle such cases.

hyf0 commented 2 days ago

Since printing legal comments only have a pratical standard from esbuild, I guess we could wait for users feedback and decide what's next.