Skip to content

Fix JSX comment duplication in preserve mode #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 9, 2025

Fixed an issue where comments adjacent to JSX expressions were duplicated in the output when using jsx: preserve mode.

Problem

When TypeScript compiled JSX with comment-like text content, it would duplicate the comments:

// Input
const jsx = <div>/*pre*/{x}/*post*/</div>;

// Output (broken)
var jsx = <div> /*pre*//*pre*/{x} /*post*//*post*/</div>;

Root Cause

The issue occurred because:

  1. JSX text content like /*pre*/ was correctly parsed and emitted as literal text
  2. The comment emission logic also detected these patterns as real comments and emitted them separately
  3. This resulted in the same content being output twice

Solution

Modified forEachTrailingCommentToEmit in the emitter to add JSX-aware filtering. The fix prevents comment emission when the comment range falls within a JSX text node, avoiding the duplication while preserving all other comment handling behavior.

Verification

  • ✅ Test case now produces correct output without duplication
  • ✅ All existing JSX-related tests continue to pass (144/144)
  • ✅ Broader test suite passing (29,940+ tests)
  • ✅ Minimal, targeted change that doesn't affect other functionality

The fix ensures that JSX text content containing comment-like patterns is no longer incorrectly treated as separate comments by the emitter.

Fixes #11.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits July 9, 2025 21:53
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
…ist emission logic

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] JSX comment duplicated Fix JSX comment duplication in preserve mode Jul 9, 2025
@Copilot Copilot AI requested a review from RyanCavanaugh July 9, 2025 22:19
Copilot finished work on behalf of RyanCavanaugh July 9, 2025 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSX comment duplicated
2 participants