namespace ts . codefix { const fixID = "wrapJsxInFragment" ; const errorCodes = [ Diagnostics . JSX_expressions_must_have_one_parent_element . code ] ; registerCodeFix ( { errorCodes , getCodeActions : function getCodeActionsToWrapJsxInFragment ( context ) { const { sourceFile , span } = context ; const node = findNodeToFix ( sourceFile , span . start ) ; if ( ! node ) return undefined ; const changes = textChanges . ChangeTracker . with ( context , t => doChange ( t , sourceFile , node ) ) ; return [ createCodeFixAction ( fixID , changes , Diagnostics . Wrap_in_JSX_fragment , fixID , Diagnostics . Wrap_all_unparented_JSX_in_JSX_fragment ) ] ; } , fixIds : [ fixID ] , getAllCodeActions : context => codeFixAll ( context , errorCodes , ( changes , diag ) => { const node = findNodeToFix ( context . sourceFile , diag . start ) ; if ( ! node ) return undefined ; doChange ( changes , context . sourceFile , node ) ; } ) , } ) ; function findNodeToFix ( sourceFile , pos ) { const lessThanToken = getTokenAtPosition ( sourceFile , pos ) ; const firstJsxElementOrOpenElement = lessThanToken . parent ; let binaryExpr = firstJsxElementOrOpenElement . parent ; if ( ! isBinaryExpression ( binaryExpr ) ) { binaryExpr = binaryExpr . parent ; if ( ! isBinaryExpression ( binaryExpr ) ) return undefined ; } if ( ! nodeIsMissing ( binaryExpr . operatorToken ) ) return undefined ; return binaryExpr ; } function doChange ( changeTracker , sf , node ) { const jsx = flattenInvalidBinaryExpr ( node ) ; if ( jsx ) changeTracker . replaceNode ( sf , node , factory . createJsxFragment ( factory . createJsxOpeningFragment ( ) , jsx , factory . createJsxJsxClosingFragment ( ) ) ) ; } function flattenInvalidBinaryExpr ( node ) { const children = [ ] ; let current = node ; while ( true ) { if ( isBinaryExpression ( current ) && nodeIsMissing ( current . operatorToken ) && current . operatorToken . kind === SyntaxKind . CommaToken ) { children . push ( current . left as JsxChild ) ; if ( isJsxChild ( current . right ) ) { children . push ( current . right ) ; return children ; } else if ( isBinaryExpression ( current . right ) ) { current = current . right ; continue ; } else return undefined ; } else return undefined ; } } }