Skip to content

Fix JSX comment duplication in both React and preserve modes #12

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
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
25 changes: 23 additions & 2 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3720,6 +3720,9 @@ export function createScanner(
// These initial values are special because the first line is:
// firstNonWhitespace = 0 to indicate that we want leading whitespace,

// Build the text content excluding comments
let textContent = "";

while (pos < end) {
char = charCodeUnchecked(pos);
if (char === CharacterCodes.openBrace) {
Expand All @@ -3739,6 +3742,21 @@ export function createScanner(
error(Diagnostics.Unexpected_token_Did_you_mean_or_rbrace, pos, 1);
}

// Handle comments - skip /* ... */ style comments
if (char === CharacterCodes.slash && charCodeUnchecked(pos + 1) === CharacterCodes.asterisk) {
pos += 2; // Skip /*
// Find the end of the comment
while (pos < end) {
if (charCodeUnchecked(pos) === CharacterCodes.asterisk && charCodeUnchecked(pos + 1) === CharacterCodes.slash) {
pos += 2; // Skip */
break;
}
pos++;
}
// Continue without adding comment content to textContent
continue;
}

// FirstNonWhitespace is 0, then we only see whitespaces so far. If we see a linebreak, we want to ignore that whitespaces.
// i.e (- : whitespace)
// <div>----
Expand All @@ -3754,13 +3772,16 @@ export function createScanner(
break;
}
else if (!isWhiteSpaceLike(char)) {
firstNonWhitespace = pos;
if (firstNonWhitespace === 0) {
firstNonWhitespace = textContent.length;
}
}

textContent += String.fromCharCode(char);
pos++;
}

tokenValue = text.substring(fullStartPos, pos);
tokenValue = textContent;

return firstNonWhitespace === -1 ? SyntaxKind.JsxTextAllWhiteSpaces : SyntaxKind.JsxText;
}
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,
"", /* 1 */
123 /* 2 */,
"");
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
> : ^^^^^^^^^^

5 changes: 5 additions & 0 deletions tests/cases/compiler/jsxCommentDuplication.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @jsx: react,preserve
// @module: commonjs

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