Skip to content

Conversation

@majiayu000
Copy link

Summary

Fixes #17301

This PR adds support for outputting trailing legal comments on the same line as the statement they follow.

Before:

console.log('hello') //! Copyright notice

was output as:

console.log("hello");

After:

console.log("hello"); //! Copyright notice

Changes

  • Add has_legal_content() method to Comment for checking legal content without position requirement
  • Handle trailing legal comments in build_comments() using span.start as key
  • Add print_trailing_comments_after() to find and print trailing comments between statement end and next statement start
  • Modify print_directives_and_statements() to print trailing comments after each statement
  • Add pop() method to CodeBuffer for removing trailing newlines

Test plan

  • Added test case test_trailing_legal_comment
  • All existing codegen tests pass

Fixes oxc-project#17301

Before this fix, trailing legal comments (like `//! Copyright notice`)
after an expression on the same line were not output by the codegen.

Changes:
- Add `has_legal_content()` method to Comment for checking legal content
  without position requirement
- Handle trailing legal comments in `build_comments()` using span.start as key
- Add `print_trailing_comments_after()` to find and print trailing comments
  between statement end and next statement start
- Modify `print_directives_and_statements()` to print trailing comments
  after each statement
- Add `pop()` method to CodeBuffer for removing trailing newlines

Example:
```js
console.log('hello') //! Copyright notice
```

Now correctly outputs:
```js
console.log("hello"); //! Copyright notice
```

Signed-off-by: majiayu000 <[email protected]>
@github-actions github-actions bot added A-ast Area - AST A-codegen Area - Code Generation C-bug Category - Bug labels Dec 25, 2025
@Boshen Boshen self-assigned this Dec 25, 2025
Copy link
Member

@Boshen Boshen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@majiayu000
Copy link
Author

Hi @Boshen, thanks for the review.

Yes, I used AI to assist with this PR. I've thoroughly reviewed and tested the code:

Testing performed:

  • ✅ Basic trailing legal comment (//!)
  • ✅ Multiple statements with trailing legal comments
  • ✅ Block statements (if/for/while) with trailing comments
  • ✅ Function/class/arrow function with trailing comments
  • ✅ Last statement trailing comment
  • ✅ Normal comments correctly NOT preserved

All 10 test cases pass.

Note: Block comment form (/*! */, /* @license */) for trailing comments is not supported because the parser classifies them as Leading rather than Trailing. This is outside the scope of #17301 which only uses line comments (//!).

Happy to make any adjustments if needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast Area - AST A-codegen Area - Code Generation C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

codegen: comment on the same line of an expression is not output

2 participants