@@ -22,7 +22,6 @@ export const lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
2222 if ( skip ) {
2323 continue ;
2424 }
25-
2625 // Import LingoComponent based on the module execution mode
2726 const packagePath =
2827 mode === "client" ? ModuleId . ReactClient : ModuleId . ReactRSC ;
@@ -31,101 +30,82 @@ export const lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
3130 exportedName : "LingoComponent" ,
3231 } ) ;
3332
34- // Preserve original scope before he JSX element is replaced with the lingo component
35- const originalJsxScope = _ . cloneDeep ( jsxScope ) ;
36-
3733 // Get the original JSX element name
3834 const originalJsxElementName = getJsxElementName ( jsxScope ) ;
3935 if ( ! originalJsxElementName ) {
4036 continue ;
4137 }
4238
43- // Replace the name with the lingo component
44- jsxScope . node . openingElement . name = t . jsxIdentifier (
45- lingoComponentImport . importedName ,
46- ) ;
47- if ( jsxScope . node . closingElement ) {
48- jsxScope . node . closingElement = null ;
49- jsxScope . node . children = [ ] ;
50- jsxScope . node . selfClosing = true ;
51- jsxScope . node . openingElement . selfClosing = true ;
52- }
39+ // Build new attributes array, preserving all original attributes
40+ const originalAttributes = jsxScope . node . openingElement . attributes . slice ( ) ;
5341
5442 // Add $as prop
5543 const as = / ^ [ A - Z ] / . test ( originalJsxElementName )
5644 ? t . jsxExpressionContainer ( t . identifier ( originalJsxElementName ) )
5745 : t . stringLiteral ( originalJsxElementName ) ;
58- jsxScope . node . openingElement . attributes . push (
59- t . jsxAttribute ( t . jsxIdentifier ( "$as" ) , as ) ,
60- ) ;
61-
46+ originalAttributes . push ( t . jsxAttribute ( t . jsxIdentifier ( "$as" ) , as ) ) ;
6247 // Add $fileKey prop
63- jsxScope . node . openingElement . attributes . push (
48+ originalAttributes . push (
6449 t . jsxAttribute (
6550 t . jsxIdentifier ( "$fileKey" ) ,
6651 t . stringLiteral ( payload . fileKey ) ,
6752 ) ,
6853 ) ;
69-
7054 // Add $entryKey prop
71- jsxScope . node . openingElement . attributes . push (
55+ originalAttributes . push (
7256 t . jsxAttribute (
7357 t . jsxIdentifier ( "$entryKey" ) ,
7458 t . stringLiteral ( getJsxScopeAttribute ( jsxScope ) ! ) ,
7559 ) ,
7660 ) ;
7761
7862 // Extract $variables from original JSX scope before lingo component was inserted
79- const $variables = getJsxVariables ( originalJsxScope ) ;
63+ const $variables = getJsxVariables ( jsxScope ) ;
8064 if ( $variables . properties . length > 0 ) {
81- jsxScope . node . openingElement . attributes . push (
65+ originalAttributes . push (
8266 t . jsxAttribute (
8367 t . jsxIdentifier ( "$variables" ) ,
8468 t . jsxExpressionContainer ( $variables ) ,
8569 ) ,
8670 ) ;
8771 }
88-
8972 // Extract nested JSX elements
90- const $elements = getNestedJsxElements ( originalJsxScope ) ;
73+ const $elements = getNestedJsxElements ( jsxScope ) ;
9174 if ( $elements . elements . length > 0 ) {
92- jsxScope . node . openingElement . attributes . push (
75+ originalAttributes . push (
9376 t . jsxAttribute (
9477 t . jsxIdentifier ( "$elements" ) ,
9578 t . jsxExpressionContainer ( $elements ) ,
9679 ) ,
9780 ) ;
9881 }
99-
10082 // Extract nested functions
101- const $functions = getJsxFunctions ( originalJsxScope ) ;
83+ const $functions = getJsxFunctions ( jsxScope ) ;
10284 if ( $functions . properties . length > 0 ) {
103- jsxScope . node . openingElement . attributes . push (
85+ originalAttributes . push (
10486 t . jsxAttribute (
10587 t . jsxIdentifier ( "$functions" ) ,
10688 t . jsxExpressionContainer ( $functions ) ,
10789 ) ,
10890 ) ;
10991 }
110-
11192 // Extract expressions
112- const $expressions = getJsxExpressions ( originalJsxScope ) ;
93+ const $expressions = getJsxExpressions ( jsxScope ) ;
11394 if ( $expressions . elements . length > 0 ) {
114- jsxScope . node . openingElement . attributes . push (
95+ originalAttributes . push (
11596 t . jsxAttribute (
11697 t . jsxIdentifier ( "$expressions" ) ,
11798 t . jsxExpressionContainer ( $expressions ) ,
11899 ) ,
119100 ) ;
120101 }
121-
122102 if ( mode === "server" ) {
123103 // Add $loadDictionary prop
124104 const loadDictionaryImport = getOrCreateImport ( payload . ast , {
125105 exportedName : "loadDictionary" ,
126106 moduleName : ModuleId . ReactRSC ,
127107 } ) ;
128- jsxScope . node . openingElement . attributes . push (
108+ originalAttributes . push (
129109 t . jsxAttribute (
130110 t . jsxIdentifier ( "$loadDictionary" ) ,
131111 t . jsxExpressionContainer (
@@ -140,6 +120,20 @@ export const lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
140120 ) ,
141121 ) ;
142122 }
123+
124+ // Create new JSXElement (self-closing)
125+ const newNode = t . jsxElement (
126+ t . jsxOpeningElement (
127+ t . jsxIdentifier ( lingoComponentImport . importedName ) ,
128+ originalAttributes ,
129+ true , // selfClosing
130+ ) ,
131+ null , // no closing element
132+ [ ] , // no children
133+ true , // selfClosing
134+ ) ;
135+
136+ jsxScope . replaceWith ( newNode ) ;
143137 }
144138
145139 return payload ;
0 commit comments