diff --git a/CHANGELOG.md b/CHANGELOG.md index 34d7a37..59e1907 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased] - Fix not sorting half classes properly `ex: mt-0.5` +- Sort all variant classes ## [0.11.0] - 2021-06-11 diff --git a/Cargo.lock b/Cargo.lock index e72f2ee..7042eb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -347,6 +347,7 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" name = "rustywind" version = "0.11.0" dependencies = [ + "aho-corasick", "clap", "ignore", "indoc", diff --git a/Cargo.toml b/Cargo.toml index c447237..986e9f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,9 +17,12 @@ ignore = "0.4" # utils itertools = "0.10" lazy_static = "1.4.0" + +# string matching +aho-corasick = "0.7" regex = "1" -# parrelel +# parallel rayon = "1.5" [dev-dependencies] diff --git a/src/consts.rs b/src/consts.rs index 0e163db..7e38e34 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -1,6 +1,39 @@ +use aho_corasick::{AhoCorasick, AhoCorasickBuilder, MatchKind}; use lazy_static::lazy_static; lazy_static! { - pub static ref RESPONSIVE_SIZES: Vec<&'static str> = - vec!["sm", "md", "lg", "xl", "2xl", "3xl", "4xl", "5xl", "6xl"]; + pub static ref VARIANTS: Vec<&'static str> = vec![ + "sm", + "md", + "lg", + "xl", + "2xl", + "3xl", + "4xl", + "5xl", + "6xl", + "dark", + "first", + "last", + "odd", + "even", + "visited", + "checked", + "group-hover", + "group-focus", + "hover", + "focus", + "focus-visible", + "focus-within", + "active", + "disabled" + ]; +} + +lazy_static! { + pub static ref VARIANT_SEARCHER: AhoCorasick = AhoCorasickBuilder::new() + .anchored(true) + .match_kind(MatchKind::LeftmostLongest) + .auto_configure(&VARIANTS) + .build(VARIANTS.iter()); } diff --git a/src/lib.rs b/src/lib.rs index 9e10ea2..853fabf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ pub mod consts; pub mod defaults; pub mod options; -use consts::RESPONSIVE_SIZES; +use consts::{VARIANTS, VARIANT_SEARCHER}; use defaults::{RE, SORTER}; use options::Options; @@ -52,41 +52,16 @@ fn sort_classes_vec<'a>(classes: impl Iterator) -> Vec<&'a str> for (class, maybe_size) in enumerated_classes { match maybe_size { Some(size) => tailwind_classes.push((class, size)), - None => match class.as_bytes() { - [b's', b'm', b':', ..] => responsive.entry("sm").or_insert(Vec::new()).push(class), - - [b'm', b'd', b':', ..] => responsive.entry("md").or_insert(Vec::new()).push(class), - - [b'l', b'g', b':', ..] => responsive.entry("lg").or_insert(Vec::new()).push(class), - - [b'x', b'l', b':', ..] => responsive.entry("xl").or_insert(Vec::new()).push(class), - - [b'2', b'x', b'l', b':', ..] => responsive - .entry("2xl") - .or_insert_with(|| vec![]) - .push(class), - - [b'3', b'x', b'l', b':', ..] => responsive - .entry("3xl") - .or_insert_with(|| vec![]) - .push(class), - - [b'4', b'x', b'l', b':', ..] => responsive - .entry("4xl") - .or_insert_with(|| vec![]) - .push(class), - - [b'5', b'x', b'l', b':', ..] => responsive - .entry("5xl") - .or_insert_with(|| vec![]) - .push(class), - - [b'6', b'x', b'l', b':', ..] => responsive - .entry("6xl") - .or_insert_with(|| vec![]) - .push(class), - - _ => custom_classes.push(class), + None => match VARIANT_SEARCHER.find(&class) { + Some(prefix_match) => { + let prefix = VARIANTS[prefix_match.pattern()]; + responsive + .entry(prefix) + .or_insert_with(Vec::new) + .push(class) + } + + None => custom_classes.push(class), }, } } @@ -100,9 +75,9 @@ fn sort_classes_vec<'a>(classes: impl Iterator) -> Vec<&'a str> let mut sorted_responsive_classes = vec![]; - for key in RESPONSIVE_SIZES.iter() { + for key in VARIANTS.iter() { let (mut sorted_classes, new_custom_classes) = sort_responsive_classes( - responsive.remove(key).unwrap_or_else(|| vec![]), + responsive.remove(key).unwrap_or_else(Vec::new), custom_classes, key.len() + 1, ); diff --git a/tests/rustywind_test.rs b/tests/rustywind_test.rs index 89ba46b..1d4bb42 100644 --- a/tests/rustywind_test.rs +++ b/tests/rustywind_test.rs @@ -247,3 +247,28 @@ fn test_sort_file_contents_with_responsive_classes() { expected_outcome ) } + +#[test] +fn test_sort_file_contents_with_variant_classes() { + let file_contents = r#" +
+
+
    +
+
+"#; + + let expected_outcome = r#" +
+
+
    +
+
+"#.to_string(); + + assert_eq!( + rustywind::sort_file_contents(file_contents, &default_options_for_test()), + expected_outcome + ) +}