diff --git a/Cargo.lock b/Cargo.lock index 736fdb0..c44d5ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,12 +50,6 @@ version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" -[[package]] -name = "arrayvec" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" - [[package]] name = "ascii" version = "1.1.0" @@ -488,14 +482,15 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" [[package]] name = "hstr" -version = "2.1.0" +version = "3.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31f11d91d7befd2ffd9d216e9e5ea1fae6174b20a2a1b67a688138003d2f4122" +checksum = "0c43c0a9e8fbdb3bb9dc8eee85e1e2ac81605418b4c83b6b7413cbf14d56ca5c" dependencies = [ "hashbrown 0.14.5", "new_debug_unreachable", "once_cell", "rustc-hash", + "serde", "triomphe", ] @@ -1342,9 +1337,9 @@ dependencies = [ [[package]] name = "swc_atoms" -version = "7.0.0" +version = "8.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3500dcf04c84606b38464561edc5e46f5132201cb3e23cf9613ed4033d6b1bb2" +checksum = "b40c2b43a19b5d0706aca8669ae5b77b92bd141f7f8ce5e980e0e52430f54b20" dependencies = [ "bytecheck", "hstr", @@ -1356,9 +1351,9 @@ dependencies = [ [[package]] name = "swc_common" -version = "15.0.0" +version = "16.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13be0317490fc330a53ee9e64b26891503c35336b509eb69c4fc1dc4e0119ff9" +checksum = "09e51fecd32bb0989543f0a64f4103cbd728e375838be83d768ce6989f5ea631" dependencies = [ "anyhow", "ast_node", @@ -1388,9 +1383,9 @@ dependencies = [ [[package]] name = "swc_core" -version = "45.0.2" +version = "46.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54e3f7a14efb82fd970b90afb1fc1afe34d969ad4b2adc4a63803cfc95249f08" +checksum = "f062270a2c008b097af0f2f512fb7f6137c3ef26527fcfa7e1477acc7dc78bba" dependencies = [ "swc_allocator", "swc_atoms", @@ -1410,9 +1405,9 @@ dependencies = [ [[package]] name = "swc_ecma_ast" -version = "16.0.0" +version = "17.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add9298e06af471f29aea2f8d1b6232885bd2a634521d0e95dc9d5bde3d39d3d" +checksum = "7da8bb0e5aaa6e077f178a28d29bc7da4a8ddaf012b3c21c043cb5f72a0b9779" dependencies = [ "bitflags", "bytecheck", @@ -1432,9 +1427,9 @@ dependencies = [ [[package]] name = "swc_ecma_codegen" -version = "18.0.0" +version = "19.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c70760095f23e70a295bc86dd52d454f5de4050621c9e27681ae26d166b77be4" +checksum = "43b756350060f51856d6d1f6ce63183b299d783d9d4458c1ecd6a3d72f4acf7e" dependencies = [ "ascii", "compact_str", @@ -1466,12 +1461,11 @@ dependencies = [ ] [[package]] -name = "swc_ecma_lexer" -version = "24.0.1" +name = "swc_ecma_parser" +version = "26.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d64b5db3b7cdba77d4063f7a0747333685cfb843fa2260d0c0765eff4bc82d0b" +checksum = "6ac3281dd9eef03b877fe9cef75a4c8951ce6df0c5f381868f302ee3c58fa6e2" dependencies = [ - "arrayvec", "bitflags", "either", "num-bigint", @@ -1479,7 +1473,6 @@ dependencies = [ "rustc-hash", "seq-macro", "serde", - "smallvec", "smartstring", "stacker", "swc_atoms", @@ -1488,27 +1481,11 @@ dependencies = [ "tracing", ] -[[package]] -name = "swc_ecma_parser" -version = "25.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8a43a89976cdbb42f152cfbd89a430b1fb693a9e0f2b5e2b1959466a88c3de0" -dependencies = [ - "either", - "num-bigint", - "serde", - "swc_atoms", - "swc_common", - "swc_ecma_ast", - "swc_ecma_lexer", - "tracing", -] - [[package]] name = "swc_ecma_testing" -version = "16.0.0" +version = "17.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "464be50bb5a907f43cf3559ccecec38bd69f91b580bcdbda9a65a35d8627b7f3" +checksum = "26ba3446b9060debb0aa7f722b9bcdaf7865f88a91ab1e77f3b35f11f7935d3a" dependencies = [ "anyhow", "hex", @@ -1519,9 +1496,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_base" -version = "28.0.1" +version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f892bb70d780486b86485493a3c7f4df8d84d5caba7e1d83e2e700836c29d613" +checksum = "0e757ebf73dcab085bed9d1290bbe387c4cf889e21e105b4f480cbafac865ed9" dependencies = [ "better_scoped_tls", "indexmap", @@ -1541,9 +1518,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_testing" -version = "31.0.0" +version = "32.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba723011d5a35faa99e61ca0de21f6c61c3449423b3709d7dce52b7ac4d10aa4" +checksum = "43c95e674bc46c27db53aaa9b293fcfdb10b65a0fe02d33be1106ea6d0ad3b1e" dependencies = [ "ansi_term", "anyhow", @@ -1567,9 +1544,9 @@ dependencies = [ [[package]] name = "swc_ecma_utils" -version = "22.0.0" +version = "23.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "110befba503dcec034023f4ad3559b84991c7a4be977287300db9562955bda1d" +checksum = "6c17da9ae2d3ad51e865bb27aa97f68b89441ef0b6ee1ba507913c412303c9b7" dependencies = [ "indexmap", "num_cpus", @@ -1586,9 +1563,9 @@ dependencies = [ [[package]] name = "swc_ecma_visit" -version = "16.0.0" +version = "17.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8763b91f52a54d5836c1f922dd393b157d47c121c7aab87308f582daf345f77" +checksum = "d6e6fea33cf8e654d46998cb65bf2915d3dbaab869a25f0ae2c70a86f1e7c2a4" dependencies = [ "new_debug_unreachable", "num-bigint", @@ -1612,9 +1589,9 @@ dependencies = [ [[package]] name = "swc_error_reporters" -version = "17.0.0" +version = "18.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61f991c703dea8982ebfb955a0b1c35fdfe551b24d8d13039c5a0b66e381edbd" +checksum = "f8457a012c93109582b926c97716ff4408923bd54690a8b1fd6b138b1b6334cd" dependencies = [ "anyhow", "miette", @@ -1656,9 +1633,9 @@ dependencies = [ [[package]] name = "swc_plugin_proxy" -version = "16.0.0" +version = "17.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef02609fd5dc946a13448833f8443173f45ff1477eb7e3683c0abb03018cdf20" +checksum = "5aa8c82358eebd41d96ffe6f9e8d8ebb77218e1e44ec9bd5b9d986a060ae896e" dependencies = [ "better_scoped_tls", "bytecheck", @@ -1702,9 +1679,9 @@ dependencies = [ [[package]] name = "swc_transform_common" -version = "9.0.0" +version = "10.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b0ccbb3fe8a93a2bbe6df94c205a0ca909bb8786705031d355904efbb89a2a3" +checksum = "ac052dc4f163680187023eaad6737cfeec2f7b69ac063bb004b3a4cc52407924" dependencies = [ "better_scoped_tls", "rustc-hash", @@ -1774,9 +1751,9 @@ dependencies = [ [[package]] name = "testing" -version = "16.0.0" +version = "17.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59f0f5f27bd9f0c9429a9330a223bc654804dcd9797f3414a491ac5190bf3cbc" +checksum = "e6071e9f3c50d975c85e606f2cc37c3a3ccff34cafc065f412fe7e04b94ae944" dependencies = [ "cargo_metadata", "difference", diff --git a/Cargo.toml b/Cargo.toml index 3521831..7e53672 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/README.md b/README.md index ff88505..3de546a 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/src/ast_utils.rs b/src/ast_utils.rs index c8673e5..bf850d3 100644 --- a/src/ast_utils.rs +++ b/src/ast_utils.rs @@ -44,7 +44,7 @@ pub fn get_local_ident_from_object_pat_prop( pub fn get_jsx_attr_value_as_string(val: &JSXAttrValue) -> Option { 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), @@ -52,7 +52,9 @@ pub fn get_jsx_attr_value_as_string(val: &JSXAttrValue) -> Option { }) => { 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, @@ -65,7 +67,7 @@ pub fn get_jsx_attr_value_as_string(val: &JSXAttrValue) -> Option { pub fn get_expr_as_string(val: &Expr) -> Option { 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, .. }) => { @@ -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 { 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, } } @@ -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, diff --git a/src/jsx_visitor.rs b/src/jsx_visitor.rs index 641e828..8e279a9 100644 --- a/src/jsx_visitor.rs +++ b/src/jsx_visitor.rs @@ -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)); } @@ -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)); @@ -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 diff --git a/src/macro_utils.rs b/src/macro_utils.rs index 6db76fb..e96f057 100644 --- a/src/macro_utils.rs +++ b/src/macro_utils.rs @@ -61,8 +61,9 @@ impl MacroCtx { /// is given ident exported from @lingui/macro? pub fn is_lingui_ident(&self, name: &str, ident: &Ident) -> bool { + let name_atom: Atom = name.into(); self.symbol_to_id_map - .get(&name.into()) + .get(&name_atom) .and_then(|refs| refs.get(&ident.to_id())) .is_some() } @@ -132,13 +133,12 @@ impl MacroCtx { let mut tokens: Vec = Vec::with_capacity(tpl.quasis.len()); for (i, tpl_element) in tpl.quasis.iter().enumerate() { - tokens.push(MsgToken::String( - tpl_element - .cooked - .as_ref() - .unwrap_or(&tpl_element.raw) - .to_string(), - )); + let value = tpl_element + .cooked + .as_ref() + .map(|c| c.to_string_lossy().into_owned()) + .unwrap_or_else(|| tpl_element.raw.to_string()); + tokens.push(MsgToken::String(value)); if let Some(exp) = tpl.exprs.get(i) { if let Expr::Call(call) = exp.as_ref() { @@ -209,7 +209,9 @@ impl MacroCtx { pub fn try_tokenize_expr(&self, expr: &Expr) -> Option> { match expr { // String Literal: "has # friend" - Expr::Lit(Lit::Str(str)) => Some(vec![MsgToken::String(str.clone().value.to_string())]), + Expr::Lit(Lit::Str(str)) => Some(vec![MsgToken::String( + str.value.to_string_lossy().into_owned(), + )]), // Template Literal: `${name} has # friend` Expr::Tpl(tpl) => Some(self.tokenize_tpl(tpl)), @@ -227,18 +229,12 @@ impl MacroCtx { pub fn get_js_choice_case_key(&self, prop: &KeyValueProp) -> Option { match &prop.key { // {one: ""} - PropName::Ident(IdentName { sym, .. }) + PropName::Ident(IdentName { sym, .. }) => Some(sym.clone()), // {"one": ""} - | PropName::Str(Str { value: sym, .. }) => { - Some(sym.clone()) - } + PropName::Str(Str { value, .. }) => Some(value.to_string_lossy().into_owned().into()), // {0: ""} -> `={number}` - PropName::Num(Number { value, .. }) => { - Some(format!("={value}").into()) - } - _ => { - None - } + PropName::Num(Number { value, .. }) => Some(format!("={value}").into()), + _ => None, } }