@@ -1249,12 +1249,12 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
1249
1249
var containerPos = - 1 ;
1250
1250
var containerEnd = - 1 ;
1251
1251
var declarationListContainerEnd = - 1 ;
1252
- // Track JSX elements to prevent duplicate comment emission
1253
- var currentJsxElement : JsxElement | undefined ;
1254
1252
var currentLineMap : readonly number [ ] | undefined ;
1255
1253
var detachedCommentsInfo : { nodePos : number ; detachedCommentEndPos : number ; } [ ] | undefined ;
1256
1254
var hasWrittenComment = false ;
1257
1255
var commentsDisabled = ! ! printerOptions . removeComments ;
1256
+ // Track JSX text ranges to prevent them from being treated as comments
1257
+ var jsxTextRanges : { start : number ; end : number ; } [ ] = [ ] ;
1258
1258
var lastSubstitution : Node | undefined ;
1259
1259
var currentParenthesizerRule : ParenthesizerRule < any > | undefined ;
1260
1260
var { enter : enterComment , exit : exitComment } = performance . createTimerIf ( extendedDiagnostics , "commentTime" , "beforeComment" , "afterComment" ) ;
@@ -1389,11 +1389,22 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
1389
1389
currentSourceFile = sourceFile ;
1390
1390
currentLineMap = undefined ;
1391
1391
detachedCommentsInfo = undefined ;
1392
+ jsxTextRanges = [ ] ; // Clear JSX text ranges for new source file
1393
+
1394
+ // Pre-collect all JSX text ranges before emission starts
1392
1395
if ( sourceFile ) {
1396
+ collectJsxTextRanges ( sourceFile ) ;
1393
1397
setSourceMapSource ( sourceFile ) ;
1394
1398
}
1395
1399
}
1396
1400
1401
+ function collectJsxTextRanges ( node : Node ) {
1402
+ if ( node . kind === SyntaxKind . JsxText ) {
1403
+ jsxTextRanges . push ( { start : node . pos , end : node . end } ) ;
1404
+ }
1405
+ forEachChild ( node , collectJsxTextRanges ) ;
1406
+ }
1407
+
1397
1408
function setWriter ( _writer : EmitTextWriter | undefined , _sourceMapGenerator : SourceMapGenerator | undefined ) {
1398
1409
if ( _writer && printerOptions . omitTrailingSemicolon ) {
1399
1410
_writer = getTrailingSemicolonDeferringWriter ( _writer ) ;
@@ -3860,14 +3871,9 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
3860
3871
//
3861
3872
3862
3873
function emitJsxElement ( node : JsxElement ) {
3863
- const savedJsxElement = currentJsxElement ;
3864
- currentJsxElement = node ;
3865
-
3866
3874
emit ( node . openingElement ) ;
3867
3875
emitList ( node , node . children , ListFormat . JsxElementOrFragmentChildren ) ;
3868
3876
emit ( node . closingElement ) ;
3869
-
3870
- currentJsxElement = savedJsxElement ;
3871
3877
}
3872
3878
3873
3879
function emitJsxSelfClosingElement ( node : JsxSelfClosingElement ) {
@@ -6103,31 +6109,27 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
6103
6109
forEachLeadingCommentWithoutDetachedComments ( cb ) ;
6104
6110
}
6105
6111
else {
6106
- forEachLeadingCommentRange ( currentSourceFile . text , pos , cb , /*state*/ pos ) ;
6112
+ forEachLeadingCommentRange ( currentSourceFile . text , pos , ( commentPos , commentEnd , kind , hasTrailingNewLine , rangePos ) => {
6113
+ // Check if this comment position falls within any JSX text range
6114
+ const isWithinJsxText = jsxTextRanges . some ( range => commentPos >= range . start && commentEnd <= range . end ) ;
6115
+ if ( ! isWithinJsxText ) {
6116
+ cb ( commentPos , commentEnd , kind , hasTrailingNewLine , rangePos ) ;
6117
+ }
6118
+ } , /*state*/ pos ) ;
6107
6119
}
6108
6120
}
6109
6121
}
6110
6122
6111
6123
function forEachTrailingCommentToEmit ( end : number , cb : ( commentPos : number , commentEnd : number , kind : SyntaxKind , hasTrailingNewLine : boolean ) => void ) {
6112
6124
// Emit the trailing comments only if the container's end doesn't match because the container should take care of emitting these comments
6113
-
6114
- // Check if this position is within a JSX element that contains comment-only text children
6115
- // If so, skip emission as the JSX processor will handle these comments
6116
- if ( currentJsxElement && end >= currentJsxElement . pos && end <= currentJsxElement . end ) {
6117
- // Check if any of the JSX children are comment-only text nodes
6118
- const hasCommentOnlyText = currentJsxElement . children . some ( child => {
6119
- if ( child . kind === SyntaxKind . JsxText ) {
6120
- return child . text . trim ( ) . startsWith ( "/*" ) && child . text . trim ( ) . endsWith ( "*/" ) ;
6125
+ if ( currentSourceFile && ( containerEnd === - 1 || ( end !== containerEnd && end !== declarationListContainerEnd ) ) ) {
6126
+ forEachTrailingCommentRange ( currentSourceFile . text , end , ( commentPos , commentEnd , kind , hasTrailingNewLine ) => {
6127
+ // Check if this comment position falls within any JSX text range
6128
+ const isWithinJsxText = jsxTextRanges . some ( range => commentPos >= range . start && commentEnd <= range . end ) ;
6129
+ if ( ! isWithinJsxText ) {
6130
+ cb ( commentPos , commentEnd , kind , hasTrailingNewLine ) ;
6121
6131
}
6122
- return false ;
6123
6132
} ) ;
6124
- if ( hasCommentOnlyText ) {
6125
- return ; // Skip comment emission - will be handled by JSX processing
6126
- }
6127
- }
6128
-
6129
- if ( currentSourceFile && ( containerEnd === - 1 || ( end !== containerEnd && end !== declarationListContainerEnd ) ) ) {
6130
- forEachTrailingCommentRange ( currentSourceFile . text , end , cb ) ;
6131
6133
}
6132
6134
}
6133
6135
@@ -6146,7 +6148,13 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
6146
6148
detachedCommentsInfo = undefined ;
6147
6149
}
6148
6150
6149
- forEachLeadingCommentRange ( currentSourceFile . text , pos , cb , /*state*/ pos ) ;
6151
+ forEachLeadingCommentRange ( currentSourceFile . text , pos , ( commentPos , commentEnd , kind , hasTrailingNewLine , rangePos ) => {
6152
+ // Check if this comment position falls within any JSX text range
6153
+ const isWithinJsxText = jsxTextRanges . some ( range => commentPos >= range . start && commentEnd <= range . end ) ;
6154
+ if ( ! isWithinJsxText ) {
6155
+ cb ( commentPos , commentEnd , kind , hasTrailingNewLine , rangePos ) ;
6156
+ }
6157
+ } , /*state*/ pos ) ;
6150
6158
}
6151
6159
6152
6160
function emitDetachedCommentsAndUpdateCommentsInfo ( range : TextRange ) {
0 commit comments