Skip to content

Commit 0b2d9c3

Browse files
committed
Support JSX expressions in hast element nodes
1 parent 039bdee commit 0b2d9c3

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/hast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ pub enum PropertyValue {
187187
CommaSeparated(Vec<String>),
188188
/// A space-separated list of strings.
189189
SpaceSeparated(Vec<String>),
190+
/// A JSX expression.
191+
JsxExpression(String),
190192
}
191193

192194
/// Document type.

src/hast_util_to_swc.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,35 @@ fn transform_element(
226226
continue;
227227
}
228228
}
229-
hast::PropertyValue::String(x) => Some(Lit::Str(Str {
229+
hast::PropertyValue::String(x) => Some(JSXAttrValue::Lit(Lit::Str(Str {
230230
value: x.clone().into(),
231231
span: swc_core::common::DUMMY_SP,
232232
raw: None,
233-
})),
234-
hast::PropertyValue::CommaSeparated(x) => Some(Lit::Str(Str {
233+
}))),
234+
hast::PropertyValue::CommaSeparated(x) => Some(JSXAttrValue::Lit(Lit::Str(Str {
235235
value: x.join(", ").into(),
236236
span: swc_core::common::DUMMY_SP,
237237
raw: None,
238-
})),
239-
hast::PropertyValue::SpaceSeparated(x) => Some(Lit::Str(Str {
238+
}))),
239+
hast::PropertyValue::SpaceSeparated(x) => Some(JSXAttrValue::Lit(Lit::Str(Str {
240240
value: x.join(" ").into(),
241241
span: swc_core::common::DUMMY_SP,
242242
raw: None,
243-
})),
243+
}))),
244+
hast::PropertyValue::JsxExpression(x) => {
245+
Some(JSXAttrValue::JSXExprContainer(JSXExprContainer {
246+
expr: JSXExpr::Expr(
247+
parse_expression_to_tree(
248+
&x,
249+
&MdxExpressionKind::AttributeValueExpression,
250+
&[],
251+
context.location,
252+
)?
253+
.unwrap(),
254+
),
255+
span: swc_core::common::DUMMY_SP,
256+
}))
257+
}
244258
};
245259

246260
// Turn property case into either React-specific case, or HTML
@@ -250,7 +264,7 @@ fn transform_element(
250264

251265
attrs.push(JSXAttrOrSpread::JSXAttr(JSXAttr {
252266
name: create_jsx_attr_name_from_str(&attr_name),
253-
value: value.map(JSXAttrValue::Lit),
267+
value,
254268
span: swc_core::common::DUMMY_SP,
255269
}));
256270

0 commit comments

Comments
 (0)