11const fs = require ( 'fs' )
22const prettier = require ( 'prettier' )
3+ const { default : generate } = require ( '@babel/generator' )
34
45function babelPluginReactLive ( babel , options ) {
56 const { types : t } = babel
@@ -26,6 +27,7 @@ function babelPluginReactLive(babel, options) {
2627 // And we also escape `
2728 code = code . replace ( / ^ ; / , '' ) . replace ( / ` / g, '\\`' )
2829
30+ // Remove fragments we added in the first place
2931 if ( children . length > 1 ) {
3032 code = code . replace ( / ^ < > | < \/ > $ | ^ \s { 2 } / gm, '' )
3133 }
@@ -65,24 +67,21 @@ function babelPluginReactLive(babel, options) {
6567 path . traverse ( {
6668 ReturnStatement ( path ) {
6769 if ( currentReturnStatement === path . node ) {
68- const code = path
69- . getSource ( )
70- . replace ( / r e t u r n ( ( .| \n | \r ) * ) / , 'render($1)' )
70+ const code = astToCode ( path . node )
7171
72- const node = t . identifier ( code )
72+ const node = t . identifier (
73+ code . replace ( / r e t u r n ( ( .| \n | \r ) * ) / , 'render($1)' )
74+ )
7375 path . replaceWith ( node )
7476
7577 path . stop ( )
7678 }
7779 } ,
7880 } )
7981
80- const code = path
81- . toString ( )
82- . replace ( / ^ \{ / , '' )
83- . replace ( / \} $ / , '' )
82+ const code = astToCode ( path . node )
8483
85- children . push ( code )
84+ children . push ( code . replace ( / ^ \{ / , '' ) . replace ( / \} $ / , '' ) )
8685
8786 path . stop ( )
8887
@@ -91,21 +90,21 @@ function babelPluginReactLive(babel, options) {
9190 } ,
9291 JSXElement ( path ) {
9392 if ( rootPath === path . parentPath ) {
94- const code = path . getSource ( )
93+ const code = astToCode ( path . node )
9594
9695 children . push ( code )
9796 }
9897 } ,
9998 JSXFragment ( path ) {
10099 if ( rootPath === path . parentPath ) {
101- const code = path . getSource ( )
100+ const code = astToCode ( path . node )
102101
103102 children . push ( code )
104103 }
105104 } ,
106105 JSXText ( path ) {
107106 if ( rootPath === path . parentPath ) {
108- const code = path . getSource ( )
107+ const code = astToCode ( path . node )
109108
110109 if ( code . trim ( ) . length ) {
111110 children . push ( code )
@@ -117,7 +116,7 @@ function babelPluginReactLive(babel, options) {
117116 rootPath === path . parentPath &&
118117 path . node . expression . type !== 'ArrowFunctionExpression'
119118 ) {
120- const code = path . getSource ( )
119+ const code = astToCode ( path . node )
121120
122121 if ( code . length ) {
123122 children . push ( code )
@@ -140,7 +139,8 @@ function babelPluginReactLive(babel, options) {
140139
141140 path . node . children = [ convertCodeToJSX ( children ) ]
142141
143- path . replaceWith ( t . identifier ( path . toString ( ) ) )
142+ const code = astToCode ( path . node )
143+ path . replaceWith ( t . identifier ( code ) )
144144 }
145145 }
146146 } ,
@@ -161,4 +161,10 @@ function babelPluginReactLive(babel, options) {
161161 }
162162}
163163
164+ function astToCode ( ast ) {
165+ const { code } = generate ( ast )
166+
167+ return code . replace ( / ; $ / , '' )
168+ }
169+
164170module . exports = babelPluginReactLive
0 commit comments