Skip to content

New plugin template does not compile due to unsatisfied trait bound #11096

@jamesWalker55

Description

@jamesWalker55

Describe the bug

Following the official documentation and creating a plugin: https://swc.rs/docs/plugin/ecmascript/getting-started

swc plugin new --target-type wasm32-wasip1 my-first-plugin

Without any changes, trying to test the project will produce this error:

   Compiling my-first-plugin v0.1.0 (D:\Programming\af2ae\my-first-plugin)
error[E0277]: the trait bound `VisitMutPass<TransformVisitor>: Fold` is not satisfied
      --> src\lib.rs:33:23
       |
33     |     program.fold_with(&mut visit_mut_pass(TransformVisitor))
       |             --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Fold` is not implemented for `VisitMutPass<TransformVisitor>`
       |             |
       |             required by a bound introduced by this call
       |
       = help: the following other types implement trait `Fold`:
                 &mut V
                 Box<V>
                 Either<A, B>
                 FoldPass<V>
                 HygieneTester
                 HygieneVisualizer
                 Optional<V>
                 swc_core::common::pass::Repeat<V>
note: required by a bound in `fold_with`
      --> C:\Users\James\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\swc_ecma_visit-15.0.0\src\generated.rs:101968:32
       |
101968 | pub trait FoldWith<V: ?Sized + Fold> {
       |                                ^^^^ required by this bound in `FoldWith::fold_with`
101969 |     #[doc = r" Calls a visitor method (visitor.fold_xxx) with self."]
101970 |     fn fold_with(self, visitor: &mut V) -> Self;
       |        --------- required by a bound in this associated function

Input code

use swc_core::ecma::{
    ast::Program,
    transforms::testing::test_inline,
    visit::{visit_mut_pass, 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 visit_mut_pass(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_inline!(
    Default::default(),
    |_| visit_mut_pass(TransformVisitor),
    boo,
    // Input codes
    r#"console.log("transform");"#,
    // Output codes after transformed with plugin
    r#"console.log("transform");"#
);

Config

Link to the code that reproduces this issue

n/a

SWC Info output

No response

Expected behavior

The plugin builds successfully

Actual behavior

No response

Version

main branch, commit 6c54969

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions