From 7bf58b2b236a455bcc1be97908715d57f9075a85 Mon Sep 17 00:00:00 2001 From: Andrew Stein Date: Fri, 24 Nov 2023 17:59:10 -0500 Subject: [PATCH] Fix regression in transitive var import --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/builder.rs | 1 + src/main.rs | 4 +++- src/transformers/apply_var.rs | 6 ------ src/transformers/mod.rs | 2 ++ src/transformers/remove_var.rs | 27 +++++++++++++++++++++++++++ tests/apply_import.rs | 6 ++++-- tests/apply_var.rs | 10 +++++++--- 9 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/transformers/remove_var.rs diff --git a/Cargo.lock b/Cargo.lock index a2cf26c..08de1eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -498,6 +498,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + [[package]] name = "core-foundation-sys" version = "0.8.4" @@ -1570,6 +1580,7 @@ version = "0.1.13" dependencies = [ "anyhow", "base64 0.13.1", + "console_error_panic_hook", "criterion", "js-sys", "mockall", diff --git a/Cargo.toml b/Cargo.toml index 8f2880d..4c0c49d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ wasm-bindgen-cli = { version = "0.2.83", artifact = "bin" } wasm-opt = "0.111.0" [target.'cfg(target_arch = "wasm32")'.dependencies] +console_error_panic_hook = "0.1.7" js-sys = "0.3.60" serde-wasm-bindgen = "0.4.5" wasm-bindgen = { version = "0.2.83", features = ["serde-serialize"] } diff --git a/src/builder.rs b/src/builder.rs index 8b697cd..c81145b 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -95,6 +95,7 @@ impl<'a> BuildCss<'a> { transformers::inline_url(&srcdir.to_string_lossy())(css); transformers::merge_siblings(css); transformers::remove_mixin(css); + transformers::remove_var(css); transformers::deduplicate(css); } diff --git a/src/main.rs b/src/main.rs index 5f76dc8..63e52be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,4 +36,6 @@ fn main() { } #[cfg(target_arch = "wasm32")] -fn main() {} +fn main() { + std::panic::set_hook(Box::new(console_error_panic_hook::hook)); +} diff --git a/src/transformers/apply_var.rs b/src/transformers/apply_var.rs index 836a3ae..dc980c0 100644 --- a/src/transformers/apply_var.rs +++ b/src/transformers/apply_var.rs @@ -17,17 +17,11 @@ use crate::ast::*; pub fn apply_var<'a>(tree: &mut Tree<'a>) { let mut mixins: HashMap<&'a str, &'a str> = HashMap::new(); tree.transform(|ruleset| { - let mut is_mixin = false; if let Ruleset::QualRule(QualRule(name, Some(val))) = ruleset { if let Some(val) = val.strip_prefix(':') { mixins.insert(name, val); - is_mixin = true; } } - - if is_mixin { - *ruleset = Ruleset::QualRuleset(QualRuleset(QualRule("", None), vec![])) - } }); let mut mixins = mixins.iter().collect::>(); diff --git a/src/transformers/mod.rs b/src/transformers/mod.rs index eb67af0..210bbe5 100644 --- a/src/transformers/mod.rs +++ b/src/transformers/mod.rs @@ -35,6 +35,7 @@ mod flat_self; mod inline_url; mod merge_siblings; mod remove_mixin; +mod remove_var; pub use self::apply_import::apply_import; pub use self::apply_mixin::apply_mixin; @@ -45,3 +46,4 @@ pub(crate) use self::flat_self::flat_self; pub use self::inline_url::inline_url; pub use self::merge_siblings::merge_siblings; pub use self::remove_mixin::remove_mixin; +pub use self::remove_var::remove_var; diff --git a/src/transformers/remove_var.rs b/src/transformers/remove_var.rs new file mode 100644 index 0000000..3bcc01c --- /dev/null +++ b/src/transformers/remove_var.rs @@ -0,0 +1,27 @@ +// ┌───────────────────────────────────────────────────────────────────────────┐ +// │ │ +// │ ██████╗ ██████╗ ██████╗ Copyright (C) 2022, The Prospective Company │ +// │ ██╔══██╗██╔══██╗██╔═══██╗ │ +// │ ██████╔╝██████╔╝██║ ██║ This file is part of the Procss library, │ +// │ ██╔═══╝ ██╔══██╗██║ ██║ distributed under the terms of the │ +// │ ██║ ██║ ██║╚██████╔╝ Apache License 2.0. The full license can │ +// │ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ be found in the LICENSE file. │ +// │ │ +// └───────────────────────────────────────────────────────────────────────────┘ + +use crate::ast::Ruleset::{self}; +use crate::ast::*; + +pub fn remove_var(css: &mut Css) { + let reduced = css + .iter() + .filter(|&ruleset| match ruleset { + Ruleset::QualRule(QualRule(name, Some(val))) if val.strip_prefix(':').is_some() => { + false + } + _ => true, + }) + .cloned(); + + *css = crate::ast::Css(reduced.collect()) +} diff --git a/tests/apply_import.rs b/tests/apply_import.rs index 20e60a6..036a688 100644 --- a/tests/apply_import.rs +++ b/tests/apply_import.rs @@ -16,7 +16,7 @@ use std::assert_matches::assert_matches; use std::collections::HashMap; use std::path::Path; -use procss::transformers::{apply_import, apply_var}; +use procss::transformers::{apply_import, apply_var, remove_var}; use procss::{parse, RenderCss}; #[test] @@ -63,7 +63,9 @@ fn test_import_ref() { .map(|mut x| { apply_import(&trees)(&mut x); apply_var(&mut x); - x.flatten_tree().as_css_string() + let mut flat = x.flatten_tree(); + remove_var(&mut flat); + flat.as_css_string() }) .as_deref(), Ok("div.open{color:#00FF00;}") diff --git a/tests/apply_var.rs b/tests/apply_var.rs index 004f289..5dc2160 100644 --- a/tests/apply_var.rs +++ b/tests/apply_var.rs @@ -14,7 +14,7 @@ #[cfg(test)] use std::assert_matches::assert_matches; -use procss::transformers::apply_var; +use procss::transformers::{apply_var, remove_var}; use procss::{parse, RenderCss}; #[test] @@ -31,7 +31,9 @@ fn test_var() { ) .map(|mut x| { apply_var(&mut x); - x.flatten_tree().as_css_string() + let mut flat = x.flatten_tree(); + remove_var(&mut flat); + flat.as_css_string() }) .as_deref(), Ok("div.open{color:#FF1111;}") @@ -52,7 +54,9 @@ fn test_var_overlapping_name() { ) .map(|mut x| { apply_var(&mut x); - x.flatten_tree().as_css_string() + let mut flat = x.flatten_tree(); + remove_var(&mut flat); + flat.as_css_string() }) .as_deref(), Ok("div.open{color:#0000FF;}")