1
- import { type ParserPlugin , parse } from '@babel/parser'
2
- import { walk } from 'estree-walker'
3
- import { encode } from 'entities'
4
- import MagicString from 'magic-string'
1
+ import { parse , type ParserPlugin } from '@babel/parser'
5
2
import {
3
+ isBooleanLiteral ,
4
+ isCallExpression ,
5
+ isFunction ,
6
+ isIdentifier ,
7
+ isJSX ,
8
+ isJSXExpressionContainer ,
9
+ isLiteral ,
10
+ isStringLiteral ,
6
11
type ArrayExpression ,
7
12
type Binary ,
8
13
type CallExpression ,
@@ -23,21 +28,16 @@ import {
23
28
type ObjectExpression ,
24
29
type TemplateLiteral ,
25
30
type UnaryExpression ,
26
- isBooleanLiteral ,
27
- isCallExpression ,
28
- isFunction ,
29
- isIdentifier ,
30
- isJSX ,
31
- isJSXExpressionContainer ,
32
- isLiteral ,
33
- isStringLiteral ,
34
31
} from '@babel/types'
32
+ import { encode } from 'entities'
33
+ import { walk } from 'estree-walker'
34
+ import MagicString from 'magic-string'
35
35
import {
36
- type Primitive ,
37
36
escapeString ,
38
37
isPlainObject ,
39
38
isPrimitive ,
40
39
styleToString ,
40
+ type Primitive ,
41
41
} from './utils'
42
42
import type { OptionsResolved } from './options'
43
43
@@ -75,15 +75,6 @@ function transformJsx(code: string, node: JSX) {
75
75
return toStringExpression ( resolveJsx ( node ) )
76
76
}
77
77
78
- function toStringExpression ( expr : EvaluatedValue ) {
79
- if ( expr instanceof RegExp ) {
80
- return expr . toString ( )
81
- } else if ( typeof expr === 'object' ) {
82
- return JSON . stringify ( expr )
83
- }
84
- return String ( expr )
85
- }
86
-
87
78
function toStringJsxChildren (
88
79
nodes : Array <
89
80
| JSXText
@@ -96,18 +87,6 @@ function transformJsx(code: string, node: JSX) {
96
87
return nodes . map ( ( child ) => toStringJsx ( child ) ) . join ( '' )
97
88
}
98
89
99
- function toStringJsxText ( node : JSXText ) {
100
- const texts = node . value . split ( '\n' )
101
-
102
- return texts
103
- . map ( ( text , idx ) => ( idx > 0 ? text . trim ( ) : text ) )
104
- . filter ( ( line ) => {
105
- if ( line . trim ( ) . length === 0 ) return false
106
- return true
107
- } )
108
- . join ( ' ' )
109
- }
110
-
111
90
function toStringJsxElement ( node : JSXElement ) {
112
91
if ( node . openingElement . selfClosing ) {
113
92
return toStringOpeningElement ( node . openingElement )
@@ -117,28 +96,6 @@ function transformJsx(code: string, node: JSX) {
117
96
node . openingElement ,
118
97
) } ${ children } </${ toStringJsxName ( node . closingElement ! . name ) } >`
119
98
}
120
-
121
- function toStringOpeningElement ( node : JSXOpeningElement ) {
122
- let str = `<${ toStringJsxName ( node . name ) } `
123
-
124
- const props : string [ ] = node . attributes
125
- . map ( ( attr ) => {
126
- if ( attr . type === 'JSXAttribute' ) {
127
- return toStringJsxAttribute ( attr )
128
- } else {
129
- return notSupported ( node )
130
- }
131
- } )
132
- . filter ( ( x ) : x is string => x !== undefined )
133
-
134
- if ( props . length > 0 ) {
135
- str += ` ${ props . join ( ' ' ) } `
136
- }
137
-
138
- str += node . selfClosing ? '/>' : '>'
139
-
140
- return str
141
- }
142
99
}
143
100
144
101
function toStringJsxAttribute ( node : JSXAttribute ) {
@@ -435,6 +392,49 @@ function transformJsx(code: string, node: JSX) {
435
392
function notSupported ( node : Node ) : never {
436
393
throw new Error ( `not supported ${ node . type } : ${ getSource ( node ) } ` )
437
394
}
395
+
396
+ function toStringOpeningElement ( node : JSXOpeningElement ) {
397
+ let str = `<${ toStringJsxName ( node . name ) } `
398
+
399
+ const props : string [ ] = node . attributes
400
+ . map ( ( attr ) => {
401
+ if ( attr . type === 'JSXAttribute' ) {
402
+ return toStringJsxAttribute ( attr )
403
+ } else {
404
+ return notSupported ( node )
405
+ }
406
+ } )
407
+ . filter ( ( x ) : x is string => x !== undefined )
408
+
409
+ if ( props . length > 0 ) {
410
+ str += ` ${ props . join ( ' ' ) } `
411
+ }
412
+
413
+ str += node . selfClosing ? '/>' : '>'
414
+
415
+ return str
416
+ }
417
+ }
418
+
419
+ function toStringExpression ( expr : EvaluatedValue ) {
420
+ if ( expr instanceof RegExp ) {
421
+ return expr . toString ( )
422
+ } else if ( typeof expr === 'object' ) {
423
+ return JSON . stringify ( expr )
424
+ }
425
+ return String ( expr )
426
+ }
427
+
428
+ function toStringJsxText ( node : JSXText ) {
429
+ const texts = node . value . split ( '\n' )
430
+
431
+ return texts
432
+ . map ( ( text , idx ) => ( idx > 0 ? text . trim ( ) : text ) )
433
+ . filter ( ( line ) => {
434
+ if ( line . trim ( ) . length === 0 ) return false
435
+ return true
436
+ } )
437
+ . join ( ' ' )
438
438
}
439
439
440
440
export function transformJsxToString (
0 commit comments