Search code examples
rustrust-cargoswc

rust / SWC plugin / how to test jsx?


I'm trying to create a plugin by doing the documentation - https://swc.rs/docs/plugin/ecmascript/getting-started

I have simplified the example. The plugin doesn't change anything.

test!(
    Default::default(),
    |_| as_folder(TransformVisitor),
    boo,
    r#"foo === bar;"#,
    r#"foo === bar;"#
);

run "cargo test" - Good!

Test to validate jsx:

test!(
    Default::default(),
    |_| as_folder(TransformVisitor),
    boo,
    r#"<main></main>"#,
    r#"<main></main>"#
);

run "cargo test" - error Expression expected

How do I do a jsx validation test?

Here is a link to the "test" macro documentation. I don't understand what to do https://rustdoc.swc.rs/swc_core/ecma/transforms/testing/macro.test.html


Solution

  • You can see on the SWC Playground that <main></main> is not valid syntax, at least not by default.

    The parameter you're passing as Default::default() is the Syntax configuration. You should instead specify that you're expecting JSX, something like this (untested):

    use swc_ecma_parser::{EsConfig, Syntax};
    
    test!(
        Syntax::Es(EsConfig {
            jsx: true,
            ..Default::default()
        }),
        |_| as_folder(TransformVisitor),
        boo,
        r#"<main></main>"#,
        r#"<main></main>"#
    );