Skip to content

Commit

Permalink
fix: unregistered case handler should not panic (#5330)
Browse files Browse the repository at this point in the history
* fix: unregistered case handler should not panic

* chore: add panic message
  • Loading branch information
h-a-n-a authored Jan 15, 2024
1 parent 3b576a3 commit ee0ed24
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
37 changes: 24 additions & 13 deletions crates/swc_plugin_import/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use inflector::Inflector;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
use serde::Deserialize;
use swc_core::{
common::{util::take::Take, BytePos, Span, SyntaxContext, DUMMY_SP},
common::{errors::HANDLER, util::take::Take, BytePos, Span, SyntaxContext, DUMMY_SP},
ecma::{
ast::{
Ident, ImportDecl, ImportDefaultSpecifier, ImportNamedSpecifier, ImportSpecifier, Module,
Expand Down Expand Up @@ -77,6 +77,9 @@ const CUSTOM_JS: &str = "CUSTOM_JS_NAME";
const CUSTOM_STYLE: &str = "CUSTOM_STYLE";
const CUSTOM_STYLE_NAME: &str = "CUSTOM_STYLE_NAME";

/// Panic:
///
/// Panics in sometimes if [swc_core::common::errors::HANDLER] is not provided.
pub fn plugin_import(config: &Vec<PluginImportConfig>) -> impl Fold + '_ {
let mut renderer = handlebars::Handlebars::new();

Expand Down Expand Up @@ -246,27 +249,35 @@ impl<'a> ImportPlugin<'a> {

let path = if let Some(transform) = &config.custom_name {
match transform {
CustomTransform::Fn(f) => f(name.clone()),
CustomTransform::Tpl(_) => Some(
self
.renderer
.render(
format!("{}{}", &config.library_name, CUSTOM_JS).as_str(),
&render_context(name.clone()),
)
.unwrap(),
),
CustomTransform::Fn(f) => Ok(f(name.clone())),
CustomTransform::Tpl(_) => self
.renderer
.render(
format!("{}{}", &config.library_name, CUSTOM_JS).as_str(),
&render_context(name.clone()),
)
.map(Some),
}
} else {
Some(format!(
Ok(Some(format!(
"{}/{}/{}",
&config.library_name,
config
.library_directory
.as_ref()
.unwrap_or(&"lib".to_string()),
transformed_name
))
)))
};

let path = match path {
Ok(p) => p,
Err(e) => {
HANDLER.with(|handler| {
handler.err(&e.to_string());
});
None
}
};

if path.is_none() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = [[/Helper not defined: \"unregisteredCase\"/]];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
it("should throw error instead of panic", () => {
expect(() => require("./lib-entry")).toThrowError(
/Helper not defined: \"unregisteredCase\"/
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { FooBar } from "./lib";
eval(FooBar);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "";
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
module: {
rules: [
{
test: /\.js$/,
loader: "builtin:swc-loader",
options: {
rspackExperiments: {
import: [
{
libraryName: "./lib",
customName: "./lib/{{ unregisteredCase member }}"
}
]
}
}
}
]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "FooBar";

1 comment on commit ee0ed24

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

name base current %
10000_development-mode + exec 1.64 s ± 23 ms 1.64 s ± 16 ms +0.11%
10000_development-mode_hmr + exec 994 ms ± 15 ms 1 s ± 3.2 ms +1.04%
10000_production-mode + exec 2.8 s ± 16 ms 2.88 s ± 87 ms +2.78%
threejs_development-mode_10x + exec 2 s ± 24 ms 2.01 s ± 15 ms +0.27%
threejs_development-mode_10x_hmr + exec 1.31 s ± 4.5 ms 1.33 s ± 5.6 ms +1.63%
threejs_production-mode_10x + exec 6.1 s ± 128 ms 5.99 s ± 29 ms -1.89%

Please sign in to comment.