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 4 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
7 changes: 5 additions & 2 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3896,8 +3896,11 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
writePunctuation(">");
}

function emitJsxText(node: JsxText) {
writer.writeLiteral(node.text);
function emitJsxText(node: JsxText) {
// Strip comments from JSX text content to prevent duplication
// Comments like /* comment */ should not be treated as text content in JSX
const textWithoutComments = node.text.replace(/\/\*[\s\S]*?\*\//g, '');
writer.writeLiteral(textWithoutComments);
}

function emitJsxClosingElementOrFragment(node: JsxClosingElement | JsxClosingFragment) {
Expand Down
47 changes: 31 additions & 16 deletions src/compiler/transformers/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,22 +554,37 @@ export function transformJsx(context: TransformationContext): (x: SourceFile | B
return fixed === undefined ? undefined : factory.createStringLiteral(fixed);
}

/**
* JSX trims whitespace at the end and beginning of lines, except that the
* start/end of a tag is considered a start/end of a line only if that line is
* on the same line as the closing tag. See examples in
* tests/cases/conformance/jsx/tsxReactEmitWhitespace.tsx
* See also https://www.w3.org/TR/html4/struct/text.html#h-9.1 and https://www.w3.org/TR/CSS2/text.html#white-space-model
*
* An equivalent algorithm would be:
* - If there is only one line, return it.
* - If there is only whitespace (but multiple lines), return `undefined`.
* - Split the text into lines.
* - 'trimRight' the first line, 'trimLeft' the last line, 'trim' middle lines.
* - Decode entities on each line (individually).
* - Remove empty lines and join the rest with " ".
*/
function fixupWhitespaceAndDecodeEntities(text: string): string | undefined {
/**
* Remove comments from JSX text content.
* Comments like slash-star comment star-slash should not be treated as text content in JSX.
*/
function stripCommentsFromJsxText(text: string): string {
// Only strip comments when not in preserve mode
if (compilerOptions.jsx === JsxEmit.Preserve) {
return text;
}
// Remove /* ... */ style comments from JSX text
return text.replace(/\/\*[\s\S]*?\*\//g, '');
}

/**
* JSX trims whitespace at the end and beginning of lines, except that the
* start/end of a tag is considered a start/end of a line only if that line is
* on the same line as the closing tag. See examples in
* tests/cases/conformance/jsx/tsxReactEmitWhitespace.tsx
* See also https://www.w3.org/TR/html4/struct/text.html#h-9.1 and https://www.w3.org/TR/CSS2/text.html#white-space-model
*
* An equivalent algorithm would be:
* - If there is only one line, return it.
* - If there is only whitespace (but multiple lines), return `undefined`.
* - Split the text into lines.
* - 'trimRight' the first line, 'trimLeft' the last line, 'trim' middle lines.
* - Decode entities on each line (individually).
* - Remove empty lines and join the rest with " ".
*/
function fixupWhitespaceAndDecodeEntities(text: string): string | undefined {
// First, strip comments from the text
text = stripCommentsFromJsxText(text);
let acc: string | undefined;
// First non-whitespace character on this line.
let firstNonWhitespace = 0;
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>;