Skip to content

Commit

Permalink
fix(plugin): Set swc_common::errors::HANDLER while invoking plugins (
Browse files Browse the repository at this point in the history
…#8511)

**Description:**

- `swc_common::errors::handler::HANDLER` should be used, and `swc_core::plugin::errors::HANDLER` is now a simple alias.



**Related issue:**

 - Closes #8502
  • Loading branch information
kdy1 authored Jan 17, 2024
1 parent fa05481 commit ba753f1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
7 changes: 1 addition & 6 deletions crates/swc_core/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ pub mod memory {
)))
)]
pub mod errors {
/// global context HANDLER in plugin's transform function.
pub static HANDLER: swc_plugin::pseudo_scoped_key::PseudoScopedKey<
swc_common::errors::Handler,
> = swc_plugin::pseudo_scoped_key::PseudoScopedKey {
inner: once_cell::sync::OnceCell::new(),
};
pub use swc_common::errors::HANDLER;
}

/// Plugin's environment metadata context.
Expand Down
12 changes: 3 additions & 9 deletions crates/swc_plugin_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,6 @@ fn handle_func(func: ItemFn, ast_type: Ident) -> TokenStream {
false,
Box::new(PluginDiagnosticsEmitter)
);
let handler_set_result = swc_core::plugin::errors::HANDLER.inner.set(handler);

if handler_set_result.is_err() {
let err = swc_core::common::plugin::serialized::PluginError::Serialize(
"Failed to set handler for plugin".to_string()
);
return construct_error_ptr(err);
}

// Construct metadata to the `Program` for the transform plugin.
let plugin_comments_proxy = if should_enable_comments_proxy == 1 { Some(swc_core::plugin::proxies::PluginCommentsProxy) } else { None };
Expand All @@ -161,7 +153,9 @@ fn handle_func(func: ItemFn, ast_type: Ident) -> TokenStream {
};

// Take original plugin fn ident, then call it with interop'ed args
let transformed_program = swc_core::common::plugin::serialized::VersionedSerializable::new(#ident(program, metadata));
let transformed_program = swc_core::common::plugin::serialized::VersionedSerializable::new(swc_core::common::errors::HANDLER.set(&handler, || {
#ident(program, metadata)
}));

// Serialize transformed result, return back to the host.
let serialized_result = swc_core::common::plugin::serialized::PluginSerializedBytes::try_serialize(
Expand Down

0 comments on commit ba753f1

Please sign in to comment.