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 7 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
9 changes: 5 additions & 4 deletions .github/copilot-questions.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Questions I have that I think the developers of this project can help me with:
* How does control flow analysis represent a circular graph? I checked the documentation server for "cfa" and "control flow"
* How do I tell if a symbol is in the global scope? I checked the documentation server for topics referencing "symbol" and "global"
* What is an `EscapedName`, exactly?
Questions I have that I think the developers of this project can help me with:
* How does control flow analysis represent a circular graph? I checked the documentation server for "cfa" and "control flow"
* How do I tell if a symbol is in the global scope? I checked the documentation server for topics referencing "symbol" and "global"
* What is an `EscapedName`, exactly?
* How does TypeScript distinguish between comments inside JSX text vs regular TypeScript comments? I searched for "JSX text comments emitter forEachTrailingCommentRange JSX text ranges" - The issue was that comment iteration doesn't understand JSX context and treats JSX text content as regular comments.
37 changes: 34 additions & 3 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
var detachedCommentsInfo: { nodePos: number; detachedCommentEndPos: number; }[] | undefined;
var hasWrittenComment = false;
var commentsDisabled = !!printerOptions.removeComments;
// Track JSX text ranges to prevent them from being treated as comments
var jsxTextRanges: { start: number; end: number; }[] = [];
var lastSubstitution: Node | undefined;
var currentParenthesizerRule: ParenthesizerRule<any> | undefined;
var { enter: enterComment, exit: exitComment } = performance.createTimerIf(extendedDiagnostics, "commentTime", "beforeComment", "afterComment");
Expand Down Expand Up @@ -1387,11 +1389,22 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
currentSourceFile = sourceFile;
currentLineMap = undefined;
detachedCommentsInfo = undefined;
jsxTextRanges = []; // Clear JSX text ranges for new source file

// Pre-collect all JSX text ranges before emission starts
if (sourceFile) {
collectJsxTextRanges(sourceFile);
setSourceMapSource(sourceFile);
}
}

function collectJsxTextRanges(node: Node) {
if (node.kind === SyntaxKind.JsxText) {
jsxTextRanges.push({ start: node.pos, end: node.end });
}
forEachChild(node, collectJsxTextRanges);
}

function setWriter(_writer: EmitTextWriter | undefined, _sourceMapGenerator: SourceMapGenerator | undefined) {
if (_writer && printerOptions.omitTrailingSemicolon) {
_writer = getTrailingSemicolonDeferringWriter(_writer);
Expand Down Expand Up @@ -6096,15 +6109,27 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
forEachLeadingCommentWithoutDetachedComments(cb);
}
else {
forEachLeadingCommentRange(currentSourceFile.text, pos, cb, /*state*/ pos);
forEachLeadingCommentRange(currentSourceFile.text, pos, (commentPos, commentEnd, kind, hasTrailingNewLine, rangePos) => {
// Check if this comment position falls within any JSX text range
const isWithinJsxText = jsxTextRanges.some(range => commentPos >= range.start && commentEnd <= range.end);
if (!isWithinJsxText) {
cb(commentPos, commentEnd, kind, hasTrailingNewLine, rangePos);
}
}, /*state*/ pos);
}
}
}

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
if (currentSourceFile && (containerEnd === -1 || (end !== containerEnd && end !== declarationListContainerEnd))) {
forEachTrailingCommentRange(currentSourceFile.text, end, cb);
forEachTrailingCommentRange(currentSourceFile.text, end, (commentPos, commentEnd, kind, hasTrailingNewLine) => {
// Check if this comment position falls within any JSX text range
const isWithinJsxText = jsxTextRanges.some(range => commentPos >= range.start && commentEnd <= range.end);
if (!isWithinJsxText) {
cb(commentPos, commentEnd, kind, hasTrailingNewLine);
}
});
}
}

Expand All @@ -6123,7 +6148,13 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
detachedCommentsInfo = undefined;
}

forEachLeadingCommentRange(currentSourceFile.text, pos, cb, /*state*/ pos);
forEachLeadingCommentRange(currentSourceFile.text, pos, (commentPos, commentEnd, kind, hasTrailingNewLine, rangePos) => {
// Check if this comment position falls within any JSX text range
const isWithinJsxText = jsxTextRanges.some(range => commentPos >= range.start && commentEnd <= range.end);
if (!isWithinJsxText) {
cb(commentPos, commentEnd, kind, hasTrailingNewLine, rangePos);
}
}, /*state*/ pos);
}

function emitDetachedCommentsAndUpdateCommentsInfo(range: TextRange) {
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 */{/* 1 */123 /* 2 */}/* 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
> : ^^^^^^^^^^

9 changes: 3 additions & 6 deletions tests/baselines/reference/tsxAttributeResolution14.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ function VerticalNavMenuItem(prop) {
}
function VerticalNav() {
return (<div>
<VerticalNavMenuItem primaryText={2}/> // error
// error
<VerticalNavMenuItem justRandomProp={2} primaryText={"hello"}/> // ok
// ok
<VerticalNavMenuItem justRandomProp1={true} primaryText={"hello"}/> // error
// error
<VerticalNavMenuItem primaryText={2}/> // error
<VerticalNavMenuItem justRandomProp={2} primaryText={"hello"}/> // ok
<VerticalNavMenuItem justRandomProp1={true} primaryText={"hello"}/> // error
</div>);
}
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>;