@@ -44,8 +44,11 @@ use super::{inherit_variants, js::*, literal::*, ts::*};
44
44
pub struct JSXElement < ' a > {
45
45
#[ serde( flatten) ]
46
46
pub span : Span ,
47
+ /// Opening tag of the element.
47
48
pub opening_element : Box < ' a , JSXOpeningElement < ' a > > ,
49
+ /// Closing tag of the element. Will be [`None`] for self-closing tags.
48
50
pub closing_element : Option < Box < ' a , JSXClosingElement < ' a > > > ,
51
+ /// Children of the element. This can be text, other elements, or expressions.
49
52
pub children : Vec < ' a , JSXChild < ' a > > ,
50
53
}
51
54
@@ -89,7 +92,15 @@ pub struct JSXOpeningElement<'a> {
89
92
90
93
/// JSX Closing Element
91
94
///
92
- /// Closing tag in a [`JSXElement`]. Not all JSX elements have a closing tag.
95
+ /// Closing tag in a [`JSXElement`]. Self-closing tags do not have closing elements.
96
+ ///
97
+ /// ## Example
98
+ ///
99
+ /// ```tsx
100
+ /// <Foo>Hello, World!</Foo>
101
+ /// // ^^^ name
102
+ /// <Bar /> // <- no closing element
103
+ /// ```
93
104
#[ ast( visit) ]
94
105
#[ derive( Debug , Hash ) ]
95
106
#[ generate_derive( CloneIn , GetSpan , GetSpanMut ) ]
@@ -117,8 +128,11 @@ pub struct JSXClosingElement<'a> {
117
128
pub struct JSXFragment < ' a > {
118
129
#[ serde( flatten) ]
119
130
pub span : Span ,
131
+ /// `<>`
120
132
pub opening_fragment : JSXOpeningFragment ,
133
+ /// `</>`
121
134
pub closing_fragment : JSXClosingFragment ,
135
+ /// Elements inside the fragment.
122
136
pub children : Vec < ' a , JSXChild < ' a > > ,
123
137
}
124
138
@@ -240,6 +254,7 @@ pub enum JSXMemberExpressionObject<'a> {
240
254
pub struct JSXExpressionContainer < ' a > {
241
255
#[ serde( flatten) ]
242
256
pub span : Span ,
257
+ /// The expression inside the container.
243
258
pub expression : JSXExpression < ' a > ,
244
259
}
245
260
@@ -276,21 +291,35 @@ pub struct JSXEmptyExpression {
276
291
// 1.3 JSX Attributes
277
292
278
293
/// JSX Attributes
294
+ ///
295
+ /// ## Example
296
+ ///
297
+ /// ```tsx
298
+ /// <Component foo="bar" baz={4} {...rest} />
299
+ /// // ^^^^^^^^^ ^^^^^^^ ^^^^^^^^^
300
+ /// // Attribute SpreadAttribute
301
+ /// ```
279
302
#[ ast( visit) ]
280
303
#[ derive( Debug , Hash ) ]
281
304
#[ generate_derive( CloneIn , GetSpan , GetSpanMut ) ]
282
305
#[ cfg_attr( feature = "serialize" , derive( Serialize , Tsify ) ) ]
283
306
#[ serde( untagged) ]
284
307
pub enum JSXAttributeItem < ' a > {
308
+ /// A `key="value"` attribute
285
309
Attribute ( Box < ' a , JSXAttribute < ' a > > ) = 0 ,
310
+ /// a `{...spread}` attribute
286
311
SpreadAttribute ( Box < ' a , JSXSpreadAttribute < ' a > > ) = 1 ,
287
312
}
288
313
289
314
/// JSX Attribute
290
315
///
316
+ /// An attribute in a JSX opening tag. May or may not have a value. Part of
317
+ /// [`JSXAttributeItem`].
318
+ ///
291
319
/// ## Example
292
320
///
293
321
/// ```tsx
322
+ /// // `has-no-value` is a JSXAttribute with no value.
294
323
/// <Component has-no-value foo="foo" />
295
324
/// // name ^^^ ^^^^ value
296
325
#[ ast( visit) ]
@@ -301,7 +330,11 @@ pub enum JSXAttributeItem<'a> {
301
330
pub struct JSXAttribute < ' a > {
302
331
#[ serde( flatten) ]
303
332
pub span : Span ,
333
+ /// The name of the attribute. This is a prop in React-like applications.
304
334
pub name : JSXAttributeName < ' a > ,
335
+ /// The value of the attribute. This can be a string literal, an expression,
336
+ /// or an element. Will be [`None`] for boolean-like attributes (e.g.
337
+ /// `<button disabled />`).
305
338
pub value : Option < JSXAttributeValue < ' a > > ,
306
339
}
307
340
@@ -326,19 +359,48 @@ pub struct JSXSpreadAttribute<'a> {
326
359
/// JSX Attribute Name
327
360
///
328
361
/// Part of a [`JSXAttribute`].
362
+ ///
363
+ /// "Normal" attributes will be a [`JSXIdentifier`], while namespaced attributes
364
+ /// will be a [`JSXNamespacedName`].
365
+ ///
366
+ /// ## Example
367
+ ///
368
+ /// ```tsx
369
+ /// const Foo = <Component foo="bar" />;
370
+ /// // ^^^ Identifier
371
+ /// const Bar = <Component foo:bar="baz" />;
372
+ /// // ^^^^^^^ NamespacedName
373
+ /// ```
329
374
#[ ast( visit) ]
330
375
#[ derive( Debug , Hash ) ]
331
376
#[ generate_derive( CloneIn , GetSpan , GetSpanMut ) ]
332
377
#[ cfg_attr( feature = "serialize" , derive( Serialize , Tsify ) ) ]
333
378
#[ serde( untagged) ]
334
379
pub enum JSXAttributeName < ' a > {
380
+ /// An attribute name without a namespace prefix, e.g. `foo` in `foo="bar"`.
335
381
Identifier ( Box < ' a , JSXIdentifier < ' a > > ) = 0 ,
382
+ /// An attribute name with a namespace prefix, e.g. `foo:bar` in `foo:bar="baz"`.
336
383
NamespacedName ( Box < ' a , JSXNamespacedName < ' a > > ) = 1 ,
337
384
}
338
385
339
386
/// JSX Attribute Value
340
387
///
341
388
/// Part of a [`JSXAttribute`].
389
+ ///
390
+ /// You're most likely interested in [`StringLiteral`] and
391
+ /// [`JSXExpressionContainer`].
392
+ ///
393
+ /// ## Example
394
+ ///
395
+ /// ```tsx
396
+ /// // v ExpressionContainer storing a NumericLiteral
397
+ /// <Component foo="bar" baz={4} />
398
+ /// // ^^^ StringLiteral
399
+ ///
400
+ /// // not a very common case, but it is valid syntax. Could also be a fragment.
401
+ /// <Component foo=<Element /> />
402
+ /// // ^^^^^^^^^^^ Element
403
+ /// ```
342
404
#[ ast( visit) ]
343
405
#[ derive( Debug , Hash ) ]
344
406
#[ generate_derive( CloneIn , GetSpan , GetSpanMut ) ]
@@ -364,6 +426,7 @@ pub enum JSXAttributeValue<'a> {
364
426
pub struct JSXIdentifier < ' a > {
365
427
#[ serde( flatten) ]
366
428
pub span : Span ,
429
+ /// The name of the identifier.
367
430
pub name : Atom < ' a > ,
368
431
}
369
432
@@ -401,6 +464,7 @@ pub enum JSXChild<'a> {
401
464
pub struct JSXSpreadChild < ' a > {
402
465
#[ serde( flatten) ]
403
466
pub span : Span ,
467
+ /// The expression being spread.
404
468
pub expression : Expression < ' a > ,
405
469
}
406
470
@@ -422,5 +486,6 @@ pub struct JSXSpreadChild<'a> {
422
486
pub struct JSXText < ' a > {
423
487
#[ serde( flatten) ]
424
488
pub span : Span ,
489
+ /// The text content.
425
490
pub value : Atom < ' a > ,
426
491
}
0 commit comments