@@ -4,6 +4,10 @@ module.exports = toEstree
4
4
5
5
var commas = require ( 'comma-separated-tokens' )
6
6
var attachComments = require ( 'estree-util-attach-comments' )
7
+ var {
8
+ start : identifierStart ,
9
+ cont : identifierCont
10
+ } = require ( 'estree-util-is-identifier-name' )
7
11
var whitespace = require ( 'hast-util-whitespace' )
8
12
var find = require ( 'property-information/find' )
9
13
var hastToReact = require ( 'property-information/hast-to-react.json' )
@@ -167,11 +171,33 @@ function element(node, context) {
167
171
value = { type : 'Literal' , value : String ( value ) }
168
172
}
169
173
170
- attributes . push ( {
171
- type : 'JSXAttribute' ,
172
- name : { type : 'JSXIdentifier' , name : prop } ,
173
- value : value
174
- } )
174
+ if ( jsxIdentifierName ( prop ) ) {
175
+ attributes . push ( {
176
+ type : 'JSXAttribute' ,
177
+ name : { type : 'JSXIdentifier' , name : prop } ,
178
+ value : value
179
+ } )
180
+ } else {
181
+ // No need to worry about `style` (which has a `JSXExpressionContainer`
182
+ // value) because that’s a valid identifier.
183
+ attributes . push ( {
184
+ type : 'JSXSpreadAttribute' ,
185
+ argument : {
186
+ type : 'ObjectExpression' ,
187
+ properties : [
188
+ {
189
+ type : 'Property' ,
190
+ method : false ,
191
+ shorthand : false ,
192
+ computed : false ,
193
+ key : { type : 'Literal' , value : String ( prop ) } ,
194
+ value : value || { type : 'Literal' , value : true } ,
195
+ kind : 'init'
196
+ }
197
+ ]
198
+ }
199
+ } )
200
+ }
175
201
}
176
202
177
203
// Restore parent schema.
@@ -490,3 +516,23 @@ function parseStyle(value, tagName) {
490
516
function styleReplacer ( $0 , $1 ) {
491
517
return $1 . toUpperCase ( )
492
518
}
519
+
520
+ /**
521
+ * Checks if the given string is a valid identifier name.
522
+ *
523
+ * @param {string } name
524
+ */
525
+ function jsxIdentifierName ( name ) {
526
+ var index = - 1
527
+
528
+ while ( ++ index < name . length ) {
529
+ if ( ! ( index ? cont : identifierStart ) ( name . charCodeAt ( index ) ) ) return false
530
+ }
531
+
532
+ // `false` if `name` is empty.
533
+ return index > 0
534
+
535
+ function cont ( code ) {
536
+ return identifierCont ( code ) || code === 45 /* `-` */
537
+ }
538
+ }
0 commit comments