Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 34 additions & 57 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde = "1.0.207"
serde_json = "1.0.125"
regex = "1.10.6"
once_cell = "1.19.0"
swc_core = { version = "45.0.2", features = [
swc_core = { version = "46.0.3", features = [
"ecma_plugin_transform",
"ecma_utils",
"ecma_visit",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Below is a table referencing the swc_core version used during the plugin build,
| `5.6.0` ~ `5.6.1` | [`27.0.6`](https://plugins.swc.rs/versions/range/364) |
| `5.7.0` | [`39.0.3`](https://plugins.swc.rs/versions/range/426) |
| `5.8.0` | [`45.0.2`](https://plugins.swc.rs/versions/range/497) |
| `5.9.0` | [`46.0.3`](https://plugins.swc.rs/versions/range/713) |

> **Note**
> next `v13.2.4` ~ `v13.3.1` cannot execute SWC Wasm plugins, due to a [bug of next-swc](https://github.com/vercel/next.js/issues/46989#issuecomment-1486989081).
Expand Down
19 changes: 11 additions & 8 deletions src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ pub fn get_local_ident_from_object_pat_prop(
pub fn get_jsx_attr_value_as_string(val: &JSXAttrValue) -> Option<String> {
match val {
// offset="5"
JSXAttrValue::Lit(Lit::Str(Str { value, .. })) => Some(value.to_string()),
JSXAttrValue::Str(Str { value, .. }) => Some(value.to_string_lossy().into_owned()),
// offset={..}
JSXAttrValue::JSXExprContainer(JSXExprContainer {
expr: JSXExpr::Expr(expr),
..
}) => {
match expr.as_ref() {
// offset={"5"}
Expr::Lit(Lit::Str(Str { value, .. })) => Some(value.to_string()),
Expr::Lit(Lit::Str(Str { value, .. })) => {
Some(value.to_string_lossy().into_owned())
}
// offset={5}
Expr::Lit(Lit::Num(Number { value, .. })) => Some(value.to_string()),
_ => None,
Expand All @@ -65,7 +67,7 @@ pub fn get_jsx_attr_value_as_string(val: &JSXAttrValue) -> Option<String> {
pub fn get_expr_as_string(val: &Expr) -> Option<String> {
match val {
// "Hello"
Expr::Lit(Lit::Str(Str { value, .. })) => Some(value.to_string()),
Expr::Lit(Lit::Str(Str { value, .. })) => Some(value.to_string_lossy().into_owned()),

// `Hello`
Expr::Tpl(Tpl { quasis, .. }) => {
Expand Down Expand Up @@ -136,14 +138,15 @@ pub fn get_object_prop<'a>(props: &'a [PropOrSpread], name: &str) -> Option<&'a
.filter_map(|prop_or_spread| to_key_value_prop(prop_or_spread))
.find(|prop| {
get_prop_key(prop)
.and_then(|key| if key == name { Some(key) } else { None })
.is_some()
.map(|key| key.as_str() == name)
.unwrap_or(false)
})
}

pub fn get_prop_key(prop: &KeyValueProp) -> Option<&Atom> {
pub fn get_prop_key(prop: &KeyValueProp) -> Option<Atom> {
match &prop.key {
PropName::Ident(IdentName { sym, .. }) | PropName::Str(Str { value: sym, .. }) => Some(sym),
PropName::Ident(IdentName { sym, .. }) => Some(sym.clone()),
PropName::Str(Str { value, .. }) => Some(value.to_string_lossy().into_owned().into()),
_ => None,
}
}
Expand Down Expand Up @@ -178,7 +181,7 @@ pub fn create_import(source: Atom, imported: IdentName, local: IdentName) -> Mod
})],
src: Box::new(Str {
span: DUMMY_SP,
value: source,
value: source.to_string().into(),
raw: None,
}),
with: None,
Expand Down
12 changes: 7 additions & 5 deletions src/jsx_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ impl TransJSXVisitor<'_> {

match attr_value {
// some="# books"
JSXAttrValue::Lit(Lit::Str(str)) => {
let string: String = str.value.clone().to_string();
JSXAttrValue::Str(str) => {
let string: String = str.value.to_string_lossy().into_owned();
tokens.push(MsgToken::String(string));
}

Expand All @@ -121,8 +121,9 @@ impl TransJSXVisitor<'_> {
}) => {
match exp.as_ref() {
// some={"# books"}
Expr::Lit(Lit::Str(str)) => tokens
.push(MsgToken::String(str.value.clone().to_string())),
Expr::Lit(Lit::Str(str)) => tokens.push(MsgToken::String(
str.value.to_string_lossy().into_owned(),
)),
// some={`# books ${name}`}
Expr::Tpl(tpl) => {
tokens.extend(self.ctx.tokenize_tpl(tpl));
Expand Down Expand Up @@ -220,7 +221,8 @@ impl Visit for TransJSXVisitor<'_> {
if let JSXExpr::Expr(exp) = &cont.expr {
match exp.as_ref() {
Expr::Lit(Lit::Str(str)) => {
self.tokens.push(MsgToken::String(str.value.to_string()));
self.tokens
.push(MsgToken::String(str.value.to_string_lossy().into_owned()));
}

// todo write tests and validate
Expand Down
Loading