Get string from JSXAttrValue (enum)

I am creating a swc plugin using rust.

I am getting JSXAttrValue enum.

fn visit_mut_jsx_attr_value(&mut self, n: &mut JSXAttrValue){

JSXAttrValue in swc_core::ecma::utils::swc_ecma_ast - Rust - see this please

How to get string from JSXAttrValue?

let value = ?????;

I need the ability to compare "value" with a string, or write to a file.

Please help me to do this

Please add cross-posts as links to your question to avoid duplicated effort by the community:

I added a link to this question on stackoverflow.
You can help me? It doesn't seem difficult for those who understand rust. I'm new and I've been in pain for days

I wish I could help you more, but I have no experience with SWC whatsoever and their documentation is not very beginner friendly IMO. I assume you still want to parse the attribute value from an attribute like this: <div background="red"></div>, right? (Based on this question you asked about swc before). From how I understand JSXAttrValue's variants, it seems to me like "red" would be encoded in the ast in a JSXAttrValue::Lit variant. I think you could extract it with a match statement resembling something like:

fn visit_mut_jsx_attr_value(&mut self, n: &mut JSXAttrValue){
    match n {
        JSXAttrValue::Lit(Lit::Str(Str { value, ... })) => {
            if value == Atom::from("red") {
                println!("yehy, we found red!");
        _ => println!("attribute is not a string literal"),

Note that this is purely guesswork from staring at the (not too well documented) API docs. I hope dbg!(n) will print the variant you have to match and you can take it from there...


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.