@@ -25,6 +25,8 @@ var renderXJSExpressionContainer =
25
25
var renderXJSLiteral = require ( './xjs' ) . renderXJSLiteral ;
26
26
var quoteAttrName = require ( './xjs' ) . quoteAttrName ;
27
27
28
+ var trimLeft = require ( './xjs' ) . trimLeft ;
29
+
28
30
/**
29
31
* Customized desugar processor.
30
32
*
@@ -53,7 +55,7 @@ function visitReactTag(traverse, object, path, state) {
53
55
var nameObject = openingElement . name ;
54
56
var attributesObject = openingElement . attributes ;
55
57
56
- utils . catchup ( openingElement . range [ 0 ] , state ) ;
58
+ utils . catchup ( openingElement . range [ 0 ] , state , trimLeft ) ;
57
59
58
60
if ( nameObject . namespace ) {
59
61
throw new Error (
@@ -68,44 +70,43 @@ function visitReactTag(traverse, object, path, state) {
68
70
69
71
utils . move ( nameObject . range [ 1 ] , state ) ;
70
72
73
+ var hasAttributes = attributesObject . length ;
74
+
71
75
// if we don't have any attributes, pass in null
72
- if ( attributesObject . length === 0 ) {
76
+ if ( hasAttributes ) {
77
+ utils . append ( '{' , state ) ;
78
+ } else {
73
79
utils . append ( 'null' , state ) ;
74
80
}
75
81
76
82
// write attributes
77
83
attributesObject . forEach ( function ( attr , index ) {
78
- utils . catchup ( attr . range [ 0 ] , state ) ;
79
84
if ( attr . name . namespace ) {
80
85
throw new Error (
81
86
'Namespace attributes are not supported. ReactJSX is not XML.' ) ;
82
87
}
83
88
var name = attr . name . name ;
84
- var isFirst = index === 0 ;
85
89
var isLast = index === attributesObject . length - 1 ;
86
90
87
- if ( isFirst ) {
88
- utils . append ( '{' , state ) ;
89
- }
90
-
91
+ utils . catchup ( attr . range [ 0 ] , state , trimLeft ) ;
91
92
utils . append ( quoteAttrName ( name ) , state ) ;
92
- utils . append ( ':' , state ) ;
93
+ utils . append ( ': ' , state ) ;
93
94
94
95
if ( ! attr . value ) {
95
96
state . g . buffer += 'true' ;
96
97
state . g . position = attr . name . range [ 1 ] ;
97
98
if ( ! isLast ) {
98
- utils . append ( ',' , state ) ;
99
+ utils . append ( ', ' , state ) ;
99
100
}
100
101
} else {
101
102
utils . move ( attr . name . range [ 1 ] , state ) ;
102
- // Use catchupWhiteSpace to skip over the '=' in the attribute
103
- utils . catchupWhiteSpace ( attr . value . range [ 0 ] , state ) ;
103
+ // Use catchupNewlines to skip over the '=' in the attribute
104
+ utils . catchupNewlines ( attr . value . range [ 0 ] , state ) ;
104
105
if ( JSX_ATTRIBUTE_TRANSFORMS . hasOwnProperty ( attr . name . name ) ) {
105
106
utils . append ( JSX_ATTRIBUTE_TRANSFORMS [ attr . name . name ] ( attr ) , state ) ;
106
107
utils . move ( attr . value . range [ 1 ] , state ) ;
107
108
if ( ! isLast ) {
108
- utils . append ( ',' , state ) ;
109
+ utils . append ( ', ' , state ) ;
109
110
}
110
111
} else if ( attr . value . type === Syntax . Literal ) {
111
112
renderXJSLiteral ( attr . value , isLast , state ) ;
@@ -114,18 +115,18 @@ function visitReactTag(traverse, object, path, state) {
114
115
}
115
116
}
116
117
117
- if ( isLast ) {
118
- utils . append ( '}' , state ) ;
119
- }
120
-
121
- utils . catchup ( attr . range [ 1 ] , state ) ;
118
+ utils . catchup ( attr . range [ 1 ] , state , trimLeft ) ;
122
119
} ) ;
123
120
124
121
if ( ! openingElement . selfClosing ) {
125
- utils . catchup ( openingElement . range [ 1 ] - 1 , state ) ;
122
+ utils . catchup ( openingElement . range [ 1 ] - 1 , state , trimLeft ) ;
126
123
utils . move ( openingElement . range [ 1 ] , state ) ;
127
124
}
128
125
126
+ if ( hasAttributes ) {
127
+ utils . append ( '}' , state ) ;
128
+ }
129
+
129
130
// filter out whitespace
130
131
var childrenToRender = object . children . filter ( function ( child ) {
131
132
return ! ( child . type === Syntax . Literal
@@ -147,7 +148,7 @@ function visitReactTag(traverse, object, path, state) {
147
148
}
148
149
149
150
childrenToRender . forEach ( function ( child , index ) {
150
- utils . catchup ( child . range [ 0 ] , state ) ;
151
+ utils . catchup ( child . range [ 0 ] , state , trimLeft ) ;
151
152
152
153
var isLast = index >= lastRenderableIndex ;
153
154
@@ -158,22 +159,21 @@ function visitReactTag(traverse, object, path, state) {
158
159
} else {
159
160
traverse ( child , path , state ) ;
160
161
if ( ! isLast ) {
161
- utils . append ( ',' , state ) ;
162
- state . g . buffer = state . g . buffer . replace ( / ( \s * ) , $ / , ',$1' ) ;
162
+ utils . append ( ', ' , state ) ;
163
163
}
164
164
}
165
165
166
- utils . catchup ( child . range [ 1 ] , state ) ;
166
+ utils . catchup ( child . range [ 1 ] , state , trimLeft ) ;
167
167
} ) ;
168
168
}
169
169
170
170
if ( openingElement . selfClosing ) {
171
171
// everything up to />
172
- utils . catchup ( openingElement . range [ 1 ] - 2 , state ) ;
172
+ utils . catchup ( openingElement . range [ 1 ] - 2 , state , trimLeft ) ;
173
173
utils . move ( openingElement . range [ 1 ] , state ) ;
174
174
} else {
175
175
// everything up to </ sdflksjfd>
176
- utils . catchup ( object . closingElement . range [ 0 ] , state ) ;
176
+ utils . catchup ( object . closingElement . range [ 0 ] , state , trimLeft ) ;
177
177
utils . move ( object . closingElement . range [ 1 ] , state ) ;
178
178
}
179
179
0 commit comments