@@ -21,7 +21,8 @@ export const renderJsSourceCode = (code) => {
2121 // (, )? - optional comma after React - some files, like tests, only need the main React import
2222 // ({([^]+?)})? - optionally capture any characters (including newlines) between the import braces
2323 // from 'react'; - ` from 'react';` exactly
24- / i m p o r t ( R e a c t ) ? ( , ) ? ( { ( [ ^ ] + ?) } ) ? f r o m ' r e a c t ' ; / ,
24+ // [\r\n] - match end of line
25+ / i m p o r t ( R e a c t ) ? ( , ) ? ( { ( [ ^ ] + ?) } ) ? f r o m ' r e a c t ' ; [ \r \n ] / ,
2526 ( match ) => {
2627 reactImport = match ;
2728 return '' ;
@@ -35,12 +36,11 @@ export const renderJsSourceCode = (code) => {
3536
3637 // Find all imports that come from '@elastic/eui'
3738 renderedCode = renderedCode . replace (
38- // [\r\n] - start of a line
3939 // import\s+\{ - import / whitespace / opening brace
4040 // ([^}]+) - group together anything that isn't a closing brace
4141 // \}\s+from\s+'@elastic\/eui'; - closing brace / whitespace / from / whitespace / '@elastic/eui';
4242 // [\r\n] - match end of line, so the extra new line is removed via the replace operation
43- / [ \r \n ] i m p o r t \s + \{ ( [ ^ } ] + ) \} \s + f r o m \s + ' @ e l a s t i c \/ e u i ' ; / g,
43+ / i m p o r t \s + \{ ( [ ^ } ] + ) \} \s + f r o m \s + ' @ e l a s t i c \/ e u i ' ; [ \r \n ] / g,
4444 ( match , imports ) => {
4545 // remove any additional characters from imports
4646 const namedImports = imports . match ( / [ a - z A - Z 0 - 9 ] + / g) ;
@@ -83,11 +83,14 @@ export const renderJsSourceCode = (code) => {
8383 }
8484 ) ;
8585
86- const formattedRemainingImports = remainingImports . join ( '\n' ) ;
86+ const formattedRemainingImports = remainingImports . reduce (
87+ ( imports , importString ) => ( imports += `\n${ importString } ` ) ,
88+ ''
89+ ) ;
8790
8891 /**
8992 * Putting it all together
9093 */
91- const fullyFormattedCode = `${ reactImport } \n ${ formattedEuiImports } \n ${ formattedRemainingImports } \n\n${ renderedCode . trim ( ) } ` ;
94+ const fullyFormattedCode = `${ reactImport } ${ formattedEuiImports } ${ formattedRemainingImports } \n\n${ renderedCode . trim ( ) } ` ;
9295 return fullyFormattedCode ;
9396} ;
0 commit comments