1
1
import { last } from './helper'
2
+ import { parser } from './parser'
2
3
import {
3
4
isCloseTag ,
4
5
isComment ,
@@ -29,7 +30,7 @@ export class Traverse {
29
30
}
30
31
31
32
// fix #7
32
- combineJsxNodes ( nodes : Node [ ] ) {
33
+ combineJsxNodes ( nodes : Node [ ] , parent ?: Parent ) {
33
34
let offset = 0
34
35
const jsxNodes : Node [ ] = [ ]
35
36
const { length } = nodes
@@ -41,8 +42,11 @@ export class Traverse {
41
42
offset ++
42
43
jsxNodes . push ( node )
43
44
} else {
44
- if ( isCloseTag ( value ) ) {
45
+ if (
46
+ isCloseTag ( value )
47
+ ) {
45
48
offset --
49
+ jsxNodes . push ( node )
46
50
}
47
51
// prettier-ignore
48
52
/* istanbul ignore next */
@@ -51,20 +55,26 @@ export class Traverse {
51
55
! isSelfClosingTag ( value ) &&
52
56
! isOpenCloseTag ( value )
53
57
) {
54
- // should never happen, just for robustness
55
- const { start } = node . position
56
- throw Object . assign (
57
- new SyntaxError ( 'unknown jsx node: ' + JSON . stringify ( value ) ) ,
58
- {
59
- lineNumber : start . line ,
60
- column : start . column ,
61
- index : start . offset ,
62
- } ,
63
- )
58
+ try {
59
+ // fix #138
60
+ const nodes = parser . normalizeJsxNode ( node , parent )
61
+ jsxNodes . push ( ...( Array . isArray ( nodes ) ? nodes : [ nodes ] ) )
62
+ } catch {
63
+ // should never happen, just for robustness
64
+ const { start } = node . position
65
+ throw Object . assign (
66
+ new SyntaxError ( 'unknown jsx node: ' + JSON . stringify ( value ) ) ,
67
+ {
68
+ lineNumber : start . line ,
69
+ column : start . column ,
70
+ index : start . offset ,
71
+ } ,
72
+ )
73
+ }
74
+ } else {
75
+ jsxNodes . push ( node )
64
76
}
65
77
66
- jsxNodes . push ( node )
67
-
68
78
if ( ! offset ) {
69
79
acc . push ( this . combineLeftJsxNodes ( jsxNodes ) )
70
80
jsxNodes . length = 0
@@ -92,8 +102,9 @@ export class Traverse {
92
102
let children = node . children as Node [ ]
93
103
94
104
if ( children ) {
95
- children = node . children = this . combineJsxNodes ( children )
96
- children . forEach ( child => this . traverse ( child , node as Parent ) )
105
+ const parent = node as Parent
106
+ children = node . children = this . combineJsxNodes ( children , parent )
107
+ children . forEach ( child => this . traverse ( child , parent ) )
97
108
}
98
109
99
110
this . _enter ( node , parent )
0 commit comments