Skip to content

Commit

Permalink
Merge pull request #18 from ProspectiveCo/fix-var-regression
Browse files Browse the repository at this point in the history
Fix regression in transitive var import
  • Loading branch information
texodus authored Nov 24, 2023
2 parents 74188f7 + 7bf58b2 commit 6a08b46
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 12 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
1 change: 1 addition & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
6 changes: 0 additions & 6 deletions src/transformers/apply_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
27 changes: 27 additions & 0 deletions src/transformers/remove_var.rs
Original file line number Diff line number Diff line change
@@ -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())
}
6 changes: 4 additions & 2 deletions tests/apply_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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;}")
Expand Down
10 changes: 7 additions & 3 deletions tests/apply_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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;}")
Expand All @@ -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;}")
Expand Down

0 comments on commit 6a08b46

Please sign in to comment.