diff --git a/Cargo.lock b/Cargo.lock index 835f64179fa8..99729f48d46a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,7 +224,7 @@ version = "4.0.0" dependencies = [ "quote", "swc_macros_common", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -235,7 +235,7 @@ checksum = "a27b8a3a6e1a44fa4c8baf1f653e4172e81486d4941f2237e20dc2d0cf4ddff1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -257,7 +257,7 @@ checksum = "3c87f3f15e7794432337fc718554eaa4dc8f04c9677a950ffe366f20a162ae42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -336,7 +336,7 @@ dependencies = [ "regex", "rustc-hash 1.1.0", "shlex", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -504,7 +504,7 @@ checksum = "efb7846e0cb180355c2dec69e721edafa36919850f1a9f52ffba4ebc0393cb71" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -655,6 +655,11 @@ dependencies = [ "rustversion", ] +[[package]] +name = "cbor4ii" +version = "1.1.1" +source = "git+https://github.com/quininer/cbor4ii?branch=f%2Fderive#1084f6edc73a98b975533b81b6f570aca73550f2" + [[package]] name = "cc" version = "1.2.13" @@ -811,7 +816,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1426,7 +1431,7 @@ dependencies = [ "proc-macro2", "quote", "strsim 0.11.1", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1448,7 +1453,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core 0.20.10", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1539,7 +1544,7 @@ checksum = "30542c1ad912e0e3d22a1935c290e12e8a29d704a420177a31faad4a601a0800" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1581,7 +1586,7 @@ dependencies = [ "darling 0.20.10", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1601,7 +1606,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "206868b8242f27cecce124c19fd88157fbd0dd334df2587f36417bafbc85097b" dependencies = [ "derive_builder_core 0.20.0", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1612,7 +1617,7 @@ checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1632,7 +1637,7 @@ checksum = "bda628edc44c4bb645fbe0f758797143e4e07926f7ebf4e9bdfbd3d2ce621df3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", "unicode-xid", ] @@ -1684,7 +1689,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1800,7 +1805,7 @@ dependencies = [ "darling 0.20.10", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1911,7 +1916,7 @@ name = "from_variant" version = "2.0.2" dependencies = [ "swc_macros_common", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -1993,7 +1998,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -2036,7 +2041,7 @@ dependencies = [ "proc-macro2", "quote", "swc_config", - "syn 2.0.101", + "syn 2.0.106", "walkdir", ] @@ -2518,7 +2523,7 @@ checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -2663,7 +2668,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -2733,6 +2738,7 @@ dependencies = [ name = "jsdoc" version = "15.0.0" dependencies = [ + "cbor4ii", "dashmap 5.5.3", "nom 7.1.3", "serde", @@ -3100,7 +3106,7 @@ checksum = "dcf09caffaac8068c346b6df2a7fc27a177fd20b39421a39ce0a211bde679a6c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3168,7 +3174,7 @@ checksum = "1bb5c1d8184f13f7d0ccbeeca0def2f9a181bce2624302793005f5ca8aa62e5e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3204,7 +3210,7 @@ dependencies = [ "napi-derive-backend", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3217,7 +3223,7 @@ dependencies = [ "proc-macro2", "quote", "semver", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3414,7 +3420,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3487,7 +3493,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3658,7 +3664,7 @@ dependencies = [ "phf_shared 0.11.3", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3696,7 +3702,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3844,7 +3850,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" dependencies = [ "proc-macro2", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3900,7 +3906,7 @@ dependencies = [ "proc-macro-error-attr2", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3958,7 +3964,7 @@ checksum = "ca414edb151b4c8d125c12566ab0d74dc9cdba36fb80eb7b848c15f495fd32d1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -3992,7 +3998,7 @@ checksum = "938543690519c20c3a480d20a8efcc8e69abeb44093ab1df4e7c1f81f26c677a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -4256,7 +4262,7 @@ checksum = "beb382a4d9f53bd5c0be86b10d8179c3f8a14c30bf774ff77096ed6581e35981" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -4429,7 +4435,7 @@ dependencies = [ "proc-macro2", "quote", "serde_derive_internals", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -4537,7 +4543,7 @@ checksum = "0ea936adf78b1f766949a4977b91d2f5595825bd6ec079aa9543ad2685fc4516" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -4548,7 +4554,7 @@ checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -4669,7 +4675,7 @@ checksum = "a5e9bca0d4a99ce6c7296fea5972a9327a5e70c062b73f38e11c7894cf7ec72c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -4804,7 +4810,7 @@ checksum = "9cf483ea7e0e3a03d1b91687895814425149ad77facd3e2b6839dde26da98454" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -4844,7 +4850,7 @@ dependencies = [ "quote", "serde", "swc_macros_common", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -4951,6 +4957,7 @@ version = "7.0.0" dependencies = [ "arbitrary", "bytecheck 0.8.1", + "cbor4ii", "hstr", "once_cell", "rancor", @@ -5032,6 +5039,7 @@ dependencies = [ "better_scoped_tls", "bytecheck 0.8.1", "bytes-str", + "cbor4ii", "either", "from_variant", "new_debug_unreachable", @@ -5108,7 +5116,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -5187,6 +5195,7 @@ name = "swc_css_ast" version = "15.0.0" dependencies = [ "bytecheck 0.8.1", + "cbor4ii", "is-macro", "rkyv", "serde", @@ -5218,7 +5227,7 @@ name = "swc_css_codegen_macros" version = "1.0.2" dependencies = [ "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -5357,6 +5366,7 @@ dependencies = [ "arbitrary", "bitflags 2.6.0", "bytecheck 0.8.1", + "cbor4ii", "is-macro", "num-bigint", "once_cell", @@ -5409,7 +5419,7 @@ version = "2.0.2" dependencies = [ "proc-macro2", "swc_macros_common", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -5732,6 +5742,7 @@ dependencies = [ name = "swc_ecma_parser" version = "25.0.0" dependencies = [ + "cbor4ii", "codspeed-criterion-compat", "either", "num-bigint", @@ -5800,7 +5811,7 @@ dependencies = [ "swc_ecma_ast", "swc_ecma_parser", "swc_macros_common", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -5834,6 +5845,7 @@ name = "swc_ecma_regexp_ast" version = "0.4.0" dependencies = [ "bitflags 2.6.0", + "cbor4ii", "is-macro", "serde", "swc_atoms", @@ -5962,7 +5974,7 @@ dependencies = [ "proc-macro2", "quote", "swc_macros_common", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6188,7 +6200,7 @@ version = "1.0.1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6263,6 +6275,7 @@ name = "swc_html_ast" version = "15.0.0" dependencies = [ "bytecheck 0.8.1", + "cbor4ii", "is-macro", "rkyv", "serde", @@ -6293,7 +6306,7 @@ name = "swc_html_codegen_macros" version = "1.0.1" dependencies = [ "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6372,7 +6385,7 @@ version = "1.0.1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6491,7 +6504,7 @@ version = "1.1.0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6561,7 +6574,7 @@ name = "swc_trace_macro" version = "2.0.2" dependencies = [ "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6636,6 +6649,7 @@ dependencies = [ name = "swc_xml_ast" version = "15.0.0" dependencies = [ + "cbor4ii", "is-macro", "serde", "string_enum", @@ -6663,7 +6677,7 @@ name = "swc_xml_codegen_macros" version = "1.0.1" dependencies = [ "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6704,9 +6718,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.101" +version = "2.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" +checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" dependencies = [ "proc-macro2", "quote", @@ -6727,7 +6741,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6883,7 +6897,7 @@ dependencies = [ "quote", "regex", "relative-path", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6924,7 +6938,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -6935,7 +6949,7 @@ checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -7060,7 +7074,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -7170,7 +7184,7 @@ checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -7638,7 +7652,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", "wasm-bindgen-shared", ] @@ -7673,7 +7687,7 @@ checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -8227,7 +8241,7 @@ checksum = "342b0466f92b7217a4de9e114175fedee1907028567d2548bcd42f71a8b5b016" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -8357,7 +8371,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", "witx", ] @@ -8369,7 +8383,7 @@ checksum = "f5872fbe512b73acd514e7ef5bd5aee0ff951a12c1fed0293e1f7992de30df9f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", "wiggle-generate", ] @@ -8725,7 +8739,7 @@ checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", "synstructure", ] @@ -8756,7 +8770,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -8767,7 +8781,7 @@ checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] [[package]] @@ -8787,7 +8801,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", "synstructure", ] @@ -8816,5 +8830,5 @@ checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn 2.0.106", ] diff --git a/Cargo.toml b/Cargo.toml index 8010457114be..a985fe5c988c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -129,6 +129,7 @@ resolver = "2" foldhash = "0.1" precomputed-map = "0.2" + cbor4ii = { git = "https://github.com/quininer/cbor4ii", branch = "f/derive" } [workspace.metadata.cargo-shear] # `serde` is used when #[ast_node] is expanded diff --git a/crates/ast_node/src/encoding/decode.rs b/crates/ast_node/src/encoding/decode.rs new file mode 100644 index 000000000000..67168f41f7b2 --- /dev/null +++ b/crates/ast_node/src/encoding/decode.rs @@ -0,0 +1,278 @@ +use syn::{spanned::Spanned, Data, DeriveInput}; + +use super::{is_unknown, is_with, EnumType}; + +pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl { + match data { + Data::Struct(data) => { + let is_named = data.fields.iter().any(|field| field.ident.is_some()); + let names = data + .fields + .iter() + .enumerate() + .map(|(idx, field)| match field.ident.as_ref() { + Some(name) => name.clone(), + None => { + let name = format!("unit{idx}"); + syn::Ident::new(&name, field.span()) + } + }) + .collect::>(); + + let fields = data.fields.iter() + .zip(names.iter()) + .map(|(field, field_name)| -> syn::Stmt { + let ty = &field.ty; + let value: syn::Expr = match is_with(&field.attrs) { + Some(with_type) => syn::parse_quote!(<#with_type<#ty> as cbor4ii::core::dec::Decode<'_>>::decode(reader)?.0), + None => syn::parse_quote!(<#ty as cbor4ii::core::dec::Decode<'_>>::decode(reader)?) + }; + + syn::parse_quote!{ + let #field_name = #value; + } + }); + let build_struct: syn::Expr = if is_named { + syn::parse_quote! { #ident { #(#names),* } } + } else { + syn::parse_quote! { #ident ( #(#names),* ) } + }; + + let count = data.fields.len(); + let head: Option = (count != 1).then(|| { + syn::parse_quote! { + let len = >::len(reader)?.unwrap(); + } + }); + let tail: Option = head.is_some().then(|| { + syn::parse_quote! { + // ignore unknown field + for _ in 0..(len - #count) { + cbor4ii::core::dec::IgnoredAny::decode(reader)?; + } + } + }); + + syn::parse_quote! { + impl<'de> cbor4ii::core::dec::Decode<'de> for #ident { + #[inline] + fn decode>(reader: &mut R) + -> Result> + { + #head; + let value = { + #(#fields)* + #build_struct + }; + #tail + Ok(value) + } + } + } + } + Data::Enum(data) => { + let enum_type = data.variants.iter().filter(|v| !is_unknown(&v.attrs)).fold( + None, + |mut sum, next| { + let ty = match &next.fields { + syn::Fields::Named(_) => EnumType::Struct, + syn::Fields::Unnamed(fields) if fields.unnamed.len() == 1 => EnumType::One, + syn::Fields::Unit => EnumType::Unit, + syn::Fields::Unnamed(_) => { + panic!("more than 1 unnamed member field are not allowed") + } + }; + match (*sum.get_or_insert(ty), ty) { + (EnumType::Struct, EnumType::Struct) + | (EnumType::Struct, EnumType::Unit) + | (EnumType::Unit, EnumType::Unit) + | (EnumType::One, EnumType::One) => (), + (EnumType::Unit, EnumType::One) + | (EnumType::One, EnumType::Unit) + | (_, EnumType::Struct) => sum = Some(EnumType::Struct), + _ => panic!("enum member types must be consistent: {:?}", (sum, ty)), + } + sum + }, + ); + let enum_type = enum_type.expect("enum cannot be empty"); + let mut iter = data.variants.iter().peekable(); + + let unknown_arm: Option = iter.next_if(|variant| is_unknown(&variant.attrs)) + .map(|unknown| { + let name = &unknown.ident; + assert!( + unknown.discriminant.is_none(), + "unknown member is not allowed custom discriminant" + ); + assert!( + is_with(&unknown.attrs).is_none(), + "unknown member is not allowed with type" + ); + + match &unknown.fields { + syn::Fields::Unnamed(fields) => match fields.unnamed.len() { + 1 => { + assert_eq!(enum_type, EnumType::Unit); + syn::parse_quote! { + tag => #ident::#name(tag), + } + } + 2 => { + assert_eq!(enum_type, EnumType::One); + let val_ty = &fields.unnamed[1].ty; + syn::parse_quote! { + tag => { + let tag: u32 = tag.try_into().map_err(|_| cbor4ii::core::error::DecodeError::CastOverflow { + name: &"tag", + })?; + let val = <#val_ty as cbor4ii::core::dec::Decode<'_>>::decode(reader)?; + #ident::#name(tag, val) + }, + } + } + _ => panic!("unknown member must be a tag and a value"), + }, + _ => panic!("named enum unsupported"), + } + }); + + if matches!(enum_type, EnumType::Struct) { + assert!( + unknown_arm.is_none(), + "struct enum does not allow unknown variants" + ); + } + + let mut discriminant: u32 = 0; + let fields = iter + .map(|field| -> syn::Arm { + match field.discriminant.as_ref() { + Some((_, syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }))) => { + discriminant = lit.base10_parse::().unwrap(); + }, + Some(_) => panic!("unsupported discriminant type"), + None => (), + }; + discriminant += 1; + let idx = discriminant as u64; + let name = &field.ident; + + assert!(!is_unknown(&field.attrs), "unknown member must be first"); + + match enum_type { + EnumType::Unit => { + assert!(is_with(&field.attrs).is_none(), "unit member is not allowed with type"); + syn::parse_quote!{ + #idx => #ident::#name, + } + }, + EnumType::One => { + let val_ty = &field.fields.iter().next().unwrap().ty; + let value: syn::Expr = match is_with(&field.attrs) { + Some(with_type) + => syn::parse_quote!(<#with_type<#val_ty> as cbor4ii::core::dec::Decode<'_>>::decode(reader)?.0), + None => syn::parse_quote!(<#val_ty as cbor4ii::core::dec::Decode<'_>>::decode(reader)?) + }; + + syn::parse_quote!{ + #idx => #ident::#name(#value), + } + }, + EnumType::Struct => { + let is_named = field.fields.iter().all(|field| field.ident.is_some()); + let names = field.fields.iter() + .enumerate() + .map(|(idx, field)| match field.ident.as_ref() { + Some(name) => name.clone(), + None => { + let name = format!("unit{idx}"); + syn::Ident::new(&name, field.span()) + } + }) + .collect::>(); + let count = field.fields.len(); + + let stmt = field.fields.iter() + .zip(names.iter()) + .map(|(field, field_name)| -> syn::Stmt { + let val_ty = &field.ty; + match is_with(&field.attrs) { + Some(with_type) => syn::parse_quote!{ + let #field_name = <#with_type<#val_ty> as cbor4ii::core::dec::Decode<'_>>::decode(reader)?.0; + }, + None => syn::parse_quote!{ + let #field_name = <#val_ty as cbor4ii::core::dec::Decode<'_>>::decode(reader)?; + } + } + }); + let build_struct: syn::Expr = if is_named { + syn::parse_quote! { #ident::#name { #(#names),* } } + } else { + syn::parse_quote! { #ident::#name ( #(#names),* ) } + }; + + syn::parse_quote!{ + #idx => { + let len = cbor4ii::core::types::Array::len(reader)?.unwrap(); + let value = { + #(#stmt)* + #build_struct + }; + + // ignore unknown field + for _ in 0..(len - #count) { + cbor4ii::core::dec::IgnoredAny::decode(reader)?; + } + + value + }, + } + } + } + }); + + let unknown_arm = match unknown_arm { + Some(arm) => arm, + None => { + syn::parse_quote! { + _ => { + let err = cbor4ii::core::error::DecodeError::Mismatch { + name: &stringify!(#ident), + found: 0 + }; + return Err(err); + } + } + } + }; + + let tag: syn::Stmt = if matches!(enum_type, EnumType::Unit) { + syn::parse_quote! { + let tag = >::decode(reader)?; + } + } else { + syn::parse_quote! { + let tag = >::tag(reader)?; + } + }; + + syn::parse_quote! { + impl<'de> cbor4ii::core::dec::Decode<'de> for #ident { + #[inline] + fn decode>(reader: &mut R) + -> Result> + { + #tag + let value = match tag { + #(#fields)* + #unknown_arm + }; + Ok(value) + } + } + } + } + Data::Union(_) => panic!("union unsupported"), + } +} diff --git a/crates/ast_node/src/encoding/encode.rs b/crates/ast_node/src/encoding/encode.rs new file mode 100644 index 000000000000..e0dcf28ac32d --- /dev/null +++ b/crates/ast_node/src/encoding/encode.rs @@ -0,0 +1,239 @@ +use syn::{spanned::Spanned, Data, DeriveInput}; + +use super::{is_unknown, is_with, EnumType}; + +pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl { + match data { + Data::Struct(data) => { + let fields = data + .fields + .iter() + .enumerate() + .map(|(idx, field)| -> syn::Stmt { + let fieldpath: syn::ExprField = match field.ident.as_ref() { + Some(name) => syn::parse_quote!(self.#name), + None => { + let name = format!("{idx}"); + let name = syn::LitInt::new(&name, field.span()); + syn::parse_quote!(self.#name) + } + }; + + match is_with(&field.attrs) { + Some(with_type) => syn::parse_quote! { + cbor4ii::core::enc::Encode::encode(&#with_type(&#fieldpath), writer)?; + }, + None => syn::parse_quote! { + cbor4ii::core::enc::Encode::encode(&#fieldpath, writer)?; + }, + } + }); + let count = data.fields.len(); + let head: Option = (count != 1).then(|| { + syn::parse_quote! { + >::bounded(#count, writer)?; + } + }); + + syn::parse_quote! { + impl cbor4ii::core::enc::Encode for #ident { + #[inline] + fn encode(&self, writer: &mut W) + -> Result<(), cbor4ii::core::error::EncodeError> + { + #head + #(#fields)*; + Ok(()) + } + } + } + } + Data::Enum(data) => { + let enum_type = data.variants.iter().filter(|v| !is_unknown(&v.attrs)).fold( + None, + |mut sum, next| { + let ty = match &next.fields { + syn::Fields::Named(_) => EnumType::Struct, + syn::Fields::Unnamed(fields) if fields.unnamed.len() == 1 => EnumType::One, + syn::Fields::Unit => EnumType::Unit, + syn::Fields::Unnamed(_) => { + panic!("more than 1 unnamed member field are not allowed") + } + }; + match (*sum.get_or_insert(ty), ty) { + (EnumType::Struct, EnumType::Struct) + | (EnumType::Struct, EnumType::Unit) + | (EnumType::Unit, EnumType::Unit) + | (EnumType::One, EnumType::One) => (), + (EnumType::Unit, EnumType::One) + | (EnumType::One, EnumType::Unit) + | (_, EnumType::Struct) => sum = Some(EnumType::Struct), + _ => panic!("enum member types must be consistent: {:?}", (sum, ty)), + } + sum + }, + ); + let enum_type = enum_type.expect("enum cannot be empty"); + let mut iter = data.variants.iter().peekable(); + + let unknown_arm: Option = iter.next_if(|variant| is_unknown(&variant.attrs)) + .map(|unknown| { + let name = &unknown.ident; + assert!( + unknown.discriminant.is_none(), + "unknown member is not allowed custom discriminant" + ); + assert!( + is_with(&unknown.attrs).is_none(), + "unknown member is not allowed with type" + ); + + match &unknown.fields { + syn::Fields::Unnamed(fields) => match fields.unnamed.len() { + 1 => { + assert_eq!(enum_type, EnumType::Unit); + syn::parse_quote! { + #ident::#name(tag) + => tag.encode(writer)?, + } + } + 2 => { + assert_eq!(enum_type, EnumType::One); + syn::parse_quote! { + #ident::#name(tag, value) + => cbor4ii::core::types::Tag((*tag).into(), &*value).encode(writer)?, + } + } + _ => panic!("unknown member must be a tag and a value"), + }, + _ => panic!("named enum unsupported"), + } + }); + + if matches!(enum_type, EnumType::Struct) { + assert!( + unknown_arm.is_none(), + "struct enum does not allow unknown variants" + ); + } + + let mut discriminant: u32 = 0; + let fields = iter.map(|field| -> syn::Arm { + match field.discriminant.as_ref() { + Some((_, syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }))) => { + discriminant = lit.base10_parse::().unwrap(); + }, + Some(_) => panic!("unsupported discriminant type"), + None => (), + }; + discriminant += 1; + let idx = discriminant; + let name = &field.ident; + + assert!( + !is_unknown(&field.attrs), + "unknown member must be first: {:?}", + field.attrs.len() + ); + + match enum_type { + EnumType::Unit => { + assert!( + is_with(&field.attrs).is_none(), + "unit member is not allowed with type" + ); + + syn::parse_quote! { + #ident::#name => #idx.encode(writer)?, + } + }, + EnumType::One => { + let value: syn::Expr = match is_with(&field.attrs) { + Some(ty) => syn::parse_quote! { + &#ty(&*value) + }, + None => syn::parse_quote! { + &*value + }, + }; + + syn::parse_quote! { + #ident::#name(value) => { + cbor4ii::core::types::Tag( + #idx.into(), + #value + ).encode(writer)?; + }, + } + }, + EnumType::Struct => { + let is_named = field.fields.iter().all(|field| field.ident.is_some()); + let names = field.fields.iter() + .enumerate() + .map(|(idx, field)| match field.ident.as_ref() { + Some(name) => name.clone(), + None => { + let name = format!("unit{idx}"); + syn::Ident::new(&name, field.span()) + } + }) + .collect::>(); + let stmt = field.fields.iter() + .zip(names.iter()) + .map(|(field, field_name)| -> syn::Stmt { + match is_with(&field.attrs) { + Some(ty) => syn::parse_quote! { + cbor4ii::core::enc::Encode::encode(&#ty(&*#field_name), writer)?; + }, + None => syn::parse_quote! { + cbor4ii::core::enc::Encode::encode(&*#field_name, writer)?; + }, + } + }); + let head: syn::Expr = match field.fields.len() { + 0 | 1 => syn::parse_quote!{{ + cbor4ii::core::types::Tag(#idx.into(), cbor4ii::core::types::Nothing).encode(writer)?; + }}, + n => syn::parse_quote!{{ + cbor4ii::core::types::Tag(#idx.into(), cbor4ii::core::types::Nothing).encode(writer)?; + cbor4ii::core::types::Array::bounded(#n, writer)?; + }} + }; + + if is_named { + syn::parse_quote!{ + #ident::#name { #(#names),* } => { + #head; + #(#stmt)* + }, + } + } else { + syn::parse_quote!{ + #ident::#name ( #(#names),* ) => { + #head; + #(#stmt)* + }, + } + } + } + } + }); + + syn::parse_quote! { + impl cbor4ii::core::enc::Encode for #ident { + #[inline] + fn encode(&self, writer: &mut W) + -> Result<(), cbor4ii::core::error::EncodeError> + { + match self { + #(#fields)* + #unknown_arm + } + Ok(()) + } + } + } + } + Data::Union(_) => panic!("union unsupported"), + } +} diff --git a/crates/ast_node/src/encoding/mod.rs b/crates/ast_node/src/encoding/mod.rs new file mode 100644 index 000000000000..46e59e62e1ae --- /dev/null +++ b/crates/ast_node/src/encoding/mod.rs @@ -0,0 +1,45 @@ +pub(crate) mod decode; +pub(crate) mod encode; + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum EnumType { + Unit, + One, + Struct, +} + +pub(crate) fn is_unknown(attrs: &[syn::Attribute]) -> bool { + attrs + .iter() + .filter(|attr| attr.path().is_ident("encoding")) + .any(|attr| { + let mut is_unknown = false; + attr.parse_nested_meta(|meta| { + is_unknown |= meta.path.is_ident("unknown"); + Ok(()) + }) + .unwrap(); + is_unknown + }) +} + +fn is_with(attrs: &[syn::Attribute]) -> Option { + attrs + .iter() + .filter(|attr| attr.path().is_ident("encoding")) + .find_map(|attr| { + let mut with_type = None; + attr.parse_nested_meta(|meta| { + if meta.path.is_ident("with") { + let val = meta.value()?; + let val: syn::LitStr = val.parse()?; + let val: syn::Path = val.parse()?; + with_type = Some(val); + } + + Ok(()) + }) + .ok()?; + with_type + }) +} diff --git a/crates/ast_node/src/enum_deserialize.rs b/crates/ast_node/src/enum_deserialize.rs index 0a1b0b275daa..2bbbee78aee7 100644 --- a/crates/ast_node/src/enum_deserialize.rs +++ b/crates/ast_node/src/enum_deserialize.rs @@ -36,6 +36,7 @@ pub fn expand( let tag_match_arms = data .variants .iter() + .filter(|v| !crate::encoding::is_unknown(&v.attrs)) .map(|variant| { let field_type = match variant.fields { Fields::Unnamed(ref fields) => { @@ -117,6 +118,10 @@ pub fn expand( let mut visit_bytes_arms = Vec::new(); for variant in &data.variants { + if crate::encoding::is_unknown(&variant.attrs) { + continue; + } + let tags = variant .attrs .iter() @@ -358,6 +363,7 @@ pub fn expand( let variants: Punctuated = { data.variants .iter() + .filter(|v| !crate::encoding::is_unknown(&v.attrs)) .cloned() .map(|variant| Variant { attrs: Default::default(), diff --git a/crates/ast_node/src/lib.rs b/crates/ast_node/src/lib.rs index ade3332a9eba..351afe17e03c 100644 --- a/crates/ast_node/src/lib.rs +++ b/crates/ast_node/src/lib.rs @@ -8,6 +8,7 @@ use swc_macros_common::prelude::*; use syn::{visit_mut::VisitMut, *}; mod ast_node_macro; +mod encoding; mod enum_deserialize; mod spanned; @@ -23,7 +24,7 @@ pub fn derive_spanned(input: proc_macro::TokenStream) -> proc_macro::TokenStream } /// Derives `serde::Deserialize` which is aware of `tag` based deserialization. -#[proc_macro_derive(DeserializeEnum, attributes(tag))] +#[proc_macro_derive(DeserializeEnum, attributes(tag, encoding))] pub fn derive_deserialize_enum(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let input = parse::(input).expect("failed to parse input as DeriveInput"); @@ -32,6 +33,24 @@ pub fn derive_deserialize_enum(input: proc_macro::TokenStream) -> proc_macro::To print("derive(DeserializeEnum)", item.into_token_stream()) } +#[proc_macro_derive(Encode, attributes(encoding))] +pub fn derive_encode(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + let input = + syn::parse::(input).expect("failed to parse input as DeriveInput"); + + let item = encoding::encode::expand(input); + print("derive(Encode)", item.into_token_stream()) +} + +#[proc_macro_derive(Decode, attributes(encoding))] +pub fn derive_decode(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + let input = + syn::parse::(input).expect("failed to parse input as DeriveInput"); + + let item = encoding::decode::expand(input); + print("derive(Decode)", item.into_token_stream()) +} + /// Derives `serde::Serialize` and `serde::Deserialize`. /// /// # Struct attributes @@ -156,8 +175,8 @@ pub fn ast_node( // we should use call_site let mut item = TokenStream::new(); - match input.data { - Data::Enum(..) => { + match &input.data { + Data::Enum(data) => { use syn::parse::Parser; let attrs = >::parse_terminated @@ -187,6 +206,35 @@ pub fn ast_node( None }; + let mut data = data.clone(); + if !has_no_unknown { + let unknown: syn::Variant = if data + .variants + .iter() + .all(|variant| variant.fields.is_empty()) + { + syn::parse_quote! { + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + #[from_variant(ignore)] + #[span(unknown)] + #[encoding(unknown)] + Unknown(u32) + } + } else { + syn::parse_quote! { + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + #[from_variant(ignore)] + #[span(unknown)] + #[encoding(unknown)] + Unknown(u32, swc_common::unknown::Unknown) + } + }; + + // insert unknown member + data.variants.insert(0, unknown); + input.data = Data::Enum(data); + } + item.extend(quote!( #[allow(clippy::derive_partial_eq_without_eq)] #[cfg_attr( @@ -229,6 +277,10 @@ pub fn ast_node( feature = "serde-impl", serde(untagged) )] + #[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) + )] #input )); } @@ -303,6 +355,10 @@ pub fn ast_node( serde(rename_all = "camelCase") )] #serde_rename + #[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) + )] #input )); diff --git a/crates/ast_node/src/spanned.rs b/crates/ast_node/src/spanned.rs index 2b11a0e1ced0..489b8be0d895 100644 --- a/crates/ast_node/src/spanned.rs +++ b/crates/ast_node/src/spanned.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] use swc_macros_common::prelude::*; -use syn::{parse::Parse, *}; +use syn::{parse::Parse, spanned::Spanned, *}; struct MyField { /// Name of the field. @@ -27,6 +27,21 @@ impl Parse for InputFieldAttr { } } +fn is_unknown(attrs: &[syn::Attribute]) -> bool { + attrs + .iter() + .filter(|attr| attr.path().is_ident("span")) + .any(|attr| { + let mut is_unknown = false; + attr.parse_nested_meta(|meta| { + is_unknown |= meta.path.is_ident("unknown"); + Ok(()) + }) + .unwrap(); + is_unknown + }) +} + impl MyField { fn from_field(f: &Field) -> Self { let mut lo = false; @@ -121,6 +136,12 @@ fn make_body_for_variant(v: &VariantBinder<'_>, bindings: Vec>) })) } + if is_unknown(v.attrs()) { + return Box::new(parse_quote_spanned! { v.data().span() => { + swc_common::DUMMY_SP + }}); + } + if bindings.is_empty() { panic!("#[derive(Spanned)] requires a field to get span from") } diff --git a/crates/from_variant/src/lib.rs b/crates/from_variant/src/lib.rs index 1f809dfedbf9..daa6c688befb 100644 --- a/crates/from_variant/src/lib.rs +++ b/crates/from_variant/src/lib.rs @@ -5,7 +5,7 @@ use syn::*; /// Derives [`From`] for all variants. This only supports an enum where every /// variant has a single field. -#[proc_macro_derive(FromVariant)] +#[proc_macro_derive(FromVariant, attributes(from_variant))] pub fn derive_from_variant(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let input = parse::(input).expect("failed to parse input as DeriveInput"); @@ -19,6 +19,21 @@ pub fn derive_from_variant(input: proc_macro::TokenStream) -> proc_macro::TokenS print("derive(FromVariant)", item) } +fn is_ignored(attrs: &[syn::Attribute]) -> bool { + attrs + .iter() + .filter(|attr| attr.path().is_ident("from_variant")) + .any(|attr| { + let mut is_unknown = false; + attr.parse_nested_meta(|meta| { + is_unknown |= meta.path.is_ident("ignore"); + Ok(()) + }) + .unwrap(); + is_unknown + }) +} + fn derive( DeriveInput { generics, @@ -35,6 +50,10 @@ fn derive( let mut from_impls: Vec = Vec::new(); for v in variants { + if is_ignored(&v.attrs) { + continue; + } + let variant_name = v.ident; match v.fields { Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => { diff --git a/crates/jsdoc/Cargo.toml b/crates/jsdoc/Cargo.toml index 1f3c6dd2d789..f1df6af1a04c 100644 --- a/crates/jsdoc/Cargo.toml +++ b/crates/jsdoc/Cargo.toml @@ -14,10 +14,12 @@ bench = false [features] serde-impl = ["serde"] +encoding-impl = ["cbor4ii", "swc_atoms/encoding-impl", "swc_common/encoding-impl"] [dependencies] nom = { workspace = true } serde = { workspace = true, features = ["derive"], optional = true } +cbor4ii = { workspace = true, features = [ "use_std" ], optional = true } swc_atoms = { version = "7.0.0", path = "../swc_atoms" } swc_common = { version = "15.0.0", path = "../swc_common" } diff --git a/crates/jsdoc/src/ast.rs b/crates/jsdoc/src/ast.rs index 7ac521b58773..d100c91ce109 100644 --- a/crates/jsdoc/src/ast.rs +++ b/crates/jsdoc/src/ast.rs @@ -29,7 +29,7 @@ pub struct TagItem { pub tag: Tag, } -#[ast_node] +#[ast_node(no_unknown)] pub enum Tag { #[tag("Yield")] Yield(YieldTag), @@ -205,7 +205,15 @@ pub struct JSDocClassDescTag { #[ast_node] pub struct ConstTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, } @@ -255,7 +263,15 @@ pub struct AuthorTag { #[derive(Eq)] pub struct ClassTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, } @@ -269,6 +285,10 @@ pub struct PublicTag { #[derive(Eq)] pub struct PrivateTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, } @@ -276,6 +296,10 @@ pub struct PrivateTag { #[derive(Eq)] pub struct ProtectedTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, } @@ -296,6 +320,10 @@ pub struct PropertyTag { pub span: Span, pub name_path: NamePath, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, pub desc: Text, } @@ -304,8 +332,16 @@ pub struct PropertyTag { pub struct ParameterTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, #[cfg_attr(feature = "serde-impl", serde(rename = "typeExpression"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, pub desc: Text, @@ -321,6 +357,10 @@ pub struct EnumTag { #[ast_node] pub struct ReturnTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, pub description: Text, } @@ -340,6 +380,10 @@ pub struct TypeTag { #[ast_node] pub struct TemplateTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub constraint: Option, } @@ -347,14 +391,26 @@ pub struct TemplateTag { pub struct TypedefTag { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(rename = "full_name"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub full_name: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, #[cfg_attr(feature = "serde-impl", serde(rename = "typeExpression"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_expr: Option, } -#[ast_node] +#[ast_node(no_unknown)] pub enum Type { /// `*` #[tag("AllType")] @@ -551,6 +607,10 @@ pub struct FileTag { #[ast_node] pub struct FunctionTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, } @@ -586,6 +646,10 @@ pub struct InstanceTag { #[ast_node] pub struct InterfaceTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, } #[ast_node] @@ -628,6 +692,10 @@ pub struct MemberOfTag { #[ast_node] pub struct TypeDefTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, pub name_path: NamePath, } @@ -660,7 +728,15 @@ pub struct NameTag { #[ast_node] pub struct NamespaceTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, } @@ -672,6 +748,10 @@ pub struct OverrideTag { #[ast_node] pub struct PackageTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ty: Option, } @@ -737,6 +817,10 @@ pub struct VersionTag { #[ast_node] pub struct YieldTag { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub value: Option, pub description: Text, } diff --git a/crates/swc_atoms/Cargo.toml b/crates/swc_atoms/Cargo.toml index e66a468c564e..756753bc650a 100644 --- a/crates/swc_atoms/Cargo.toml +++ b/crates/swc_atoms/Cargo.toml @@ -14,6 +14,7 @@ bench = false [features] __rkyv = [] rkyv-impl = ["__rkyv", "rkyv", "bytecheck", "rancor"] +encoding-impl = ["cbor4ii"] shrink-to-fit = ["dep:shrink-to-fit"] [dependencies] @@ -25,5 +26,6 @@ rancor = { workspace = true, optional = true } rkyv = { workspace = true, optional = true } serde = { workspace = true } shrink-to-fit = { workspace = true, optional = true } +cbor4ii = { workspace = true, features = [ "use_std" ], optional = true } hstr = { version = "2.1.0", path = "../hstr" } diff --git a/crates/swc_atoms/src/lib.rs b/crates/swc_atoms/src/lib.rs index 07c584f10c8a..051bf76d65d5 100644 --- a/crates/swc_atoms/src/lib.rs +++ b/crates/swc_atoms/src/lib.rs @@ -34,6 +34,28 @@ mod fast; #[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct Atom(hstr::Atom); +#[cfg(feature = "encoding-impl")] +impl cbor4ii::core::enc::Encode for Atom { + #[inline] + fn encode( + &self, + writer: &mut W, + ) -> Result<(), cbor4ii::core::enc::Error> { + self.as_str().encode(writer) + } +} + +#[cfg(feature = "encoding-impl")] +impl<'de> cbor4ii::core::dec::Decode<'de> for Atom { + #[inline] + fn decode>( + reader: &mut R, + ) -> Result> { + let s = <&str>::decode(reader)?; + Ok(Atom::new(s)) + } +} + #[cfg(feature = "arbitrary")] #[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))] impl<'a> arbitrary::Arbitrary<'a> for Atom { diff --git a/crates/swc_common/Cargo.toml b/crates/swc_common/Cargo.toml index b39eee10faf1..13e4357d28b6 100644 --- a/crates/swc_common/Cargo.toml +++ b/crates/swc_common/Cargo.toml @@ -15,6 +15,9 @@ version = "15.0.0" [lib] bench = false +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(swc_ast_unknown)'] } + [features] __plugin = [] __plugin_mode = [] @@ -42,6 +45,7 @@ rkyv-impl = [ "bytecheck", "rancor", ] +encoding-impl = ["cbor4ii", "swc_atoms/encoding-impl"] shrink-to-fit = ["dep:shrink-to-fit", "swc_atoms/shrink-to-fit"] @@ -67,6 +71,7 @@ termcolor = { workspace = true, optional = true } tracing = { workspace = true } unicode-width = { workspace = true } url = { workspace = true } +cbor4ii = { workspace = true, features = [ "use_std" ], optional = true } ast_node = { version = "4.0.0", path = "../ast_node" } better_scoped_tls = { version = "1.0.1", path = "../better_scoped_tls" } @@ -75,7 +80,6 @@ swc_atoms = { version = "7.0.0", path = "../swc_atoms" } swc_eq_ignore_macros = { version = "1.0.1", path = "../swc_eq_ignore_macros" } swc_visit = { version = "2.0.1", path = "../swc_visit" } - [dev-dependencies] par-iter = { workspace = true } serde_json = { workspace = true } diff --git a/crates/swc_common/src/lib.rs b/crates/swc_common/src/lib.rs index 8d09e8cc960d..bedc3a438677 100644 --- a/crates/swc_common/src/lib.rs +++ b/crates/swc_common/src/lib.rs @@ -32,7 +32,7 @@ use std::fmt::Debug; -pub use ast_node::{ast_node, ast_serde, DeserializeEnum, Spanned}; +pub use ast_node::{ast_node, ast_serde, Decode, DeserializeEnum, Encode, Spanned}; pub use from_variant::FromVariant; pub use swc_eq_ignore_macros::{EqIgnoreSpan, TypeEq}; @@ -69,6 +69,8 @@ pub mod serializer; pub mod source_map; pub mod sync; mod syntax_pos; +#[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] +pub mod unknown; pub mod util; #[cfg(all(not(debug_assertions), feature = "plugin-rt", feature = "plugin-mode"))] @@ -85,3 +87,9 @@ pub use self::syntax_pos::{ ArchivedBytePos, ArchivedCharPos, ArchivedFileName, ArchivedMultiSpan, ArchivedSourceFile, ArchivedSourceFileAndBytePos, ArchivedSpan, ArchivedSpanLinesError, ArchivedSpanSnippetError, }; + +#[cfg(swc_ast_unknown)] +#[track_caller] +pub fn unknown() -> ! { + panic!("unknown node") +} diff --git a/crates/swc_common/src/serializer.rs b/crates/swc_common/src/serializer.rs index 5449cfbd1abf..713c9e1802aa 100644 --- a/crates/swc_common/src/serializer.rs +++ b/crates/swc_common/src/serializer.rs @@ -1,5 +1,5 @@ #![allow(deprecated)] -#![deprecated = "Not used by swc, and this will be removed with next breaking change"] + use serde::Deserialize; #[derive(Deserialize)] @@ -16,3 +16,71 @@ pub struct Type { #[serde(rename = "type")] pub ty: String, } + +#[cfg(feature = "encoding-impl")] +pub struct WithChar(pub T); + +#[cfg(feature = "encoding-impl")] +impl cbor4ii::core::enc::Encode for WithChar<&char> { + fn encode( + &self, + writer: &mut W, + ) -> Result<(), cbor4ii::core::enc::Error> { + u32::from(*self.0).encode(writer) + } +} + +#[cfg(feature = "encoding-impl")] +impl<'de> cbor4ii::core::dec::Decode<'de> for WithChar { + fn decode>( + reader: &mut R, + ) -> Result> { + let n = u32::decode(reader)?; + let value = char::from_u32(n).ok_or_else(|| cbor4ii::core::dec::Error::Mismatch { + name: &"WithChar", + found: 0, + })?; + Ok(WithChar(value)) + } +} + +#[cfg(feature = "encoding-impl")] +pub struct ArrayOption(pub T); + +#[cfg(feature = "encoding-impl")] +impl cbor4ii::core::enc::Encode for ArrayOption<&'_ Vec>> { + fn encode( + &self, + writer: &mut W, + ) -> Result<(), cbor4ii::core::enc::Error> { + >::bounded(self.0.len(), writer)?; + for item in self.0.iter() { + cbor4ii::core::types::Maybe(item).encode(writer)?; + } + Ok(()) + } +} + +#[cfg(feature = "encoding-impl")] +impl<'de, T: cbor4ii::core::dec::Decode<'de>> cbor4ii::core::dec::Decode<'de> + for ArrayOption>> +{ + fn decode>( + reader: &mut R, + ) -> Result> { + let len = >::len(reader)?; + let Some(len) = len else { + return Err(cbor4ii::core::error::DecodeError::RequireLength { + name: &"array-option", + found: len + .map(cbor4ii::core::error::Len::new) + .unwrap_or(cbor4ii::core::error::Len::Indefinite), + }); + }; + let mut q = Vec::with_capacity(std::cmp::min(len, 128)); + for _ in 0..len { + q.push(>>::decode(reader)?.0); + } + Ok(ArrayOption(q)) + } +} diff --git a/crates/swc_common/src/syntax_pos.rs b/crates/swc_common/src/syntax_pos.rs index 3c1bbe5997e2..f1cb04b524cc 100644 --- a/crates/swc_common/src/syntax_pos.rs +++ b/crates/swc_common/src/syntax_pos.rs @@ -35,6 +35,10 @@ pub mod hygiene; )] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(C))] +#[cfg_attr( + feature = "encoding-impl", + derive(::ast_node::Encode, ::ast_node::Decode) +)] #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] pub struct Span { #[serde(rename = "start")] @@ -1012,6 +1016,27 @@ pub trait SmallPos { #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] pub struct BytePos(#[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub u32); +#[cfg(feature = "encoding-impl")] +impl cbor4ii::core::enc::Encode for BytePos { + #[inline] + fn encode( + &self, + writer: &mut W, + ) -> Result<(), cbor4ii::core::enc::Error> { + self.0.encode(writer) + } +} + +#[cfg(feature = "encoding-impl")] +impl<'de> cbor4ii::core::dec::Decode<'de> for BytePos { + #[inline] + fn decode>( + reader: &mut R, + ) -> Result> { + u32::decode(reader).map(BytePos) + } +} + impl BytePos { /// Dummy position. This is reserved for synthesized spans. pub const DUMMY: Self = BytePos(0); diff --git a/crates/swc_common/src/syntax_pos/hygiene.rs b/crates/swc_common/src/syntax_pos/hygiene.rs index 2de3cd470148..a76e589a5fc3 100644 --- a/crates/swc_common/src/syntax_pos/hygiene.rs +++ b/crates/swc_common/src/syntax_pos/hygiene.rs @@ -40,6 +40,27 @@ use crate::EqIgnoreSpan; #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] pub struct SyntaxContext(#[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] u32); +#[cfg(feature = "encoding-impl")] +impl cbor4ii::core::enc::Encode for SyntaxContext { + #[inline] + fn encode( + &self, + writer: &mut W, + ) -> Result<(), cbor4ii::core::enc::Error> { + self.0.encode(writer) + } +} + +#[cfg(feature = "encoding-impl")] +impl<'de> cbor4ii::core::dec::Decode<'de> for SyntaxContext { + #[inline] + fn decode>( + reader: &mut R, + ) -> Result> { + u32::decode(reader).map(SyntaxContext) + } +} + #[cfg(feature = "arbitrary")] #[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))] impl<'a> arbitrary::Arbitrary<'a> for SyntaxContext { diff --git a/crates/swc_common/src/unknown.rs b/crates/swc_common/src/unknown.rs new file mode 100644 index 000000000000..3a41664393ac --- /dev/null +++ b/crates/swc_common/src/unknown.rs @@ -0,0 +1,139 @@ +use cbor4ii::core::{dec, enc}; + +use crate::eq::EqIgnoreSpan; + +#[derive(Clone)] +pub struct Unknown(cbor4ii::core::BoxedRawValue); + +impl enc::Encode for Unknown { + fn encode(&self, writer: &mut W) -> Result<(), enc::Error> { + self.0.encode(writer) + } +} + +impl<'de> dec::Decode<'de> for Unknown { + fn decode>(reader: &mut R) -> Result> { + cbor4ii::core::BoxedRawValue::decode(reader).map(Unknown) + } +} + +impl PartialEq for Unknown { + fn eq(&self, other: &Self) -> bool { + self.0.as_bytes() == other.0.as_bytes() + } +} + +impl Eq for Unknown {} + +impl EqIgnoreSpan for Unknown { + fn eq_ignore_span(&self, other: &Self) -> bool { + self.0.as_bytes() == other.0.as_bytes() + } +} + +impl std::hash::Hash for Unknown { + fn hash(&self, state: &mut H) { + self.0.as_bytes().hash(state) + } +} + +#[cfg(feature = "rkyv-impl")] +#[derive(Debug)] +struct UnknownError; + +#[cfg(feature = "rkyv-impl")] +impl std::error::Error for UnknownError {} + +#[cfg(feature = "rkyv-impl")] +impl std::fmt::Display for UnknownError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "cannot access unknown type") + } +} + +#[cfg(feature = "rkyv-impl")] +impl rkyv::Archive for Unknown { + type Archived = std::marker::PhantomData; + type Resolver = (); + + fn resolve(&self, _resolver: Self::Resolver, _out: rkyv::Place) { + // + } +} + +/// NOT A PUBLIC API +#[cfg(feature = "rkyv-impl")] +impl rkyv::Serialize for Unknown +where + S: rancor::Fallible + rkyv::ser::Writer + ?Sized, + S::Error: rancor::Source, +{ + fn serialize(&self, _serializer: &mut S) -> Result { + Err(::new(UnknownError)) + } +} + +/// NOT A PUBLIC API +#[cfg(feature = "rkyv-impl")] +impl rkyv::Deserialize for std::marker::PhantomData +where + D: ?Sized + rancor::Fallible, + D::Error: rancor::Source, +{ + fn deserialize( + &self, + _deserializer: &mut D, + ) -> Result::Error> { + Err(::new(UnknownError)) + } +} + +impl std::fmt::Debug for Unknown { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Unknown") + } +} + +impl serde::Serialize for Unknown { + fn serialize(&self, _serializer: S) -> Result + where + S: serde::Serializer, + { + use serde::ser::Error; + + Err(S::Error::custom("cannot serialize unknown type")) + } +} + +impl<'de> serde::Deserialize<'de> for Unknown { + fn deserialize(_deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + use serde::de::Error; + + Err(D::Error::custom("cannot deserialize unknown type")) + } +} + +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for Unknown { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + use cbor4ii::core::{BoxedRawValue, Value}; + + let mut list = Vec::new(); + for _ in 0..u.arbitrary_len::()? { + let n = u.int_in_range::(0..=102410241024)?; + list.push(Value::Integer(n.into())); + } + let value = Value::Array(list); + let value = + BoxedRawValue::from_value(&value).map_err(|_| arbitrary::Error::IncorrectFormat)?; + Ok(Unknown(value)) + } +} + +#[cfg(feature = "shrink-to-fit")] +impl shrink_to_fit::ShrinkToFit for Unknown { + fn shrink_to_fit(&mut self) {} +} diff --git a/crates/swc_common/tests/attr_interop.rs b/crates/swc_common/tests/attr_interop.rs index 536a6617f027..4b94d4bbc3de 100644 --- a/crates/swc_common/tests/attr_interop.rs +++ b/crates/swc_common/tests/attr_interop.rs @@ -13,7 +13,7 @@ pub struct Class { } #[ast_node("Tuple")] -pub struct Tuple(#[span] HasSpan, usize, usize); +pub struct Tuple(#[span] HasSpan, u64, u64); #[derive(Debug, Clone, PartialEq, Eq, Spanned, Serialize, Deserialize)] #[cfg_attr( @@ -37,6 +37,10 @@ pub struct Tuple(#[span] HasSpan, usize, usize); )) )] #[cfg_attr(feature = "rkyv-impl", repr(C))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub struct HasSpan { #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub span: Span, diff --git a/crates/swc_css_ast/Cargo.toml b/crates/swc_css_ast/Cargo.toml index 2d18182d2fa7..ae57ea73ae91 100644 --- a/crates/swc_css_ast/Cargo.toml +++ b/crates/swc_css_ast/Cargo.toml @@ -22,6 +22,7 @@ rkyv-impl = [ "bytecheck", ] serde-impl = ["serde"] +encoding-impl = ["cbor4ii", "swc_atoms/encoding-impl", "swc_common/encoding-impl"] [dependencies] bytecheck = { workspace = true, optional = true } @@ -33,3 +34,4 @@ swc_atoms = { version = "7.0.0", path = "../swc_atoms" } swc_common = { version = "15.0.0", path = "../swc_common" } rkyv = { workspace = true, optional = true } +cbor4ii = { workspace = true, features = [ "use_std" ], optional = true } diff --git a/crates/swc_css_ast/src/at_rule.rs b/crates/swc_css_ast/src/at_rule.rs index cb530c76cf41..b986304de805 100644 --- a/crates/swc_css_ast/src/at_rule.rs +++ b/crates/swc_css_ast/src/at_rule.rs @@ -14,7 +14,15 @@ use crate::{ pub struct AtRule { pub span: Span, pub name: AtRuleName, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub prelude: Option>, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub block: Option, } @@ -94,8 +102,16 @@ pub enum AtRulePrelude { pub struct ScopeRange { pub span: Span, /// https://drafts.csswg.org/css-cascade-6/#typedef-scope-start + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub scope_start: Option, /// https://drafts.csswg.org/css-cascade-6/#typedef-scope-end + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub scope_end: Option, } @@ -184,7 +200,15 @@ pub enum KeyframeSelector { pub struct ImportPrelude { pub span: Span, pub href: Box, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub layer_name: Option>, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub import_conditions: Option>, } @@ -210,7 +234,15 @@ pub enum ImportLayerName { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct ImportConditions { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub supports: Option>, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub media: Option>, } @@ -218,6 +250,10 @@ pub struct ImportConditions { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct NamespacePrelude { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub prefix: Option, pub uri: Box, } @@ -242,9 +278,25 @@ pub struct MediaQueryList { #[derive(Eq, Hash)] pub struct MediaQuery { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub modifier: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub media_type: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub condition: Option>, } @@ -333,6 +385,10 @@ pub enum MediaConditionWithoutOrType { #[derive(Eq, Hash)] pub struct MediaNot { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, pub condition: MediaInParens, } @@ -347,6 +403,10 @@ impl EqIgnoreSpan for MediaNot { #[derive(Eq, Hash)] pub struct MediaAnd { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, pub condition: MediaInParens, } @@ -361,6 +421,10 @@ impl EqIgnoreSpan for MediaAnd { #[derive(Eq, Hash)] pub struct MediaOr { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, pub condition: MediaInParens, } @@ -451,6 +515,10 @@ pub struct MediaFeatureBoolean { )] #[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] //#[cfg_attr( // feature = "rkyv", // archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + @@ -522,6 +590,10 @@ pub enum SupportsConditionType { #[derive(Eq, Hash)] pub struct SupportsNot { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, pub condition: Box, } @@ -536,6 +608,10 @@ impl EqIgnoreSpan for SupportsNot { #[derive(Eq, Hash)] pub struct SupportsAnd { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, pub condition: Box, } @@ -550,6 +626,10 @@ impl EqIgnoreSpan for SupportsAnd { #[derive(Eq, Hash)] pub struct SupportsOr { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, pub condition: Box, } @@ -602,7 +682,15 @@ pub struct PageSelectorList { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct PageSelector { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub page_type: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub pseudos: Option>, } @@ -647,6 +735,10 @@ pub struct LayerNameList { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct ContainerCondition { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, pub query: ContainerQuery, } @@ -685,6 +777,10 @@ pub enum ContainerQueryType { #[derive(Eq, Hash)] pub struct ContainerQueryNot { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, pub query: QueryInParens, } @@ -699,6 +795,10 @@ impl EqIgnoreSpan for ContainerQueryNot { #[derive(Eq, Hash)] pub struct ContainerQueryAnd { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, pub query: QueryInParens, } @@ -713,6 +813,10 @@ impl EqIgnoreSpan for ContainerQueryAnd { #[derive(Eq, Hash)] pub struct ContainerQueryOr { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub keyword: Option, pub query: QueryInParens, } @@ -783,6 +887,10 @@ pub struct SizeFeatureBoolean { // archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + // rkyv::ser::Serializer")) //)] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum SizeFeatureRangeComparison { /// `<` Lt, @@ -853,6 +961,10 @@ pub enum SizeFeatureName { pub struct ExtensionName { pub span: Span, pub value: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } diff --git a/crates/swc_css_ast/src/base.rs b/crates/swc_css_ast/src/base.rs index 4638cf78a60a..ad0fa3f9b61c 100644 --- a/crates/swc_css_ast/src/base.rs +++ b/crates/swc_css_ast/src/base.rs @@ -303,6 +303,10 @@ pub struct Declaration { pub name: DeclarationName, pub value: Vec, /// The span includes `!` + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub important: Option, } diff --git a/crates/swc_css_ast/src/selector.rs b/crates/swc_css_ast/src/selector.rs index de5555b51e42..aa2a56f67401 100644 --- a/crates/swc_css_ast/src/selector.rs +++ b/crates/swc_css_ast/src/selector.rs @@ -96,6 +96,10 @@ pub enum ComplexSelectorChildren { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct RelativeSelector { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub combinator: Option, pub selector: ComplexSelector, } @@ -106,7 +110,15 @@ pub struct RelativeSelector { pub struct CompoundSelector { pub span: Span, /// "&" + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub nesting_selector: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_selector: Option>, pub subclass_selectors: Vec, } @@ -130,6 +142,10 @@ pub struct Combinator { )] #[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum CombinatorValue { /// ` ` Descendant, @@ -173,6 +189,10 @@ pub struct TagNameSelector { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct UniversalSelector { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub prefix: Option, } @@ -180,6 +200,10 @@ pub struct UniversalSelector { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct NamespacePrefix { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub namespace: Option, } @@ -209,6 +233,10 @@ pub struct AnyNamespace { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct WqName { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub prefix: Option, pub value: Ident, } @@ -253,8 +281,20 @@ pub struct ClassSelector { pub struct AttributeSelector { pub span: Span, pub name: WqName, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub matcher: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub value: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub modifier: Option, } @@ -270,6 +310,10 @@ pub struct AttributeSelector { // archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + // rkyv::ser::Serializer")) //)] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum AttributeSelectorMatcherValue { /// `=` Equals, @@ -319,6 +363,10 @@ pub struct AttributeSelectorModifier { pub struct PseudoClassSelector { pub span: Span, pub name: Ident, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub children: Option>, } @@ -375,9 +423,25 @@ pub enum AnPlusB { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct AnPlusBNotation { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub a: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub a_raw: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub b: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub b_raw: Option, } @@ -386,6 +450,10 @@ pub struct AnPlusBNotation { pub struct PseudoElementSelector { pub span: Span, pub name: Ident, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub children: Option>, } @@ -408,6 +476,10 @@ pub struct CustomHighlightName { pub span: Span, pub value: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } diff --git a/crates/swc_css_ast/src/token.rs b/crates/swc_css_ast/src/token.rs index ab89d6307a01..4e0eb5bdc287 100644 --- a/crates/swc_css_ast/src/token.rs +++ b/crates/swc_css_ast/src/token.rs @@ -30,6 +30,10 @@ impl Take for TokenAndSpan { )] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(C))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub struct UrlKeyValue(pub Atom, pub Atom); #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Is, EqIgnoreSpan)] @@ -45,6 +49,10 @@ pub struct UrlKeyValue(pub Atom, pub Atom); __S::Error: rkyv::rancor::Source)) )] #[cfg_attr(feature = "serde-impl", derive(Serialize, Deserialize))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum NumberType { #[cfg_attr(feature = "serde-impl", serde(rename = "integer"))] Integer, @@ -60,6 +68,10 @@ pub enum NumberType { #[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv", repr(C))] #[cfg_attr(feature = "serde-impl", derive(Serialize, Deserialize))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub struct DimensionToken { pub value: f64, pub raw_value: Atom, @@ -84,6 +96,10 @@ pub struct DimensionToken { __S::Error: rkyv::rancor::Source)) )] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum Token { Ident { value: Atom, @@ -122,6 +138,10 @@ pub enum Token { raw: Atom, }, Delim { + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "::swc_common::serializer::WithChar") + )] value: char, }, Number { @@ -134,7 +154,9 @@ pub enum Token { value: f64, raw: Atom, }, - Dimension(Box), + Dimension { + dimension: Box, + }, /// One or more whitespace. WhiteSpace { value: Atom, @@ -235,7 +257,7 @@ impl Hash for Token { integer_decode(*value).hash(state); raw.hash(state); } - Token::Dimension(dimension) => { + Token::Dimension { dimension } => { integer_decode(dimension.value).hash(state); dimension.unit.hash(state); dimension.type_flag.hash(state); diff --git a/crates/swc_css_ast/src/value.rs b/crates/swc_css_ast/src/value.rs index dbd8faa64c4d..4203f4ddab3e 100644 --- a/crates/swc_css_ast/src/value.rs +++ b/crates/swc_css_ast/src/value.rs @@ -13,6 +13,10 @@ pub struct Ident { pub span: Span, pub value: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -47,6 +51,10 @@ pub struct CustomIdent { pub span: Span, pub value: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -63,6 +71,10 @@ pub struct DashedIdent { pub span: Span, pub value: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -86,6 +98,10 @@ pub struct CustomPropertyName { pub span: Span, pub value: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -102,6 +118,10 @@ pub struct Str { pub span: Span, pub value: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -123,6 +143,10 @@ impl EqIgnoreSpan for Str { )] #[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum DelimiterValue { /// `,` Comma, @@ -172,6 +196,10 @@ pub struct HexColor { /// Does **not** include `#` pub value: Atom, /// Does **not** include `#` + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -333,6 +361,10 @@ pub enum TimePercentage { pub struct Integer { pub span: Span, pub value: i64, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -346,6 +378,10 @@ impl EqIgnoreSpan for Integer { pub struct Number { pub span: Span, pub value: f64, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -385,6 +421,10 @@ impl EqIgnoreSpan for Number { pub struct Ratio { pub span: Span, pub left: Number, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub right: Option, } @@ -400,6 +440,10 @@ pub struct Ratio { )] #[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum BinOp { /// `+` Add, @@ -416,7 +460,15 @@ pub enum BinOp { pub struct Url { pub span: Span, pub name: Ident, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub value: Option>, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub modifiers: Option>, } @@ -435,6 +487,10 @@ pub struct UrlValueRaw { pub span: Span, pub value: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -454,7 +510,15 @@ pub struct UnicodeRange { pub start: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub end: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -507,6 +571,10 @@ pub struct CalcOperator { )] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum CalcOperatorType { /// `+` Add, diff --git a/crates/swc_css_codegen/src/lib.rs b/crates/swc_css_codegen/src/lib.rs index 8857afa2680f..0e1fc1a8c745 100644 --- a/crates/swc_css_codegen/src/lib.rs +++ b/crates/swc_css_codegen/src/lib.rs @@ -1865,7 +1865,7 @@ where write_raw!(self, span, &percentage); } - Token::Dimension(token) => { + Token::Dimension { dimension: token } => { let mut dimension = String::with_capacity(token.raw_value.len() + token.raw_unit.len()); diff --git a/crates/swc_css_lints/src/rules/unit_no_unknown.rs b/crates/swc_css_lints/src/rules/unit_no_unknown.rs index 94248a89438a..8c389903d17d 100644 --- a/crates/swc_css_lints/src/rules/unit_no_unknown.rs +++ b/crates/swc_css_lints/src/rules/unit_no_unknown.rs @@ -52,7 +52,9 @@ impl Visit for UnitNoUnknown { fn visit_component_value(&mut self, component_value: &ComponentValue) { if let Some(token_and_span) = component_value.as_preserved_token() { if let Some(unit) = match &token_and_span.token { - Token::Dimension(dimension_token) => Some(dimension_token.unit.as_ref()), + Token::Dimension { + dimension: dimension_token, + } => Some(dimension_token.unit.as_ref()), _ => None, } { if self.ignored_units.iter().all(|item| !item.is_match(unit)) { diff --git a/crates/swc_css_minifier/src/compressor/mod.rs b/crates/swc_css_minifier/src/compressor/mod.rs index 7d91afab17f4..295f8b4a9132 100644 --- a/crates/swc_css_minifier/src/compressor/mod.rs +++ b/crates/swc_css_minifier/src/compressor/mod.rs @@ -462,9 +462,9 @@ impl VisitMut for Compressor { Token::BadUrl { raw: value, .. } if !contains_only_ascii_characters(value) => { self.need_utf8_at_rule = true; } - Token::Dimension(dimension_token) - if !contains_only_ascii_characters(&dimension_token.unit) => - { + Token::Dimension { + dimension: dimension_token, + } if !contains_only_ascii_characters(&dimension_token.unit) => { self.need_utf8_at_rule = true; } _ => {} diff --git a/crates/swc_css_parser/src/lexer/mod.rs b/crates/swc_css_parser/src/lexer/mod.rs index 3ff7cc011cd6..904633584aab 100644 --- a/crates/swc_css_parser/src/lexer/mod.rs +++ b/crates/swc_css_parser/src/lexer/mod.rs @@ -586,13 +586,14 @@ where let ident_sequence = self.read_ident_sequence()?; // Create a with the same value and type flag as number, and a // unit set initially to the empty string. - let token = Token::Dimension(Box::new(DimensionToken { + let token = Box::new(DimensionToken { value: number.0, raw_value: number.1, unit: ident_sequence.0, raw_unit: ident_sequence.1, type_flag: number.2, - })); + }); + let token = Token::Dimension { dimension: token }; // Return the . return Ok(token); diff --git a/crates/swc_css_parser/src/parser/selectors/mod.rs b/crates/swc_css_parser/src/parser/selectors/mod.rs index 9e50d65cc0ca..230726ea329d 100644 --- a/crates/swc_css_parser/src/parser/selectors/mod.rs +++ b/crates/swc_css_parser/src/parser/selectors/mod.rs @@ -1281,7 +1281,7 @@ where } tok!("dimension") => { let dimension = match bump!(self) { - Token::Dimension(dimension) => { + Token::Dimension { dimension } => { let DimensionToken { value, raw_value, unit, .. } = *dimension; (value, raw_value, unit) diff --git a/crates/swc_css_parser/src/parser/values_and_units/mod.rs b/crates/swc_css_parser/src/parser/values_and_units/mod.rs index 6860389eb95e..affd3e2755e3 100644 --- a/crates/swc_css_parser/src/parser/values_and_units/mod.rs +++ b/crates/swc_css_parser/src/parser/values_and_units/mod.rs @@ -1964,7 +1964,9 @@ where } match cur!(self) { - Token::Dimension(dimension_token) => { + Token::Dimension { + dimension: dimension_token, + } => { match &dimension_token.unit { // unit if is_length_unit(unit) @@ -2004,7 +2006,9 @@ where } match bump!(self) { - Token::Dimension(dimension_token) => { + Token::Dimension { + dimension: dimension_token, + } => { let DimensionToken { value, unit, @@ -2050,7 +2054,9 @@ where } match bump!(self) { - Token::Dimension(dimension_token) => { + Token::Dimension { + dimension: dimension_token, + } => { let DimensionToken { value, unit, @@ -2101,7 +2107,9 @@ where } match bump!(self) { - Token::Dimension(dimension_token) => { + Token::Dimension { + dimension: dimension_token, + } => { let DimensionToken { value, unit, @@ -2149,7 +2157,9 @@ where } match bump!(self) { - Token::Dimension(dimension_token) => { + Token::Dimension { + dimension: dimension_token, + } => { let DimensionToken { value, unit, @@ -2197,7 +2207,9 @@ where } match bump!(self) { - Token::Dimension(dimension_token) => { + Token::Dimension { + dimension: dimension_token, + } => { let DimensionToken { value, unit, @@ -2248,7 +2260,9 @@ where } match bump!(self) { - Token::Dimension(dimension_token) => { + Token::Dimension { + dimension: dimension_token, + } => { let DimensionToken { value, unit, @@ -2296,7 +2310,9 @@ where } match bump!(self) { - Token::Dimension(dimension_token) => { + Token::Dimension { + dimension: dimension_token, + } => { let DimensionToken { value, unit, @@ -2805,9 +2821,9 @@ where } tok!("dimension") => { let raw = match bump!(self) { - Token::Dimension(dimension_token) => { - (dimension_token.raw_value, dimension_token.raw_unit) - } + Token::Dimension { + dimension: dimension_token, + } => (dimension_token.raw_value, dimension_token.raw_unit), _ => { unreachable!(); } @@ -2833,9 +2849,9 @@ where // u '?'* tok!("dimension") => { let raw = match bump!(self) { - Token::Dimension(dimension_token) => { - (dimension_token.raw_value, dimension_token.raw_unit) - } + Token::Dimension { + dimension: dimension_token, + } => (dimension_token.raw_value, dimension_token.raw_unit), _ => { unreachable!(); } diff --git a/crates/swc_css_parser/tests/fixture/at-rule/media/output.json b/crates/swc_css_parser/tests/fixture/at-rule/media/output.json index 96139f503b41..be92500276c7 100644 --- a/crates/swc_css_parser/tests/fixture/at-rule/media/output.json +++ b/crates/swc_css_parser/tests/fixture/at-rule/media/output.json @@ -13630,11 +13630,13 @@ }, "token": { "Dimension": { - "value": 600.0, - "raw_value": "600", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 600.0, + "raw_value": "600", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } @@ -14045,11 +14047,13 @@ }, "token": { "Dimension": { - "value": 600.0, - "raw_value": "600", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 600.0, + "raw_value": "600", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } @@ -14401,11 +14405,13 @@ }, "token": { "Dimension": { - "value": 100.0, - "raw_value": "100", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 100.0, + "raw_value": "100", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } @@ -14511,11 +14517,13 @@ }, "token": { "Dimension": { - "value": 100.0, - "raw_value": "100", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 100.0, + "raw_value": "100", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/fixture/at-rule/media/span.swc-stderr b/crates/swc_css_parser/tests/fixture/at-rule/media/span.swc-stderr index f7259f5017ac..ef1897e45911 100644 --- a/crates/swc_css_parser/tests/fixture/at-rule/media/span.swc-stderr +++ b/crates/swc_css_parser/tests/fixture/at-rule/media/span.swc-stderr @@ -18704,7 +18704,7 @@ : ^^^^^ 143 | @media (height foo bar) {} `---- - x Dimension(DimensionToken { value: 600.0, raw_value: "600", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 600.0, raw_value: "600", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/at-rule/media/input.css:142:1] 141 | 142 | @media (height << 600px) {} @@ -19180,7 +19180,7 @@ : ^^^^^ 146 | @media screen and (min-width: ) {} `---- - x Dimension(DimensionToken { value: 600.0, raw_value: "600", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 600.0, raw_value: "600", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/at-rule/media/input.css:145:1] 144 | @media (height foo("bar")) {} 145 | @media (height + 600px) {} @@ -19600,7 +19600,7 @@ : ^^^^^ 149 | @media screen and func(100px) {} `---- - x Dimension(DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/at-rule/media/input.css:148:1] 147 | @media (aspect-ratio: 12/) {} 148 | @media func(100px) {} @@ -19726,7 +19726,7 @@ : ^^^^^ 150 | @media(min-width:calc(10px + 10px)) {} `---- - x Dimension(DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/at-rule/media/input.css:149:1] 148 | @media func(100px) {} 149 | @media screen and func(100px) {} diff --git a/crates/swc_css_parser/tests/fixture/function/calc/output.json b/crates/swc_css_parser/tests/fixture/function/calc/output.json index fa6506e6ddcf..d40f430bc206 100644 --- a/crates/swc_css_parser/tests/fixture/function/calc/output.json +++ b/crates/swc_css_parser/tests/fixture/function/calc/output.json @@ -333,11 +333,13 @@ }, "token": { "Dimension": { - "value": 30.0, - "raw_value": "30", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 30.0, + "raw_value": "30", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/fixture/function/calc/span.swc-stderr b/crates/swc_css_parser/tests/fixture/function/calc/span.swc-stderr index 3b939c158668..69962be2ad3d 100644 --- a/crates/swc_css_parser/tests/fixture/function/calc/span.swc-stderr +++ b/crates/swc_css_parser/tests/fixture/function/calc/span.swc-stderr @@ -609,7 +609,7 @@ 6 | --width: calc(10% + 30px); : ^^^^ `---- - x Dimension(DimensionToken { value: 30.0, raw_value: "30", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 30.0, raw_value: "30", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/function/calc/input.css:6:1] 5 | div { 6 | --width: calc(10% + 30px); diff --git a/crates/swc_css_parser/tests/fixture/function/unknown/output.json b/crates/swc_css_parser/tests/fixture/function/unknown/output.json index e9bb74918e52..c7377d4f1dac 100644 --- a/crates/swc_css_parser/tests/fixture/function/unknown/output.json +++ b/crates/swc_css_parser/tests/fixture/function/unknown/output.json @@ -179,11 +179,13 @@ }, "token": { "Dimension": { - "value": 100.0, - "raw_value": "100", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 100.0, + "raw_value": "100", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } @@ -232,11 +234,13 @@ }, "token": { "Dimension": { - "value": 100.0, - "raw_value": "100", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 100.0, + "raw_value": "100", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/fixture/function/unknown/span.swc-stderr b/crates/swc_css_parser/tests/fixture/function/unknown/span.swc-stderr index 7405a6260c64..df567bcfbd9d 100644 --- a/crates/swc_css_parser/tests/fixture/function/unknown/span.swc-stderr +++ b/crates/swc_css_parser/tests/fixture/function/unknown/span.swc-stderr @@ -218,7 +218,7 @@ : ^^^^^ 4 | prod: --fn--fn(100px); `---- - x Dimension(DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/function/unknown/input.css:3:1] 2 | prod: fn(100px); 3 | prod: --fn(100px); @@ -281,7 +281,7 @@ : ^^^^^ 5 | } `---- - x Dimension(DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/function/unknown/input.css:4:1] 3 | prod: --fn(100px); 4 | prod: --fn--fn(100px); diff --git a/crates/swc_css_parser/tests/fixture/style-block/output.json b/crates/swc_css_parser/tests/fixture/style-block/output.json index e254185cbfe8..93cee01d624e 100644 --- a/crates/swc_css_parser/tests/fixture/style-block/output.json +++ b/crates/swc_css_parser/tests/fixture/style-block/output.json @@ -3101,11 +3101,13 @@ }, "token": { "Dimension": { - "value": 100.0, - "raw_value": "100", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 100.0, + "raw_value": "100", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -3933,11 +3935,13 @@ }, "token": { "Dimension": { - "value": 16.0, - "raw_value": "16", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 16.0, + "raw_value": "16", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -4002,11 +4006,13 @@ }, "token": { "Dimension": { - "value": 16.0, - "raw_value": "16", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 16.0, + "raw_value": "16", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, diff --git a/crates/swc_css_parser/tests/fixture/style-block/span.swc-stderr b/crates/swc_css_parser/tests/fixture/style-block/span.swc-stderr index 82fd6410652e..d988fcfe6970 100644 --- a/crates/swc_css_parser/tests/fixture/style-block/span.swc-stderr +++ b/crates/swc_css_parser/tests/fixture/style-block/span.swc-stderr @@ -3985,7 +3985,7 @@ : ^^^^^ 93 | } `---- - x Dimension(DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 100.0, raw_value: "100", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/style-block/input.css:92:1] 91 | @mixin mobile { 92 | height: 100px; @@ -4914,7 +4914,7 @@ : ^^^^ 112 | height: 16px; `---- - x Dimension(DimensionToken { value: 16.0, raw_value: "16", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 16.0, raw_value: "16", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/style-block/input.css:111:1] 110 | --small-icon: { 111 | width: 16px; @@ -4998,7 +4998,7 @@ : ^^^^ 113 | } `---- - x Dimension(DimensionToken { value: 16.0, raw_value: "16", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 16.0, raw_value: "16", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/style-block/input.css:112:1] 111 | width: 16px; 112 | height: 16px; diff --git a/crates/swc_css_parser/tests/fixture/value/custom-property/output.json b/crates/swc_css_parser/tests/fixture/value/custom-property/output.json index 61a0c4951388..ede6baa475c5 100644 --- a/crates/swc_css_parser/tests/fixture/value/custom-property/output.json +++ b/crates/swc_css_parser/tests/fixture/value/custom-property/output.json @@ -567,11 +567,13 @@ }, "token": { "Dimension": { - "value": 100.0, - "raw_value": "100", - "unit": "vw", - "type": "integer", - "raw_unit": "vw" + "dimension": { + "value": 100.0, + "raw_value": "100", + "unit": "vw", + "type": "integer", + "raw_unit": "vw" + } } } } @@ -2546,11 +2548,13 @@ }, "token": { "Dimension": { - "value": 16.0, - "raw_value": "16", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 16.0, + "raw_value": "16", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -2615,11 +2619,13 @@ }, "token": { "Dimension": { - "value": 16.0, - "raw_value": "16", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 16.0, + "raw_value": "16", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, diff --git a/crates/swc_css_parser/tests/fixture/value/custom-property/span.swc-stderr b/crates/swc_css_parser/tests/fixture/value/custom-property/span.swc-stderr index 746efe7ab587..ce35d0d82424 100644 --- a/crates/swc_css_parser/tests/fixture/value/custom-property/span.swc-stderr +++ b/crates/swc_css_parser/tests/fixture/value/custom-property/span.swc-stderr @@ -899,7 +899,7 @@ : ^^^^^ 19 | --color: #06c; `---- - x Dimension(DimensionToken { value: 100.0, raw_value: "100", unit: "vw", type_flag: Integer, raw_unit: "vw" }) + x Dimension { dimension: DimensionToken { value: 100.0, raw_value: "100", unit: "vw", type_flag: Integer, raw_unit: "vw" } } ,-[$DIR/tests/fixture/value/custom-property/input.css:18:1] 17 | --number: 1; 18 | --unit: 100vw; @@ -3390,7 +3390,7 @@ : ^^^^ 54 | height: 16px; `---- - x Dimension(DimensionToken { value: 16.0, raw_value: "16", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 16.0, raw_value: "16", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/value/custom-property/input.css:53:1] 52 | --small-icon: { 53 | width: 16px; @@ -3474,7 +3474,7 @@ : ^^^^ 55 | } `---- - x Dimension(DimensionToken { value: 16.0, raw_value: "16", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 16.0, raw_value: "16", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/value/custom-property/input.css:54:1] 53 | width: 16px; 54 | height: 16px; diff --git a/crates/swc_css_parser/tests/fixture/vendor/rome/custom-properties/output.json b/crates/swc_css_parser/tests/fixture/vendor/rome/custom-properties/output.json index 8d8d23323e78..78d9b2b3aa86 100644 --- a/crates/swc_css_parser/tests/fixture/vendor/rome/custom-properties/output.json +++ b/crates/swc_css_parser/tests/fixture/vendor/rome/custom-properties/output.json @@ -289,11 +289,13 @@ }, "token": { "Dimension": { - "value": 12.0, - "raw_value": "12", - "unit": "em", - "type": "integer", - "raw_unit": "em" + "dimension": { + "value": 12.0, + "raw_value": "12", + "unit": "em", + "type": "integer", + "raw_unit": "em" + } } } } @@ -324,11 +326,13 @@ }, "token": { "Dimension": { - "value": 75.0, - "raw_value": "75", - "unit": "ms", - "type": "integer", - "raw_unit": "ms" + "dimension": { + "value": 75.0, + "raw_value": "75", + "unit": "ms", + "type": "integer", + "raw_unit": "ms" + } } } } diff --git a/crates/swc_css_parser/tests/fixture/vendor/rome/custom-properties/span.swc-stderr b/crates/swc_css_parser/tests/fixture/vendor/rome/custom-properties/span.swc-stderr index d75308db7a46..43a27569daae 100644 --- a/crates/swc_css_parser/tests/fixture/vendor/rome/custom-properties/span.swc-stderr +++ b/crates/swc_css_parser/tests/fixture/vendor/rome/custom-properties/span.swc-stderr @@ -431,7 +431,7 @@ : ^^^^ 9 | --time: 75ms; `---- - x Dimension(DimensionToken { value: 12.0, raw_value: "12", unit: "em", type_flag: Integer, raw_unit: "em" }) + x Dimension { dimension: DimensionToken { value: 12.0, raw_value: "12", unit: "em", type_flag: Integer, raw_unit: "em" } } ,-[$DIR/tests/fixture/vendor/rome/custom-properties/input.css:8:1] 7 | --number: 37; 8 | --length: 12em; @@ -473,7 +473,7 @@ : ^^^^ 10 | --function: foo(); `---- - x Dimension(DimensionToken { value: 75.0, raw_value: "75", unit: "ms", type_flag: Integer, raw_unit: "ms" }) + x Dimension { dimension: DimensionToken { value: 75.0, raw_value: "75", unit: "ms", type_flag: Integer, raw_unit: "ms" } } ,-[$DIR/tests/fixture/vendor/rome/custom-properties/input.css:9:1] 8 | --length: 12em; 9 | --time: 75ms; diff --git a/crates/swc_css_parser/tests/fixture/vendor/rome/functions/output.json b/crates/swc_css_parser/tests/fixture/vendor/rome/functions/output.json index 130d52e34322..290d30948b1b 100644 --- a/crates/swc_css_parser/tests/fixture/vendor/rome/functions/output.json +++ b/crates/swc_css_parser/tests/fixture/vendor/rome/functions/output.json @@ -95,11 +95,13 @@ }, "token": { "Dimension": { - "value": 2.0, - "raw_value": "2", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 2.0, + "raw_value": "2", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/fixture/vendor/rome/functions/span.swc-stderr b/crates/swc_css_parser/tests/fixture/vendor/rome/functions/span.swc-stderr index 82bf1a32a98e..8f4ba555a16b 100644 --- a/crates/swc_css_parser/tests/fixture/vendor/rome/functions/span.swc-stderr +++ b/crates/swc_css_parser/tests/fixture/vendor/rome/functions/span.swc-stderr @@ -119,7 +119,7 @@ : ^^^ 3 | border: var(--fancy); `---- - x Dimension(DimensionToken { value: 2.0, raw_value: "2", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 2.0, raw_value: "2", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/fixture/vendor/rome/functions/input.css:2:1] 1 | .style { 2 | --fancy: 2px; diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/condition-and-or/output.json b/crates/swc_css_parser/tests/recovery/at-rule/media/condition-and-or/output.json index 009c0df107ee..c5988b01ad48 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/condition-and-or/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/condition-and-or/output.json @@ -145,11 +145,13 @@ }, "token": { "Dimension": { - "value": 900.0, - "raw_value": "900", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 900.0, + "raw_value": "900", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } @@ -248,11 +250,13 @@ }, "token": { "Dimension": { - "value": 1200.0, - "raw_value": "1200", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 1200.0, + "raw_value": "1200", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/condition-and-or/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/media/condition-and-or/span.swc-stderr index ac01b6190027..b95bc44f045b 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/condition-and-or/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/condition-and-or/span.swc-stderr @@ -123,7 +123,7 @@ 1 | @media screen and (min-width: 900px) or (min-width: 1200px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 900.0, raw_value: "900", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 900.0, raw_value: "900", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/condition-and-or/input.css:1:1] 1 | @media screen and (min-width: 900px) or (min-width: 1200px) {} : ^^^^^ @@ -208,7 +208,7 @@ 1 | @media screen and (min-width: 900px) or (min-width: 1200px) {} : ^^^^^^ `---- - x Dimension(DimensionToken { value: 1200.0, raw_value: "1200", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 1200.0, raw_value: "1200", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/condition-and-or/input.css:1:1] 1 | @media screen and (min-width: 900px) or (min-width: 1200px) {} : ^^^^^^ diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name-1/output.json b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name-1/output.json index 44efcdaf13dd..6aa1409f4696 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name-1/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name-1/output.json @@ -62,11 +62,13 @@ }, "token": { "Dimension": { - "value": 20.0, - "raw_value": "20", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 20.0, + "raw_value": "20", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -98,11 +100,13 @@ }, "token": { "Dimension": { - "value": 20.0, - "raw_value": "20", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 20.0, + "raw_value": "20", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name-1/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name-1/span.swc-stderr index a8e26406f861..7da324bcde96 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name-1/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name-1/span.swc-stderr @@ -53,7 +53,7 @@ 1 | @media (20px: 20px) {} : ^^^^ `---- - x Dimension(DimensionToken { value: 20.0, raw_value: "20", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 20.0, raw_value: "20", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-name-1/input.css:1:1] 1 | @media (20px: 20px) {} : ^^^^ @@ -83,7 +83,7 @@ 1 | @media (20px: 20px) {} : ^^^^ `---- - x Dimension(DimensionToken { value: 20.0, raw_value: "20", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 20.0, raw_value: "20", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-name-1/input.css:1:1] 1 | @media (20px: 20px) {} : ^^^^ diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name/output.json b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name/output.json index caa4e0aa456d..881bceacff88 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name/output.json @@ -95,11 +95,13 @@ }, "token": { "Dimension": { - "value": 20.0, - "raw_value": "20", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 20.0, + "raw_value": "20", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name/span.swc-stderr index de2cedd1780b..a0fd6a97d62a 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-name/span.swc-stderr @@ -83,7 +83,7 @@ 1 | @media ("min-width": 20px) {} : ^^^^ `---- - x Dimension(DimensionToken { value: 20.0, raw_value: "20", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 20.0, raw_value: "20", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-name/input.css:1:1] 1 | @media ("min-width": 20px) {} : ^^^^ diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-2/output.json b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-2/output.json index d480183a2d8a..ff05bd5819b7 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-2/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-2/output.json @@ -62,11 +62,13 @@ }, "token": { "Dimension": { - "value": 400.0, - "raw_value": "400", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 400.0, + "raw_value": "400", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -163,11 +165,13 @@ }, "token": { "Dimension": { - "value": 700.0, - "raw_value": "700", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 700.0, + "raw_value": "700", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-2/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-2/span.swc-stderr index 612ec3114ad1..e7413ddfd0b4 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-2/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-2/span.swc-stderr @@ -53,7 +53,7 @@ 1 | @media (400px > "width" > 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-2/input.css:1:1] 1 | @media (400px > "width" > 700px) {} : ^^^^^ @@ -133,7 +133,7 @@ 1 | @media (400px > "width" > 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-2/input.css:1:1] 1 | @media (400px > "width" > 700px) {} : ^^^^^ diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-3/output.json b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-3/output.json index ca7d303fbaaf..55fdeac26093 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-3/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-3/output.json @@ -62,11 +62,13 @@ }, "token": { "Dimension": { - "value": 400.0, - "raw_value": "400", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 400.0, + "raw_value": "400", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -211,11 +213,13 @@ }, "token": { "Dimension": { - "value": 700.0, - "raw_value": "700", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 700.0, + "raw_value": "700", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-3/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-3/span.swc-stderr index 2bba7c771d03..5ba418961bde 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-3/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-3/span.swc-stderr @@ -53,7 +53,7 @@ 1 | @media (400px > = width > = 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-3/input.css:1:1] 1 | @media (400px > = width > = 700px) {} : ^^^^^ @@ -173,7 +173,7 @@ 1 | @media (400px > = width > = 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-3/input.css:1:1] 1 | @media (400px > = width > = 700px) {} : ^^^^^ diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-4/output.json b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-4/output.json index 1f84b68c99fc..9a52ff05ea8c 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-4/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-4/output.json @@ -62,11 +62,13 @@ }, "token": { "Dimension": { - "value": 400.0, - "raw_value": "400", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 400.0, + "raw_value": "400", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -175,11 +177,13 @@ }, "token": { "Dimension": { - "value": 700.0, - "raw_value": "700", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 700.0, + "raw_value": "700", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-4/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-4/span.swc-stderr index 2e2f4e2d89b2..a4f306d8a959 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-4/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-4/span.swc-stderr @@ -53,7 +53,7 @@ 1 | @media (400px >= width ! 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-4/input.css:1:1] 1 | @media (400px >= width ! 700px) {} : ^^^^^ @@ -143,7 +143,7 @@ 1 | @media (400px >= width ! 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-4/input.css:1:1] 1 | @media (400px >= width ! 700px) {} : ^^^^^ diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-5/output.json b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-5/output.json index cb6a5e67d58f..e218f69af72c 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-5/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-5/output.json @@ -62,11 +62,13 @@ }, "token": { "Dimension": { - "value": 400.0, - "raw_value": "400", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 400.0, + "raw_value": "400", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -187,11 +189,13 @@ }, "token": { "Dimension": { - "value": 700.0, - "raw_value": "700", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 700.0, + "raw_value": "700", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-5/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-5/span.swc-stderr index 39eb7a69f4be..88e15c2d82d1 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-5/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-5/span.swc-stderr @@ -53,7 +53,7 @@ 1 | @media (400px <= width >= 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-5/input.css:1:1] 1 | @media (400px <= width >= 700px) {} : ^^^^^ @@ -153,7 +153,7 @@ 1 | @media (400px <= width >= 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-5/input.css:1:1] 1 | @media (400px <= width >= 700px) {} : ^^^^^ diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-6/output.json b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-6/output.json index 0a920ef27427..050da4f7fb93 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-6/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-6/output.json @@ -62,11 +62,13 @@ }, "token": { "Dimension": { - "value": 400.0, - "raw_value": "400", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 400.0, + "raw_value": "400", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -187,11 +189,13 @@ }, "token": { "Dimension": { - "value": 700.0, - "raw_value": "700", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 700.0, + "raw_value": "700", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-6/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-6/span.swc-stderr index 6340b599f010..4500d296e8df 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-6/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/feature-range-6/span.swc-stderr @@ -53,7 +53,7 @@ 1 | @media (400px >= width <= 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 400.0, raw_value: "400", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-6/input.css:1:1] 1 | @media (400px >= width <= 700px) {} : ^^^^^ @@ -153,7 +153,7 @@ 1 | @media (400px >= width <= 700px) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 700.0, raw_value: "700", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/feature-range-6/input.css:1:1] 1 | @media (400px >= width <= 700px) {} : ^^^^^ diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/invalid-nesting/output.json b/crates/swc_css_parser/tests/recovery/at-rule/media/invalid-nesting/output.json index e94fccfdeda7..99dea231648f 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/invalid-nesting/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/invalid-nesting/output.json @@ -152,11 +152,13 @@ }, "token": { "Dimension": { - "value": 1024.0, - "raw_value": "1024", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 1024.0, + "raw_value": "1024", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, diff --git a/crates/swc_css_parser/tests/recovery/at-rule/media/invalid-nesting/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/media/invalid-nesting/span.swc-stderr index 5f51b3211d25..43f868ee4dfb 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/media/invalid-nesting/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/media/invalid-nesting/span.swc-stderr @@ -180,7 +180,7 @@ : ^^^^^^ 3 | } `---- - x Dimension(DimensionToken { value: 1024.0, raw_value: "1024", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 1024.0, raw_value: "1024", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/at-rule/media/invalid-nesting/input.css:2:1] 1 | @media (min-width: 480px) { 2 | max-inline-size: 1024px; diff --git a/crates/swc_css_parser/tests/recovery/at-rule/supports/wrong-or-and/output.json b/crates/swc_css_parser/tests/recovery/at-rule/supports/wrong-or-and/output.json index 5ee4b3e90f9e..ce92fdee9bfc 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/supports/wrong-or-and/output.json +++ b/crates/swc_css_parser/tests/recovery/at-rule/supports/wrong-or-and/output.json @@ -311,11 +311,13 @@ }, "token": { "Dimension": { - "value": 10.0, - "raw_value": "10", - "unit": "deg", - "type": "integer", - "raw_unit": "deg" + "dimension": { + "value": 10.0, + "raw_value": "10", + "unit": "deg", + "type": "integer", + "raw_unit": "deg" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/at-rule/supports/wrong-or-and/span.swc-stderr b/crates/swc_css_parser/tests/recovery/at-rule/supports/wrong-or-and/span.swc-stderr index 3cfba09e1c1a..fab855e81184 100644 --- a/crates/swc_css_parser/tests/recovery/at-rule/supports/wrong-or-and/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/at-rule/supports/wrong-or-and/span.swc-stderr @@ -268,7 +268,7 @@ 1 | @supports (transition-property: color) or (animation-name: foo) and (transform: rotate(10deg)) {} : ^^^^^ `---- - x Dimension(DimensionToken { value: 10.0, raw_value: "10", unit: "deg", type_flag: Integer, raw_unit: "deg" }) + x Dimension { dimension: DimensionToken { value: 10.0, raw_value: "10", unit: "deg", type_flag: Integer, raw_unit: "deg" } } ,-[$DIR/tests/recovery/at-rule/supports/wrong-or-and/input.css:1:1] 1 | @supports (transition-property: color) or (animation-name: foo) and (transform: rotate(10deg)) {} : ^^^^^ diff --git a/crates/swc_css_parser/tests/recovery/declaration/wrong-name-3/output.json b/crates/swc_css_parser/tests/recovery/declaration/wrong-name-3/output.json index 6f2e165294db..660b928eda4d 100644 --- a/crates/swc_css_parser/tests/recovery/declaration/wrong-name-3/output.json +++ b/crates/swc_css_parser/tests/recovery/declaration/wrong-name-3/output.json @@ -108,11 +108,13 @@ }, "token": { "Dimension": { - "value": 20.0, - "raw_value": "20", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 20.0, + "raw_value": "20", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/declaration/wrong-name-3/span.swc-stderr b/crates/swc_css_parser/tests/recovery/declaration/wrong-name-3/span.swc-stderr index 13e42667c863..0196892e1740 100644 --- a/crates/swc_css_parser/tests/recovery/declaration/wrong-name-3/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/declaration/wrong-name-3/span.swc-stderr @@ -105,7 +105,7 @@ : ^^^^ 3 | } `---- - x Dimension(DimensionToken { value: 20.0, raw_value: "20", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 20.0, raw_value: "20", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/declaration/wrong-name-3/input.css:2:1] 1 | a { 2 | func(20px); diff --git a/crates/swc_css_parser/tests/recovery/function/calc/space/output.json b/crates/swc_css_parser/tests/recovery/function/calc/space/output.json index 7f9de925724a..804943b16006 100644 --- a/crates/swc_css_parser/tests/recovery/function/calc/space/output.json +++ b/crates/swc_css_parser/tests/recovery/function/calc/space/output.json @@ -111,11 +111,13 @@ }, "token": { "Dimension": { - "value": 2.0, - "raw_value": "2", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 2.0, + "raw_value": "2", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, @@ -127,11 +129,13 @@ }, "token": { "Dimension": { - "value": 1.0, - "raw_value": "+1", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 1.0, + "raw_value": "+1", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/function/calc/space/span.swc-stderr b/crates/swc_css_parser/tests/recovery/function/calc/space/span.swc-stderr index 6a3de1b5d526..086c39a0caff 100644 --- a/crates/swc_css_parser/tests/recovery/function/calc/space/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/function/calc/space/span.swc-stderr @@ -120,7 +120,7 @@ : ^^^ 3 | } `---- - x Dimension(DimensionToken { value: 2.0, raw_value: "2", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 2.0, raw_value: "2", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/function/calc/space/input.css:2:1] 1 | .style { 2 | width: calc(2px+1px); @@ -134,7 +134,7 @@ : ^^^^ 3 | } `---- - x Dimension(DimensionToken { value: 1.0, raw_value: "+1", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 1.0, raw_value: "+1", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/function/calc/space/input.css:2:1] 1 | .style { 2 | width: calc(2px+1px); diff --git a/crates/swc_css_parser/tests/recovery/function/unclosed-2/output.json b/crates/swc_css_parser/tests/recovery/function/unclosed-2/output.json index a202d133a98f..1bb497209493 100644 --- a/crates/swc_css_parser/tests/recovery/function/unclosed-2/output.json +++ b/crates/swc_css_parser/tests/recovery/function/unclosed-2/output.json @@ -187,11 +187,13 @@ }, "token": { "Dimension": { - "value": 12.0, - "raw_value": "12", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 12.0, + "raw_value": "12", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, diff --git a/crates/swc_css_parser/tests/recovery/function/unclosed-2/span.swc-stderr b/crates/swc_css_parser/tests/recovery/function/unclosed-2/span.swc-stderr index d7e7b2b268a1..4579ef34f05e 100644 --- a/crates/swc_css_parser/tests/recovery/function/unclosed-2/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/function/unclosed-2/span.swc-stderr @@ -207,7 +207,7 @@ : ^^^^ 3 | } `---- - x Dimension(DimensionToken { value: 12.0, raw_value: "12", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 12.0, raw_value: "12", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/function/unclosed-2/input.css:2:1] 1 | .style { 2 | grid-template-columns: repeat(4, [col-start] 12px diff --git a/crates/swc_css_parser/tests/recovery/function/unclosed/output.json b/crates/swc_css_parser/tests/recovery/function/unclosed/output.json index f62ed3696d34..c6f2fb4c7715 100644 --- a/crates/swc_css_parser/tests/recovery/function/unclosed/output.json +++ b/crates/swc_css_parser/tests/recovery/function/unclosed/output.json @@ -111,11 +111,13 @@ }, "token": { "Dimension": { - "value": 500.0, - "raw_value": "500", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 500.0, + "raw_value": "500", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, diff --git a/crates/swc_css_parser/tests/recovery/function/unclosed/span.swc-stderr b/crates/swc_css_parser/tests/recovery/function/unclosed/span.swc-stderr index 99a29c17a41b..47f411671295 100644 --- a/crates/swc_css_parser/tests/recovery/function/unclosed/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/function/unclosed/span.swc-stderr @@ -116,7 +116,7 @@ : ^^^^^ 3 | } `---- - x Dimension(DimensionToken { value: 500.0, raw_value: "500", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 500.0, raw_value: "500", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/function/unclosed/input.css:2:1] 1 | .style { 2 | width: min(500px; diff --git a/crates/swc_css_parser/tests/recovery/number/output.json b/crates/swc_css_parser/tests/recovery/number/output.json index f60b4c7e419a..028c708a4258 100644 --- a/crates/swc_css_parser/tests/recovery/number/output.json +++ b/crates/swc_css_parser/tests/recovery/number/output.json @@ -101,11 +101,13 @@ }, "token": { "Dimension": { - "value": 10.1, - "raw_value": "10.10", - "unit": "px", - "type": "number", - "raw_unit": "px" + "dimension": { + "value": 10.1, + "raw_value": "10.10", + "unit": "px", + "type": "number", + "raw_unit": "px" + } } } }, @@ -162,11 +164,13 @@ }, "token": { "Dimension": { - "value": 10.0, - "raw_value": "10", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 10.0, + "raw_value": "10", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, diff --git a/crates/swc_css_parser/tests/recovery/number/span.swc-stderr b/crates/swc_css_parser/tests/recovery/number/span.swc-stderr index f82038163bc5..6b1ee6f0bd64 100644 --- a/crates/swc_css_parser/tests/recovery/number/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/number/span.swc-stderr @@ -121,7 +121,7 @@ : ^^^^^^^ 3 | prop1: 10px `---- - x Dimension(DimensionToken { value: 10.1, raw_value: "10.10", unit: "px", type_flag: Number, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 10.1, raw_value: "10.10", unit: "px", type_flag: Number, raw_unit: "px" } } ,-[$DIR/tests/recovery/number/input.css:2:1] 1 | a { 2 | prop: 10.10px @@ -191,7 +191,7 @@ : ^^^^ 4 | prop2: 10 `---- - x Dimension(DimensionToken { value: 10.0, raw_value: "10", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 10.0, raw_value: "10", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/number/input.css:3:1] 2 | prop: 10.10px 3 | prop1: 10px diff --git a/crates/swc_css_parser/tests/recovery/style-blocks-contents/invalid-nested-1/output.json b/crates/swc_css_parser/tests/recovery/style-blocks-contents/invalid-nested-1/output.json index 73b7be28adfd..d67a9a0f544f 100644 --- a/crates/swc_css_parser/tests/recovery/style-blocks-contents/invalid-nested-1/output.json +++ b/crates/swc_css_parser/tests/recovery/style-blocks-contents/invalid-nested-1/output.json @@ -205,11 +205,13 @@ }, "token": { "Dimension": { - "value": 1.0, - "raw_value": "1", - "unit": "em", - "type": "integer", - "raw_unit": "em" + "dimension": { + "value": 1.0, + "raw_value": "1", + "unit": "em", + "type": "integer", + "raw_unit": "em" + } } } }, diff --git a/crates/swc_css_parser/tests/recovery/style-blocks-contents/invalid-nested-1/span.swc-stderr b/crates/swc_css_parser/tests/recovery/style-blocks-contents/invalid-nested-1/span.swc-stderr index 90ce3276869b..42c3e32d324b 100644 --- a/crates/swc_css_parser/tests/recovery/style-blocks-contents/invalid-nested-1/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/style-blocks-contents/invalid-nested-1/span.swc-stderr @@ -244,7 +244,7 @@ : ^^^ 6 | } `---- - x Dimension(DimensionToken { value: 1.0, raw_value: "1", unit: "em", type_flag: Integer, raw_unit: "em" }) + x Dimension { dimension: DimensionToken { value: 1.0, raw_value: "1", unit: "em", type_flag: Integer, raw_unit: "em" } } ,-[$DIR/tests/recovery/style-blocks-contents/invalid-nested-1/input.css:5:1] 4 | input { 5 | margin: 1em; diff --git a/crates/swc_css_parser/tests/recovery/unicode-range/output.json b/crates/swc_css_parser/tests/recovery/unicode-range/output.json index a323d033cc8f..2e120b393205 100644 --- a/crates/swc_css_parser/tests/recovery/unicode-range/output.json +++ b/crates/swc_css_parser/tests/recovery/unicode-range/output.json @@ -108,11 +108,13 @@ }, "token": { "Dimension": { - "value": 1.0, - "raw_value": "+1", - "unit": "Z1ee1-FFFFFF", - "type": "integer", - "raw_unit": "Z1ee1-FFFFFF" + "dimension": { + "value": 1.0, + "raw_value": "+1", + "unit": "Z1ee1-FFFFFF", + "type": "integer", + "raw_unit": "Z1ee1-FFFFFF" + } } } } @@ -156,11 +158,13 @@ }, "token": { "Dimension": { - "value": 10.0, - "raw_value": "+1e1", - "unit": "ee1-FFZFFF", - "type": "number", - "raw_unit": "ee1-FFZFFF" + "dimension": { + "value": 10.0, + "raw_value": "+1e1", + "unit": "ee1-FFZFFF", + "type": "number", + "raw_unit": "ee1-FFZFFF" + } } } } diff --git a/crates/swc_css_parser/tests/recovery/unicode-range/span.swc-stderr b/crates/swc_css_parser/tests/recovery/unicode-range/span.swc-stderr index aaa38fb53b0e..126632701fc7 100644 --- a/crates/swc_css_parser/tests/recovery/unicode-range/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/unicode-range/span.swc-stderr @@ -117,7 +117,7 @@ : ^^^^^^^^^^^^^^ 3 | unicode-range: U+1e1ee1-FFZFFF; `---- - x Dimension(DimensionToken { value: 1.0, raw_value: "+1", unit: "Z1ee1-FFFFFF", type_flag: Integer, raw_unit: "Z1ee1-FFFFFF" }) + x Dimension { dimension: DimensionToken { value: 1.0, raw_value: "+1", unit: "Z1ee1-FFFFFF", type_flag: Integer, raw_unit: "Z1ee1-FFFFFF" } } ,-[$DIR/tests/recovery/unicode-range/input.css:2:1] 1 | .class { 2 | unicode-range: U+1Z1ee1-FFFFFF; @@ -173,7 +173,7 @@ : ^^^^^^^^^^^^^^ 4 | } `---- - x Dimension(DimensionToken { value: 10.0, raw_value: "+1e1", unit: "ee1-FFZFFF", type_flag: Number, raw_unit: "ee1-FFZFFF" }) + x Dimension { dimension: DimensionToken { value: 10.0, raw_value: "+1e1", unit: "ee1-FFZFFF", type_flag: Number, raw_unit: "ee1-FFZFFF" } } ,-[$DIR/tests/recovery/unicode-range/input.css:3:1] 2 | unicode-range: U+1Z1ee1-FFFFFF; 3 | unicode-range: U+1e1ee1-FFZFFF; diff --git a/crates/swc_css_parser/tests/recovery/vercel/004/output.json b/crates/swc_css_parser/tests/recovery/vercel/004/output.json index 774f4330bc19..46149574d88b 100644 --- a/crates/swc_css_parser/tests/recovery/vercel/004/output.json +++ b/crates/swc_css_parser/tests/recovery/vercel/004/output.json @@ -195,11 +195,13 @@ }, "token": { "Dimension": { - "value": 7.0, - "raw_value": "7", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 7.0, + "raw_value": "7", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, diff --git a/crates/swc_css_parser/tests/recovery/vercel/004/span.swc-stderr b/crates/swc_css_parser/tests/recovery/vercel/004/span.swc-stderr index 791a92d37c15..af906bd06092 100644 --- a/crates/swc_css_parser/tests/recovery/vercel/004/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/vercel/004/span.swc-stderr @@ -238,7 +238,7 @@ : ^^^ 5 | color: white; `---- - x Dimension(DimensionToken { value: 7.0, raw_value: "7", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 7.0, raw_value: "7", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/vercel/004/input.css:4:1] 3 | __ident__ 4 | border-radius: 7px; diff --git a/crates/swc_css_parser/tests/recovery/vercel/005/output.json b/crates/swc_css_parser/tests/recovery/vercel/005/output.json index 679288e13968..57202733533f 100644 --- a/crates/swc_css_parser/tests/recovery/vercel/005/output.json +++ b/crates/swc_css_parser/tests/recovery/vercel/005/output.json @@ -137,11 +137,13 @@ }, "token": { "Dimension": { - "value": 7.0, - "raw_value": "7", - "unit": "px", - "type": "integer", - "raw_unit": "px" + "dimension": { + "value": 7.0, + "raw_value": "7", + "unit": "px", + "type": "integer", + "raw_unit": "px" + } } } }, diff --git a/crates/swc_css_parser/tests/recovery/vercel/005/span.swc-stderr b/crates/swc_css_parser/tests/recovery/vercel/005/span.swc-stderr index 1225e3a56dbe..40bbb9b0424a 100644 --- a/crates/swc_css_parser/tests/recovery/vercel/005/span.swc-stderr +++ b/crates/swc_css_parser/tests/recovery/vercel/005/span.swc-stderr @@ -190,7 +190,7 @@ : ^^^ 5 | color: white; `---- - x Dimension(DimensionToken { value: 7.0, raw_value: "7", unit: "px", type_flag: Integer, raw_unit: "px" }) + x Dimension { dimension: DimensionToken { value: 7.0, raw_value: "7", unit: "px", type_flag: Integer, raw_unit: "px" } } ,-[$DIR/tests/recovery/vercel/005/input.css:4:1] 3 | __ident__ 4 | border-radius: 7px; diff --git a/crates/swc_css_visit/src/generated.rs b/crates/swc_css_visit/src/generated.rs index a7973ca8edba..82aa18b2016b 100644 --- a/crates/swc_css_visit/src/generated.rs +++ b/crates/swc_css_visit/src/generated.rs @@ -11490,8 +11490,10 @@ impl VisitWith for Token { >::visit_with(raw, visitor) }; } - Token::Dimension { 0: _field_0 } => { - as VisitWith>::visit_with(_field_0, visitor); + Token::Dimension { dimension } => { + { + as VisitWith>::visit_with(dimension, visitor) + }; } Token::WhiteSpace { value } => { { @@ -35223,16 +35225,22 @@ impl VisitWithAstPath for Token { ) }; } - Token::Dimension { 0: _field_0 } => { + Token::Dimension { dimension } => { let mut __ast_path = __ast_path.with_guard(AstParentNodeRef::Token( self, self::fields::TokenField::Dimension, )); - as VisitWithAstPath>::visit_with_ast_path( - _field_0, - visitor, - &mut *__ast_path, - ); + { + let mut __ast_path = __ast_path.with_guard(AstParentNodeRef::Token( + self, + self::fields::TokenField::Dimension, + )); + as VisitWithAstPath>::visit_with_ast_path( + dimension, + visitor, + &mut *__ast_path, + ) + }; } Token::WhiteSpace { value } => { let mut __ast_path = __ast_path.with_guard(AstParentNodeRef::Token( @@ -49517,8 +49525,10 @@ impl VisitMutWith for Token { >::visit_mut_with(raw, visitor) }; } - Token::Dimension { 0: _field_0 } => { - as VisitMutWith>::visit_mut_with(_field_0, visitor); + Token::Dimension { dimension } => { + { + as VisitMutWith>::visit_mut_with(dimension, visitor) + }; } Token::WhiteSpace { value } => { { @@ -70444,14 +70454,18 @@ impl VisitMutWithAstPath for Token { ) }; } - Token::Dimension { 0: _field_0 } => { + Token::Dimension { dimension } => { let mut __ast_path = __ast_path .with_guard(AstParentKind::Token(self::fields::TokenField::Dimension)); - as VisitMutWithAstPath>::visit_mut_with_ast_path( - _field_0, - visitor, - &mut *__ast_path, - ); + { + let mut __ast_path = __ast_path + .with_guard(AstParentKind::Token(self::fields::TokenField::Dimension)); + as VisitMutWithAstPath>::visit_mut_with_ast_path( + dimension, + visitor, + &mut *__ast_path, + ) + }; } Token::WhiteSpace { value } => { let mut __ast_path = __ast_path @@ -84608,9 +84622,10 @@ impl FoldWith for Token { let raw = { >::fold_with(raw, visitor) }; Token::Percentage { value, raw } } - Token::Dimension { 0: _field_0 } => { - let _field_0 = as FoldWith>::fold_with(_field_0, visitor); - Token::Dimension { 0: _field_0 } + Token::Dimension { dimension } => { + let dimension = + { as FoldWith>::fold_with(dimension, visitor) }; + Token::Dimension { dimension } } Token::WhiteSpace { value } => { let value = { >::fold_with(value, visitor) }; @@ -106250,15 +106265,19 @@ impl FoldWithAstPath for Token { }; Token::Percentage { value, raw } } - Token::Dimension { 0: _field_0 } => { + Token::Dimension { dimension } => { let mut __ast_path = __ast_path .with_guard(AstParentKind::Token(self::fields::TokenField::Dimension)); - let _field_0 = as FoldWithAstPath>::fold_with_ast_path( - _field_0, - visitor, - &mut *__ast_path, - ); - Token::Dimension { 0: _field_0 } + let dimension = { + let mut __ast_path = __ast_path + .with_guard(AstParentKind::Token(self::fields::TokenField::Dimension)); + as FoldWithAstPath>::fold_with_ast_path( + dimension, + visitor, + &mut *__ast_path, + ) + }; + Token::Dimension { dimension } } Token::WhiteSpace { value } => { let mut __ast_path = __ast_path @@ -114710,7 +114729,9 @@ impl<'ast> NodeRef<'ast> { _ => Box::new(::std::iter::empty::>()), }, NodeRef::Token(node) => match node { - Token::Dimension(v0) => Box::new(::std::iter::once(NodeRef::DimensionToken(v0))), + Token::Dimension { dimension: v0 } => { + Box::new(::std::iter::once(NodeRef::DimensionToken(v0))) + } _ => Box::new(::std::iter::empty::>()), }, NodeRef::TokenAndSpan(node) => { diff --git a/crates/swc_ecma_ast/Cargo.toml b/crates/swc_ecma_ast/Cargo.toml index 31201c932603..5b0f2c75c821 100644 --- a/crates/swc_ecma_ast/Cargo.toml +++ b/crates/swc_ecma_ast/Cargo.toml @@ -31,6 +31,7 @@ rkyv-impl = [ "swc_common/rkyv-impl", ] serde-impl = ["serde"] +encoding-impl = ["cbor4ii", "swc_atoms/encoding-impl", "swc_common/encoding-impl"] shrink-to-fit = [ "dep:shrink-to-fit", "swc_atoms/shrink-to-fit", @@ -57,5 +58,7 @@ swc_atoms = { version = "7.0.0", path = "../swc_atoms" } swc_common = { version = "15.0.0", path = "../swc_common" } swc_visit = { version = "2.0.1", path = "../swc_visit" } +cbor4ii = { workspace = true, features = [ "use_std" ], optional = true } + [dev-dependencies] serde_json = { workspace = true } diff --git a/crates/swc_ecma_ast/src/class.rs b/crates/swc_ecma_ast/src/class.rs index 86daf8b68230..b3591a11ed43 100644 --- a/crates/swc_ecma_ast/src/class.rs +++ b/crates/swc_ecma_ast/src/class.rs @@ -30,15 +30,27 @@ pub struct Class { pub body: Vec, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub super_class: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] pub is_abstract: bool, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub super_type_params: Option>, /// Typescript extension. @@ -102,9 +114,17 @@ pub struct ClassProp { pub key: PropName, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub value: Option>, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] @@ -115,6 +135,10 @@ pub struct ClassProp { /// Typescript extension. #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub accessibility: Option, /// Typescript extension. @@ -151,9 +175,17 @@ pub struct PrivateProp { pub key: PrivateName, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub value: Option>, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] @@ -164,6 +196,10 @@ pub struct PrivateProp { /// Typescript extension. #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub accessibility: Option, #[cfg_attr(feature = "serde-impl", serde(default))] @@ -193,6 +229,10 @@ pub struct ClassMethod { pub is_static: bool, #[doc = r" Typescript extension."] #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub accessibility: Option, #[doc = r" Typescript extension."] #[cfg_attr(feature = "serde-impl", serde(default))] @@ -217,6 +257,10 @@ pub struct PrivateMethod { pub is_static: bool, #[doc = r" Typescript extension."] #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub accessibility: Option, #[doc = r" Typescript extension."] #[cfg_attr(feature = "serde-impl", serde(default))] @@ -241,9 +285,17 @@ pub struct Constructor { pub params: Vec, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub body: Option, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub accessibility: Option, #[cfg_attr(feature = "serde-impl", serde(default))] @@ -271,6 +323,10 @@ pub struct Decorator { #[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum MethodKind { #[default] @@ -346,9 +402,17 @@ pub struct AutoAccessor { pub key: Key, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub value: Option>, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] @@ -359,6 +423,10 @@ pub struct AutoAccessor { /// Typescript extension. #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub accessibility: Option, #[cfg_attr(feature = "serde-impl", serde(default))] diff --git a/crates/swc_ecma_ast/src/decl.rs b/crates/swc_ecma_ast/src/decl.rs index 40282c8cec7d..8e8ecfd40232 100644 --- a/crates/swc_ecma_ast/src/decl.rs +++ b/crates/swc_ecma_ast/src/decl.rs @@ -179,6 +179,10 @@ impl Take for VarDecl { #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum VarDeclKind { /// `var` @@ -201,6 +205,10 @@ pub struct VarDeclarator { /// Initialization expression. #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub init: Option>, /// Typescript only diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index e87bcdc188a4..3e63326475b6 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -376,6 +376,8 @@ impl Expr { Expr::PrivateName(e) => e.span = span, Expr::OptChain(e) => e.span = span, Expr::Lit(e) => e.set_span(span), + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } } @@ -386,6 +388,8 @@ impl Clone for Expr { fn clone(&self) -> Self { use Expr::*; match self { + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + Unknown(tag, v) => Unknown(*tag, v.clone()), This(e) => This(e.clone()), Array(e) => Array(e.clone()), Object(e) => Object(e.clone()), @@ -513,6 +517,10 @@ pub struct ArrayLit { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(default, rename = "elements"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "swc_common::serializer::ArrayOption") + )] pub elems: Vec>, } @@ -564,6 +572,8 @@ impl ObjectLit { } _ => return None, }, + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } @@ -760,6 +770,10 @@ impl Take for BinExpr { #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] pub struct FnExpr { #[cfg_attr(feature = "serde-impl", serde(default, rename = "identifier"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ident: Option, #[cfg_attr(feature = "serde-impl", serde(flatten))] @@ -795,6 +809,10 @@ bridge_expr_from!(FnExpr, Box); #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] pub struct ClassExpr { #[cfg_attr(feature = "serde-impl", serde(default, rename = "identifier"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub ident: Option, #[cfg_attr(feature = "serde-impl", serde(flatten))] @@ -984,6 +1002,10 @@ pub struct CallExpr { pub args: Vec, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeArguments"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_args: Option>, // pub type_params: Option, } @@ -1006,9 +1028,17 @@ pub struct NewExpr { pub callee: Box, #[cfg_attr(feature = "serde-impl", serde(default, rename = "arguments"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "::cbor4ii::core::types::Maybe") + )] pub args: Option>, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeArguments"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "::cbor4ii::core::types::Maybe") + )] pub type_args: Option>, // pub type_params: Option, } @@ -1060,9 +1090,17 @@ pub struct ArrowExpr { pub is_generator: bool, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeParameters"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub return_type: Option>, } @@ -1082,6 +1120,10 @@ pub struct YieldExpr { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(default, rename = "argument"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub arg: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] @@ -1116,6 +1158,10 @@ pub struct MetaPropExpr { #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum MetaPropKind { /// `new.target` @@ -1170,6 +1216,10 @@ pub struct TaggedTpl { pub tag: Box, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeParameters"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, /// This is boxed to reduce the type size of [Expr]. @@ -1195,6 +1245,10 @@ pub struct TplElement { /// /// If you are going to use codegen right after creating a [TplElement], you /// don't have to worry about this value. + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub cooked: Option, /// You may need to perform. `.replace("\r\n", "\n").replace('\r', "\n")` on @@ -1334,9 +1388,17 @@ impl Take for Import { )] #[cfg_attr(feature = "rkyv-impl", repr(C))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub struct ExprOrSpread { #[cfg_attr(feature = "serde-impl", serde(default))] #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub spread: Option, #[cfg_attr(feature = "serde-impl", serde(rename = "expression"))] @@ -1497,6 +1559,8 @@ impl From for Pat { AssignTargetPat::Array(a) => a.into(), AssignTargetPat::Object(o) => o.into(), AssignTargetPat::Invalid(i) => i.into(), + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } } @@ -1618,6 +1682,8 @@ impl From for Box { SimpleAssignTarget::TsTypeAssertion(a) => a.into(), SimpleAssignTarget::TsInstantiation(a) => a.into(), SimpleAssignTarget::Invalid(i) => i.into(), + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } } @@ -1687,6 +1753,10 @@ pub struct OptCall { pub args: Vec, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeArguments"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_args: Option>, // pub type_params: Option, } @@ -1718,6 +1788,8 @@ impl From for Expr { ctxt, }), OptChainBase::Member(member) => Self::Member(member), + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } } diff --git a/crates/swc_ecma_ast/src/function.rs b/crates/swc_ecma_ast/src/function.rs index 7236bd89b49a..3d0747b3a00b 100644 --- a/crates/swc_ecma_ast/src/function.rs +++ b/crates/swc_ecma_ast/src/function.rs @@ -24,6 +24,10 @@ pub struct Function { pub ctxt: SyntaxContext, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub body: Option, /// if it's a generator. @@ -35,9 +39,17 @@ pub struct Function { pub is_async: bool, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeParameters"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub return_type: Option>, } diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index bff302b894c7..38a68175f0a4 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -40,6 +40,10 @@ use crate::{typescript::TsTypeAnn, Expr}; #[cfg_attr(feature = "rkyv-impl", repr(C))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub struct BindingIdent { #[cfg_attr(feature = "serde-impl", serde(flatten))] #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] @@ -47,6 +51,10 @@ pub struct BindingIdent { #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, } diff --git a/crates/swc_ecma_ast/src/jsx.rs b/crates/swc_ecma_ast/src/jsx.rs index 455293cd2668..cc8a091e2a3c 100644 --- a/crates/swc_ecma_ast/src/jsx.rs +++ b/crates/swc_ecma_ast/src/jsx.rs @@ -126,6 +126,10 @@ pub struct JSXOpeningElement { /// Note: This field's name is different from one from babel because it is /// misleading #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeArguments"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_args: Option>, } @@ -170,6 +174,10 @@ pub struct JSXAttr { pub span: Span, pub name: JSXAttrName, /// Babel uses Expr instead of JSXAttrValue + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub value: Option, } @@ -236,6 +244,10 @@ pub struct JSXElement { pub span: Span, pub opening: JSXOpeningElement, pub children: Vec, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub closing: Option, } diff --git a/crates/swc_ecma_ast/src/lit.rs b/crates/swc_ecma_ast/src/lit.rs index b2eb23046fbf..fa498ef5d6a7 100644 --- a/crates/swc_ecma_ast/src/lit.rs +++ b/crates/swc_ecma_ast/src/lit.rs @@ -72,6 +72,8 @@ impl Lit { Lit::BigInt(n) => n.span = span, Lit::Regex(n) => n.span = span, Lit::JSXText(n) => n.span = span, + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } } @@ -81,10 +83,15 @@ impl Lit { pub struct BigInt { pub span: Span, #[cfg_attr(any(feature = "rkyv-impl"), rkyv(with = EncodeBigInt))] + #[cfg_attr(feature = "encoding-impl", encoding(with = "EncodeBigInt2"))] pub value: Box, /// Use `None` value only for transformations to avoid recalculate /// characters in big integer + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -100,6 +107,33 @@ impl EqIgnoreSpan for BigInt { } } +#[cfg(feature = "encoding-impl")] +struct EncodeBigInt2(T); + +#[cfg(feature = "encoding-impl")] +impl cbor4ii::core::enc::Encode for EncodeBigInt2<&'_ Box> { + #[inline] + fn encode( + &self, + writer: &mut W, + ) -> Result<(), cbor4ii::core::enc::Error> { + cbor4ii::core::types::Bytes(self.0.to_signed_bytes_le().as_slice()).encode(writer) + } +} + +#[cfg(feature = "encoding-impl")] +impl<'de> cbor4ii::core::dec::Decode<'de> for EncodeBigInt2> { + #[inline] + fn decode>( + reader: &mut R, + ) -> Result> { + let buf = >::decode(reader)?; + Ok(EncodeBigInt2(Box::new(BigIntValue::from_signed_bytes_le( + buf.0, + )))) + } +} + #[cfg(feature = "rkyv-impl")] #[derive(Debug, Clone, Copy)] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] @@ -189,6 +223,10 @@ pub struct Str { /// Use `None` value only for transformations to avoid recalculate escaped /// characters in strings + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -398,6 +436,10 @@ pub struct Number { /// Use `None` value only for transformations to avoid recalculate /// characters in number literal + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } diff --git a/crates/swc_ecma_ast/src/module.rs b/crates/swc_ecma_ast/src/module.rs index 0f60dac3add4..339f4c45f72f 100644 --- a/crates/swc_ecma_ast/src/module.rs +++ b/crates/swc_ecma_ast/src/module.rs @@ -30,6 +30,10 @@ pub struct Module { pub body: Vec, #[cfg_attr(feature = "serde-impl", serde(default, rename = "interpreter"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub shebang: Option, } @@ -66,6 +70,10 @@ pub struct Script { pub body: Vec, #[cfg_attr(feature = "serde-impl", serde(default, rename = "interpreter"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub shebang: Option, } diff --git a/crates/swc_ecma_ast/src/module_decl.rs b/crates/swc_ecma_ast/src/module_decl.rs index a8a03e4499a4..5de6c1d6005e 100644 --- a/crates/swc_ecma_ast/src/module_decl.rs +++ b/crates/swc_ecma_ast/src/module_decl.rs @@ -130,6 +130,10 @@ pub struct ImportDecl { pub type_only: bool, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub with: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] @@ -146,6 +150,10 @@ pub struct ImportDecl { #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum ImportPhase { #[default] @@ -185,6 +193,10 @@ pub struct ExportAll { pub type_only: bool, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub with: Option>, } @@ -211,12 +223,20 @@ pub struct NamedExport { pub specifiers: Vec, #[cfg_attr(feature = "serde-impl", serde(rename = "source"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub src: Option>, #[cfg_attr(feature = "serde-impl", serde(rename = "typeOnly"))] pub type_only: bool, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub with: Option>, } @@ -276,6 +296,8 @@ impl ImportSpecifier { match self { ImportSpecifier::Named(named) => named.is_type_only, ImportSpecifier::Default(..) | ImportSpecifier::Namespace(..) => false, + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } @@ -284,6 +306,8 @@ impl ImportSpecifier { ImportSpecifier::Named(named) => &named.local, ImportSpecifier::Default(default) => &default.local, ImportSpecifier::Namespace(ns) => &ns.local, + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } @@ -292,6 +316,8 @@ impl ImportSpecifier { ImportSpecifier::Named(named) => &mut named.local, ImportSpecifier::Default(default) => &mut default.local, ImportSpecifier::Namespace(ns) => &mut ns.local, + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } } @@ -329,6 +355,10 @@ pub struct ImportNamedSpecifier { pub local: Ident, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub imported: Option, #[cfg_attr(feature = "serde-impl", serde(default))] @@ -381,6 +411,10 @@ pub struct ExportNamedSpecifier { pub orig: ModuleExportName, /// `Some(bar)` in `export { foo as bar }` #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub exported: Option, /// `type` in `export { type foo as bar }` #[cfg_attr(feature = "serde-impl", serde(default))] @@ -409,6 +443,8 @@ impl ModuleExportName { match self { ModuleExportName::Ident(i) => &i.sym, ModuleExportName::Str(s) => &s.value, + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } } diff --git a/crates/swc_ecma_ast/src/operators.rs b/crates/swc_ecma_ast/src/operators.rs index eb1c7bad272d..27c4b0b435b1 100644 --- a/crates/swc_ecma_ast/src/operators.rs +++ b/crates/swc_ecma_ast/src/operators.rs @@ -10,6 +10,10 @@ use swc_common::EqIgnoreSpan; )] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum BinaryOp { /// `==` @@ -124,6 +128,10 @@ impl BinaryOp { )] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum AssignOp { /// `=` @@ -202,6 +210,10 @@ impl AssignOp { )] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum UpdateOp { /// `++` @@ -220,6 +232,10 @@ pub enum UpdateOp { )] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum UnaryOp { /// `-` diff --git a/crates/swc_ecma_ast/src/pat.rs b/crates/swc_ecma_ast/src/pat.rs index d33339e8f68c..32a2e5bd96e5 100644 --- a/crates/swc_ecma_ast/src/pat.rs +++ b/crates/swc_ecma_ast/src/pat.rs @@ -43,6 +43,8 @@ impl Clone for Pat { fn clone(&self) -> Self { use Pat::*; match self { + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + Unknown(tag, v) => Unknown(*tag, v.clone()), Ident(p) => Ident(p.clone()), Array(p) => Array(p.clone()), Rest(p) => Rest(p.clone()), @@ -91,6 +93,10 @@ pub struct ArrayPat { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(rename = "elements"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "swc_common::serializer::ArrayOption") + )] pub elems: Vec>, /// Only in an ambient context @@ -98,6 +104,10 @@ pub struct ArrayPat { pub optional: bool, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, } @@ -116,6 +126,10 @@ pub struct ObjectPat { pub optional: bool, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, } @@ -146,6 +160,10 @@ pub struct RestPat { pub arg: Box, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, } @@ -188,5 +206,9 @@ pub struct AssignPatProp { pub key: BindingIdent, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub value: Option>, } diff --git a/crates/swc_ecma_ast/src/prop.rs b/crates/swc_ecma_ast/src/prop.rs index ee3110f85545..569a235ccf59 100644 --- a/crates/swc_ecma_ast/src/prop.rs +++ b/crates/swc_ecma_ast/src/prop.rs @@ -70,8 +70,16 @@ pub struct GetterProp { pub span: Span, pub key: PropName, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub body: Option, } #[ast_node("SetterProperty")] @@ -81,9 +89,17 @@ pub struct GetterProp { pub struct SetterProp { pub span: Span, pub key: PropName, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub this_param: Option, pub param: Box, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub body: Option, } #[ast_node("MethodProperty")] @@ -149,6 +165,8 @@ impl From for MemberProp { span: DUMMY_SP, expr: p.into(), }), + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } } diff --git a/crates/swc_ecma_ast/src/stmt.rs b/crates/swc_ecma_ast/src/stmt.rs index 478a4e53c298..328308aec6d2 100644 --- a/crates/swc_ecma_ast/src/stmt.rs +++ b/crates/swc_ecma_ast/src/stmt.rs @@ -176,6 +176,8 @@ impl Clone for Stmt { fn clone(&self) -> Self { use Stmt::*; match self { + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + Unknown(tag, v) => Unknown(*tag, v.clone()), Block(s) => Block(s.clone()), Empty(s) => Empty(s.clone()), Debugger(s) => Debugger(s.clone()), @@ -256,6 +258,10 @@ pub struct WithStmt { pub struct ReturnStmt { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(default, rename = "argument"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub arg: Option>, } @@ -276,6 +282,10 @@ pub struct LabeledStmt { pub struct BreakStmt { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub label: Option, } @@ -286,6 +296,10 @@ pub struct BreakStmt { pub struct ContinueStmt { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub label: Option, } @@ -301,6 +315,10 @@ pub struct IfStmt { pub cons: Box, #[cfg_attr(feature = "serde-impl", serde(default, rename = "alternate"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub alt: Option>, } @@ -334,9 +352,17 @@ pub struct TryStmt { pub block: BlockStmt, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub handler: Option, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub finalizer: Option, } @@ -368,12 +394,24 @@ pub struct ForStmt { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub init: Option, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub test: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub update: Option>, pub body: Box, @@ -423,6 +461,10 @@ pub struct SwitchCase { /// None for `default:` #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub test: Option>, #[cfg_attr(feature = "serde-impl", serde(rename = "consequent"))] @@ -450,6 +492,10 @@ pub struct CatchClause { /// The param is null if the catch binding is omitted. E.g., try { foo() } /// catch { bar() } #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub param: Option, pub body: BlockStmt, diff --git a/crates/swc_ecma_ast/src/typescript.rs b/crates/swc_ecma_ast/src/typescript.rs index fc983b812ac8..4c5556b6547d 100644 --- a/crates/swc_ecma_ast/src/typescript.rs +++ b/crates/swc_ecma_ast/src/typescript.rs @@ -62,9 +62,17 @@ pub struct TsTypeParam { pub is_const: bool, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub constraint: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub default: Option>, } @@ -87,6 +95,10 @@ pub struct TsParamProp { pub decorators: Vec, /// At least one of `accessibility` or `readonly` must be set. #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub accessibility: Option, #[cfg_attr(feature = "serde-impl", serde(rename = "override"))] pub is_override: bool, @@ -168,8 +180,16 @@ pub struct TsCallSignatureDecl { pub span: Span, pub params: Vec, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, } @@ -181,8 +201,16 @@ pub struct TsConstructSignatureDecl { pub span: Span, pub params: Vec, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, } @@ -197,6 +225,10 @@ pub struct TsPropertySignature { pub computed: bool, pub optional: bool, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, } @@ -209,6 +241,10 @@ pub struct TsGetterSignature { pub key: Box, pub computed: bool, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, } @@ -234,8 +270,16 @@ pub struct TsMethodSignature { pub optional: bool, pub params: Vec, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, } @@ -246,6 +290,10 @@ pub struct TsMethodSignature { pub struct TsIndexSignature { pub params: Vec, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, pub readonly: bool, @@ -332,6 +380,8 @@ impl Clone for TsType { fn clone(&self) -> Self { use TsType::*; match self { + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + Unknown(tag, v) => Unknown(*tag, v.clone()), TsKeywordType(t) => TsKeywordType(t.clone()), TsThisType(t) => TsThisType(t.clone()), TsFnOrConstructorType(t) => TsFnOrConstructorType(t.clone()), @@ -410,6 +460,10 @@ pub struct TsKeywordType { #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum TsKeywordTypeKind { #[cfg_attr(feature = "serde-impl", serde(rename = "any"))] @@ -487,6 +541,10 @@ pub struct TsFnType { pub params: Vec, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, #[cfg_attr(feature = "serde-impl", serde(rename = "typeAnnotation"))] pub type_ann: Box, @@ -500,6 +558,10 @@ pub struct TsConstructorType { pub span: Span, pub params: Vec, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, #[cfg_attr(feature = "serde-impl", serde(rename = "typeAnnotation"))] pub type_ann: Box, @@ -514,6 +576,10 @@ pub struct TsTypeRef { pub span: Span, pub type_name: TsEntityName, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, } @@ -526,6 +592,10 @@ pub struct TsTypePredicate { pub asserts: bool, pub param_name: TsThisTypeOrIdent, #[cfg_attr(feature = "serde-impl", serde(rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, } @@ -551,6 +621,10 @@ pub struct TsTypeQuery { pub span: Span, pub expr_name: TsTypeQueryExpr, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeArguments"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_args: Option>, } @@ -584,10 +658,22 @@ pub struct TsImportType { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(rename = "argument"))] pub arg: Str, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub qualifier: Option, #[cfg_attr(feature = "serde-impl", serde(rename = "typeArguments"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_args: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub attributes: Option, } @@ -625,6 +711,10 @@ pub struct TsTupleType { pub struct TsTupleElement { pub span: Span, /// `Ident` or `RestPat { arg: Ident }` + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub label: Option, pub ty: Box, } @@ -730,6 +820,10 @@ pub struct TsTypeOperator { )] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum TsTypeOperatorOp { /// `keyof` @@ -761,6 +855,10 @@ pub struct TsIndexedAccessType { )] #[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum TruePlusMinus { True, @@ -832,13 +930,29 @@ impl<'de> Deserialize<'de> for TruePlusMinus { pub struct TsMappedType { pub span: Span, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub readonly: Option, pub type_param: TsTypeParam, #[cfg_attr(feature = "serde-impl", serde(default, rename = "nameType"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name_type: Option>, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub optional: Option, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_ann: Option>, } @@ -898,6 +1012,10 @@ pub struct TsInterfaceDecl { pub id: Ident, pub declare: bool, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, pub extends: Vec, pub body: TsInterfaceBody, @@ -921,6 +1039,10 @@ pub struct TsExprWithTypeArgs { #[cfg_attr(feature = "serde-impl", serde(rename = "expression"))] pub expr: Box, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeArguments"))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_args: Option>, } @@ -933,6 +1055,10 @@ pub struct TsTypeAliasDecl { pub declare: bool, pub id: Ident, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub type_params: Option>, #[cfg_attr(feature = "serde-impl", serde(rename = "typeAnnotation"))] pub type_ann: Box, @@ -958,6 +1084,10 @@ pub struct TsEnumMember { pub span: Span, pub id: TsEnumMemberId, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub init: Option>, } @@ -980,6 +1110,8 @@ impl AsRef for TsEnumMemberId { match &self { TsEnumMemberId::Str(Str { value: ref sym, .. }) | TsEnumMemberId::Ident(Ident { ref sym, .. }) => sym, + #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))] + _ => swc_common::unknown(), } } } @@ -997,6 +1129,10 @@ pub struct TsModuleDecl { pub id: TsModuleName, #[cfg_attr(feature = "serde-impl", serde(default))] + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub body: Option, } @@ -1165,6 +1301,10 @@ pub struct TsSatisfiesExpr { #[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] #[cfg_attr(swc_ast_unknown, non_exhaustive)] pub enum Accessibility { #[cfg_attr(feature = "serde-impl", serde(rename = "public"))] diff --git a/crates/swc_ecma_parser/Cargo.toml b/crates/swc_ecma_parser/Cargo.toml index cb46829fa396..f21e40c8ae86 100644 --- a/crates/swc_ecma_parser/Cargo.toml +++ b/crates/swc_ecma_parser/Cargo.toml @@ -48,10 +48,12 @@ walkdir = { workspace = true } codspeed-criterion-compat = { workspace = true } swc_ecma_ast = { version = "16.0.0", path = "../swc_ecma_ast", features = [ "serde-impl", + "encoding-impl", ] } swc_ecma_visit = { version = "16.0.0", path = "../swc_ecma_visit" } swc_malloc = { version = "1.2.3", path = "../swc_malloc" } testing = { version = "16.0.0", path = "../testing" } +cbor4ii = { workspace = true, features = [ "use_std" ] } [[example]] name = "typescript" diff --git a/crates/swc_ecma_parser/tests/js.rs b/crates/swc_ecma_parser/tests/js.rs index fe796cc6a00d..255eb6223131 100644 --- a/crates/swc_ecma_parser/tests/js.rs +++ b/crates/swc_ecma_parser/tests/js.rs @@ -63,11 +63,30 @@ fn run_spec(file: &Path, output_json: &Path, config_path: &Path) { is_test262: false, }); - let json = - serde_json::to_string_pretty(&program).expect("failed to serialize module as json"); + // json + { + let json = + serde_json::to_string_pretty(&program).expect("failed to serialize module as json"); + + if StdErr::from(json).compare_to_file(output_json).is_err() { + panic!() + } + } + + // cbor + { + use cbor4ii::core::{ + dec::Decode, + enc::Encode, + utils::{BufWriter, SliceReader}, + }; + + let mut buf = BufWriter::new(Vec::new()); + program.encode(&mut buf).unwrap(); - if StdErr::from(json).compare_to_file(output_json).is_err() { - panic!() + let buf = buf.into_inner(); + let mut buf = SliceReader::new(buf.as_slice()); + let _program = Program::decode(&mut buf).unwrap(); } Ok(()) diff --git a/crates/swc_ecma_regexp_ast/Cargo.toml b/crates/swc_ecma_regexp_ast/Cargo.toml index b08d703380b6..a230a48351b7 100644 --- a/crates/swc_ecma_regexp_ast/Cargo.toml +++ b/crates/swc_ecma_regexp_ast/Cargo.toml @@ -16,11 +16,13 @@ __rkyv = [] default = [] rkyv-impl = [] serde-impl = ["serde"] +encoding-impl = ["cbor4ii", "swc_atoms/encoding-impl", "swc_common/encoding-impl"] [dependencies] bitflags = { workspace = true } is-macro = { workspace = true } serde = { workspace = true, features = ["derive"], optional = true } +cbor4ii = { workspace = true, features = [ "use_std" ], optional = true } swc_atoms = { version = "7.0.0", path = "../swc_atoms" } swc_common = { version = "15.0.0", path = "../swc_common" } diff --git a/crates/swc_ecma_regexp_ast/src/lib.rs b/crates/swc_ecma_regexp_ast/src/lib.rs index 42c2cef25497..103e635c6dd5 100644 --- a/crates/swc_ecma_regexp_ast/src/lib.rs +++ b/crates/swc_ecma_regexp_ast/src/lib.rs @@ -85,6 +85,10 @@ pub struct BoundaryAssertion { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum BoundaryAssertionKind { Start = 0, End = 1, @@ -104,6 +108,10 @@ pub struct LookAroundAssertion { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum LookAroundAssertionKind { Lookahead = 0, NegativeLookahead = 1, @@ -119,6 +127,10 @@ pub struct Quantifier { pub span: Span, pub min: u64, /// `None` means no upper bound. + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub max: Option, pub greedy: bool, pub body: Term, @@ -138,6 +150,10 @@ pub struct Character { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum CharacterKind { ControlLetter = 0, HexadecimalEscape = 1, @@ -163,6 +179,10 @@ pub struct CharacterClassEscape { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum CharacterClassEscapeKind { D = 0, NegativeD = 1, @@ -183,6 +203,10 @@ pub struct UnicodePropertyEscape { /// strings. pub strings: bool, pub name: Atom, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub value: Option, } @@ -211,6 +235,10 @@ pub struct CharacterClass { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum CharacterClassContentsKind { Union = 0, /// `UnicodeSetsMode` only. @@ -281,6 +309,10 @@ pub struct ClassString { pub struct CapturingGroup { pub span: Span, /// Group name to be referenced by [`NamedReference`]. + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, pub body: Disjunction, } @@ -291,6 +323,10 @@ pub struct CapturingGroup { #[derive(Eq, Hash, EqIgnoreSpan)] pub struct IgnoreGroup { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub modifiers: Option, pub body: Disjunction, } @@ -338,6 +374,29 @@ pub struct NamedReference { pub name: Atom, } +#[cfg(feature = "encoding-impl")] +impl cbor4ii::core::enc::Encode for Modifier { + fn encode( + &self, + writer: &mut W, + ) -> Result<(), cbor4ii::core::enc::Error> { + self.bits().encode(writer) + } +} + +#[cfg(feature = "encoding-impl")] +impl<'de> cbor4ii::core::dec::Decode<'de> for Modifier { + fn decode>( + reader: &mut R, + ) -> Result> { + let n = u8::decode(reader)?; + Modifier::from_bits(n).ok_or_else(|| cbor4ii::core::dec::Error::Mismatch { + name: &"Modifier", + found: 0, + }) + } +} + #[cfg(target_pointer_width = "64")] #[test] fn size_asserts() { diff --git a/crates/swc_html_ast/Cargo.toml b/crates/swc_html_ast/Cargo.toml index 19350da0121d..aadf687c4b0e 100644 --- a/crates/swc_html_ast/Cargo.toml +++ b/crates/swc_html_ast/Cargo.toml @@ -24,12 +24,14 @@ rkyv-impl = [ "bytecheck", ] serde-impl = ["serde"] +encoding-impl = ["cbor4ii", "swc_atoms/encoding-impl", "swc_common/encoding-impl"] [dependencies] bytecheck = { workspace = true, optional = true } is-macro = { workspace = true } rkyv = { workspace = true, optional = true } serde = { workspace = true, features = ["derive"], optional = true } +cbor4ii = { workspace = true, features = [ "use_std" ], optional = true } string_enum = { version = "1.0.2", path = "../string_enum/" } swc_atoms = { version = "7.0.0", path = "../swc_atoms" } diff --git a/crates/swc_html_ast/src/base.rs b/crates/swc_html_ast/src/base.rs index 7881fb4a00f7..ae218a4e16cc 100644 --- a/crates/swc_html_ast/src/base.rs +++ b/crates/swc_html_ast/src/base.rs @@ -30,6 +30,10 @@ pub struct DocumentFragment { // rkyv::ser::Serializer")) //)] #[cfg_attr(feature = "rkyv", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum DocumentMode { /// `no-quirks` NoQuirks, @@ -57,11 +61,27 @@ pub enum Child { pub struct DocumentType { pub span: Span, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub name: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub public_id: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub system_id: Option, + #[cfg_attr( + feature = "encoding-impl", + encoding(with = "cbor4ii::core::types::Maybe") + )] pub raw: Option, } @@ -85,6 +105,10 @@ impl EqIgnoreSpan for DocumentType { // rkyv::ser::Serializer")) //)] #[cfg_attr(feature = "rkyv", repr(u32))] +#[cfg_attr( + feature = "encoding-impl", + derive(::swc_common::Encode, ::swc_common::Decode) +)] pub enum Namespace { /// `http://www.w3.org/1999/xhtml` HTML, @@ -110,6 +134,10 @@ pub struct Element { pub attributes: Vec, pub children: Vec, /// For child nodes in `