swc icon indicating copy to clipboard operation
swc copied to clipboard

The test! macro isn't work as document said now

Open roark47 opened this issue 3 years ago • 1 comments

Describe the bug

I followed the SWC plugin document to initialize a project with swc plugin new --target-type wasm32-wasi but when I ran cargo test command in my demo project, I got an error as follows:

error: no rules expected the token `r#"console.log("transform");"#`
   --> src/lib.rs:47:5
    |
47  |     r#"console.log("transform");"#
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no rules expected this token in macro call
    |
note: while trying to match `ok_if_code_eq`
   --> /Users/<anonymous>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_testing-0.137.44/src/lib.rs:414:61
    |
414 |     ($syntax:expr, $tr:expr, $test_name:ident, $input:expr, ok_if_code_eq) => {
    |                                                             ^^^^^^^^^^^^^

I don't know what the ok_if_code_eq param did but the test macro is not working now apparently.

Input code

use swc_core::ecma::{
    ast::Program,
    transforms::testing::test,
    visit::{as_folder, FoldWith, VisitMut},
};
use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};

pub struct TransformVisitor;

impl VisitMut for TransformVisitor {
    // Implement necessary visit_mut_* methods for actual custom transform.
    // A comprehensive list of possible visitor methods can be found here:
    // https://rustdoc.swc.rs/swc_ecma_visit/trait.VisitMut.html
}

/// An example plugin function with macro support.
/// `plugin_transform` macro interop pointers into deserialized structs, as well
/// as returning ptr back to host.
///
/// It is possible to opt out from macro by writing transform fn manually
/// if plugin need to handle low-level ptr directly via
/// `__transform_plugin_process_impl(
///     ast_ptr: *const u8, ast_ptr_len: i32,
///     unresolved_mark: u32, should_enable_comments_proxy: i32) ->
///     i32 /*  0 for success, fail otherwise.
///             Note this is only for internal pointer interop result,
///             not actual transform result */`
///
/// This requires manual handling of serialization / deserialization from ptrs.
/// Refer swc_plugin_macro to see how does it work internally.
#[plugin_transform]
pub fn process_transform(program: Program, _metadata: TransformPluginProgramMetadata) -> Program {
    program.fold_with(&mut as_folder(TransformVisitor))
}

// An example to test plugin transform.
// Recommended strategy to test plugin's transform is verify
// the Visitor's behavior, instead of trying to run `process_transform` with mocks
// unless explicitly required to do so.
test!(
    Default::default(), // <-- there is an error saying it should be a `ignore` not a `Default`
    |_| as_folder(TransformVisitor),
    boo,
    // Input codes
    r#"console.log("transform");"#,
    // Output codes after transformed with plugin
    r#"console.log("transform");"#
);

Config

No response

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.94-nightly-20231016.1&code=H4sIAAAAAAAAAwMAAAAAAAAAAAA%3D&config=H4sIAAAAAAAAA1WPSw7DIAwF9zkF8rq7Sl30Dj2ERZ2IiJ9sIhVFuXsJgbTZ4ffGjLwOSsEsGp5qLc8yRGQhPueSSPYJPyUB0g5Fs4kJbr2dZa9GtEI12o4GEvJEqW7JveFgQxDqeMuc8WbM%2F0IdXGQSuYI7in6ydNUNTQkuvJdatkNSjnToH%2FCDuuz8GIy8%2BmbihYbtC6sl6BwUAQAA

SWC Info output

No response

Expected behavior

The cargo test command isn't throwing any error.

Actual behavior

No response

Version

0.86.75

Additional context

No response

roark47 avatar Nov 22 '23 07:11 roark47

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

old-swc-bot avatar Dec 30 '23 12:12 old-swc-bot