Skip to content

Commit 8827659

Browse files
committed
docs(ast): more doc comments for JSX nodes (#4830)
1 parent 89712f5 commit 8827659

File tree

2 files changed

+117
-52
lines changed

2 files changed

+117
-52
lines changed

crates/oxc_ast/src/ast/jsx.rs

+66-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ use super::{inherit_variants, js::*, literal::*, ts::*};
4444
pub struct JSXElement<'a> {
4545
#[serde(flatten)]
4646
pub span: Span,
47+
/// Opening tag of the element.
4748
pub opening_element: Box<'a, JSXOpeningElement<'a>>,
49+
/// Closing tag of the element. Will be [`None`] for self-closing tags.
4850
pub closing_element: Option<Box<'a, JSXClosingElement<'a>>>,
51+
/// Children of the element. This can be text, other elements, or expressions.
4952
pub children: Vec<'a, JSXChild<'a>>,
5053
}
5154

@@ -89,7 +92,15 @@ pub struct JSXOpeningElement<'a> {
8992

9093
/// JSX Closing Element
9194
///
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+
/// ```
93104
#[ast(visit)]
94105
#[derive(Debug, Hash)]
95106
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
@@ -117,8 +128,11 @@ pub struct JSXClosingElement<'a> {
117128
pub struct JSXFragment<'a> {
118129
#[serde(flatten)]
119130
pub span: Span,
131+
/// `<>`
120132
pub opening_fragment: JSXOpeningFragment,
133+
/// `</>`
121134
pub closing_fragment: JSXClosingFragment,
135+
/// Elements inside the fragment.
122136
pub children: Vec<'a, JSXChild<'a>>,
123137
}
124138

@@ -240,6 +254,7 @@ pub enum JSXMemberExpressionObject<'a> {
240254
pub struct JSXExpressionContainer<'a> {
241255
#[serde(flatten)]
242256
pub span: Span,
257+
/// The expression inside the container.
243258
pub expression: JSXExpression<'a>,
244259
}
245260

@@ -276,21 +291,35 @@ pub struct JSXEmptyExpression {
276291
// 1.3 JSX Attributes
277292

278293
/// JSX Attributes
294+
///
295+
/// ## Example
296+
///
297+
/// ```tsx
298+
/// <Component foo="bar" baz={4} {...rest} />
299+
/// // ^^^^^^^^^ ^^^^^^^ ^^^^^^^^^
300+
/// // Attribute SpreadAttribute
301+
/// ```
279302
#[ast(visit)]
280303
#[derive(Debug, Hash)]
281304
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
282305
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
283306
#[serde(untagged)]
284307
pub enum JSXAttributeItem<'a> {
308+
/// A `key="value"` attribute
285309
Attribute(Box<'a, JSXAttribute<'a>>) = 0,
310+
/// a `{...spread}` attribute
286311
SpreadAttribute(Box<'a, JSXSpreadAttribute<'a>>) = 1,
287312
}
288313

289314
/// JSX Attribute
290315
///
316+
/// An attribute in a JSX opening tag. May or may not have a value. Part of
317+
/// [`JSXAttributeItem`].
318+
///
291319
/// ## Example
292320
///
293321
/// ```tsx
322+
/// // `has-no-value` is a JSXAttribute with no value.
294323
/// <Component has-no-value foo="foo" />
295324
/// // name ^^^ ^^^^ value
296325
#[ast(visit)]
@@ -301,7 +330,11 @@ pub enum JSXAttributeItem<'a> {
301330
pub struct JSXAttribute<'a> {
302331
#[serde(flatten)]
303332
pub span: Span,
333+
/// The name of the attribute. This is a prop in React-like applications.
304334
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 />`).
305338
pub value: Option<JSXAttributeValue<'a>>,
306339
}
307340

@@ -326,19 +359,48 @@ pub struct JSXSpreadAttribute<'a> {
326359
/// JSX Attribute Name
327360
///
328361
/// 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+
/// ```
329374
#[ast(visit)]
330375
#[derive(Debug, Hash)]
331376
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
332377
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
333378
#[serde(untagged)]
334379
pub enum JSXAttributeName<'a> {
380+
/// An attribute name without a namespace prefix, e.g. `foo` in `foo="bar"`.
335381
Identifier(Box<'a, JSXIdentifier<'a>>) = 0,
382+
/// An attribute name with a namespace prefix, e.g. `foo:bar` in `foo:bar="baz"`.
336383
NamespacedName(Box<'a, JSXNamespacedName<'a>>) = 1,
337384
}
338385

339386
/// JSX Attribute Value
340387
///
341388
/// 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+
/// ```
342404
#[ast(visit)]
343405
#[derive(Debug, Hash)]
344406
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
@@ -364,6 +426,7 @@ pub enum JSXAttributeValue<'a> {
364426
pub struct JSXIdentifier<'a> {
365427
#[serde(flatten)]
366428
pub span: Span,
429+
/// The name of the identifier.
367430
pub name: Atom<'a>,
368431
}
369432

@@ -401,6 +464,7 @@ pub enum JSXChild<'a> {
401464
pub struct JSXSpreadChild<'a> {
402465
#[serde(flatten)]
403466
pub span: Span,
467+
/// The expression being spread.
404468
pub expression: Expression<'a>,
405469
}
406470

@@ -422,5 +486,6 @@ pub struct JSXSpreadChild<'a> {
422486
pub struct JSXText<'a> {
423487
#[serde(flatten)]
424488
pub span: Span,
489+
/// The text content.
425490
pub value: Atom<'a>,
426491
}

0 commit comments

Comments
 (0)