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 all 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.
58 changes: 55 additions & 3 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
var detachedCommentsInfo: { nodePos: number; detachedCommentEndPos: number; }[] | undefined;
var hasWrittenComment = false;
var commentsDisabled = !!printerOptions.removeComments;
var jsxTextPositionCache: Map<number, boolean> | undefined;
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,6 +1388,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
currentSourceFile = sourceFile;
currentLineMap = undefined;
detachedCommentsInfo = undefined;
jsxTextPositionCache = undefined; // Clear cache for new source file

if (sourceFile) {
setSourceMapSource(sourceFile);
}
Expand All @@ -1402,6 +1405,40 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
sourceMapsDisabled = !writer || !sourceMapGenerator;
}

function isPositionInJsxText(pos: number): boolean {
if (!currentSourceFile) return false;

// Use cache if available
if (!jsxTextPositionCache) {
jsxTextPositionCache = new Map();
}

if (jsxTextPositionCache.has(pos)) {
return jsxTextPositionCache.get(pos)!;
}

// Walk the AST to find if this position is within a JSX text node
let found = false;

function visitNode(node: Node): void {
if (found) return;

if (node.kind === SyntaxKind.JsxText && pos >= node.pos && pos < node.end) {
found = true;
return;
}

// Only continue traversing if this node might contain the position
if (pos >= node.pos && pos < node.end) {
forEachChild(node, visitNode);
}
}

visitNode(currentSourceFile);
jsxTextPositionCache.set(pos, found);
return found;
}

function reset() {
nodeIdToGeneratedName = [];
nodeIdToGeneratedPrivateName = [];
Expand Down Expand Up @@ -6096,15 +6133,25 @@ 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) => {
// Skip comments that are within JSX text nodes
if (!isPositionInJsxText(commentPos)) {
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) => {
// Skip comments that are within JSX text nodes
if (!isPositionInJsxText(commentPos)) {
cb(commentPos, commentEnd, kind, hasTrailingNewLine);
}
});
}
}

Expand All @@ -6123,7 +6170,12 @@ 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) => {
// Skip comments that are within JSX text nodes
if (!isPositionInJsxText(commentPos)) {
cb(commentPos, commentEnd, kind, hasTrailingNewLine, rangePos);
}
}, /*state*/ pos);
}

function emitDetachedCommentsAndUpdateCommentsInfo(range: TextRange) {
Expand Down
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>;
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))

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
> : ^^^^^^^^^^

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
jsxCommentDuplication.tsx(2,14): error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.


==== jsxCommentDuplication.tsx (1 errors) ====
function App() {}
const jsx = <App>/* no */{/* 1 */ 123 /* 2 */}/* no */</App>;
~~~
!!! error TS2874: This JSX tag requires 'React' to be in scope, but it could not be found.
12 changes: 12 additions & 0 deletions tests/baselines/reference/jsxCommentDuplication(jsx=react).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [tests/cases/compiler/jsxCommentDuplication.tsx] ////

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

//// [jsxCommentDuplication.js]
function App() { }
var jsx = React.createElement(App, null,
"/* no */", /* 1 */
123 /* 2 */,
"/* no */");
11 changes: 11 additions & 0 deletions tests/baselines/reference/jsxCommentDuplication(jsx=react).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))

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
jsxCommentDuplication.tsx(2,13): error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.


==== jsxCommentDuplication.tsx (1 errors) ====
function App() {}
const jsx = <App>/* no */{/* 1 */ 123 /* 2 */}/* no */</App>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
12 changes: 12 additions & 0 deletions tests/baselines/reference/jsxCommentDuplication(jsx=react-jsx).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [tests/cases/compiler/jsxCommentDuplication.tsx] ////

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

//// [jsxCommentDuplication.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var jsx_runtime_1 = require("react/jsx-runtime");
function App() { }
var jsx = (0, jsx_runtime_1.jsxs)(App, { children: ["/* no */", /* 1 */ 123 /* 2 */, "/* no */"] });
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))

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [tests/cases/compiler/jsxCommentDuplication.tsx] ////

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

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

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,react,react-jsx
function App() {}
const jsx = <App>/* no */{/* 1 */ 123 /* 2 */}/* no */</App>;