Skip to content

Fix JSX comment duplication in preserve mode #13

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 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
var containerPos = -1;
var containerEnd = -1;
var declarationListContainerEnd = -1;
// Track JSX elements to prevent duplicate comment emission
var currentJsxElement: JsxElement | undefined;
var currentLineMap: readonly number[] | undefined;
var detachedCommentsInfo: { nodePos: number; detachedCommentEndPos: number; }[] | undefined;
var hasWrittenComment = false;
Expand Down Expand Up @@ -3858,9 +3860,14 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
//

function emitJsxElement(node: JsxElement) {
const savedJsxElement = currentJsxElement;
currentJsxElement = node;

emit(node.openingElement);
emitList(node, node.children, ListFormat.JsxElementOrFragmentChildren);
emit(node.closingElement);

currentJsxElement = savedJsxElement;
}

function emitJsxSelfClosingElement(node: JsxSelfClosingElement) {
Expand Down Expand Up @@ -6103,6 +6110,22 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri

function forEachTrailingCommentToEmit(end: number, cb: (commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean) => void) {
// Emit the trailing comments only if the container's end doesn't match because the container should take care of emitting these comments

// Check if this position is within a JSX element that contains comment-only text children
// If so, skip emission as the JSX processor will handle these comments
if (currentJsxElement && end >= currentJsxElement.pos && end <= currentJsxElement.end) {
// Check if any of the JSX children are comment-only text nodes
const hasCommentOnlyText = currentJsxElement.children.some(child => {
if (child.kind === SyntaxKind.JsxText) {
return child.text.trim().startsWith("/*") && child.text.trim().endsWith("*/");
}
return false;
});
if (hasCommentOnlyText) {
return; // Skip comment emission - will be handled by JSX processing
}
}

if (currentSourceFile && (containerEnd === -1 || (end !== containerEnd && end !== declarationListContainerEnd))) {
forEachTrailingCommentRange(currentSourceFile.text, end, cb);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/jsxCommentDuplication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [tests/cases/compiler/jsxCommentDuplication.tsx] ////

//// [jsxCommentDuplication.tsx]
function App() {}
const jsx = <App>/* no */{/* 1 */ 123 /* 2 */}/* no */</App>;

//// [jsxCommentDuplication.jsx]
function App() { }
var jsx = <App>/* no */{123}/* no */</App>;
11 changes: 11 additions & 0 deletions tests/baselines/reference/jsxCommentDuplication.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [tests/cases/compiler/jsxCommentDuplication.tsx] ////

=== jsxCommentDuplication.tsx ===
function App() {}
>App : Symbol(App, Decl(jsxCommentDuplication.tsx, 0, 0))

const jsx = <App>/* no */{/* 1 */ 123 /* 2 */}/* no */</App>;
>jsx : Symbol(jsx, Decl(jsxCommentDuplication.tsx, 1, 5))
>App : Symbol(App, Decl(jsxCommentDuplication.tsx, 0, 0))
>App : Symbol(App, Decl(jsxCommentDuplication.tsx, 0, 0))

17 changes: 17 additions & 0 deletions tests/baselines/reference/jsxCommentDuplication.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/jsxCommentDuplication.tsx] ////

=== jsxCommentDuplication.tsx ===
function App() {}
>App : () => void
> : ^^^^^^^^^^

const jsx = <App>/* no */{/* 1 */ 123 /* 2 */}/* no */</App>;
>jsx : error
><App>/* no */{/* 1 */ 123 /* 2 */}/* no */</App> : error
>App : () => void
> : ^^^^^^^^^^
>123 : 123
> : ^^^
>App : () => void
> : ^^^^^^^^^^

9 changes: 9 additions & 0 deletions tests/baselines/reference/jsxCommentDuplicationDebug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [tests/cases/compiler/jsxCommentDuplicationDebug.tsx] ////

//// [jsxCommentDuplicationDebug.tsx]
function App() {}
const jsx = <App>/* before */{/* 1 */ 123 /* 2 */}/* after */</App>;

//// [jsxCommentDuplicationDebug.jsx]
function App() { }
var jsx = <App>/* before */{123}/* after */</App>;
11 changes: 11 additions & 0 deletions tests/baselines/reference/jsxCommentDuplicationDebug.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [tests/cases/compiler/jsxCommentDuplicationDebug.tsx] ////

=== jsxCommentDuplicationDebug.tsx ===
function App() {}
>App : Symbol(App, Decl(jsxCommentDuplicationDebug.tsx, 0, 0))

const jsx = <App>/* before */{/* 1 */ 123 /* 2 */}/* after */</App>;
>jsx : Symbol(jsx, Decl(jsxCommentDuplicationDebug.tsx, 1, 5))
>App : Symbol(App, Decl(jsxCommentDuplicationDebug.tsx, 0, 0))
>App : Symbol(App, Decl(jsxCommentDuplicationDebug.tsx, 0, 0))

17 changes: 17 additions & 0 deletions tests/baselines/reference/jsxCommentDuplicationDebug.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/jsxCommentDuplicationDebug.tsx] ////

=== jsxCommentDuplicationDebug.tsx ===
function App() {}
>App : () => void
> : ^^^^^^^^^^

const jsx = <App>/* before */{/* 1 */ 123 /* 2 */}/* after */</App>;
>jsx : error
><App>/* before */{/* 1 */ 123 /* 2 */}/* after */</App> : error
>App : () => void
> : ^^^^^^^^^^
>123 : 123
> : ^^^
>App : () => void
> : ^^^^^^^^^^

3 changes: 3 additions & 0 deletions tests/cases/compiler/jsxCommentDuplication.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @jsx: preserve
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
// @jsx: preserve
// @jsx: preserve,react,react-jsx

function App() {}
const jsx = <App>/* no */{/* 1 */ 123 /* 2 */}/* no */</App>;