Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to handle wrapped classes to extend the set of use cases #109

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

dikkadev
Copy link

As discussed in #108, a new CLI option, class_wrapping, is added to cover more use cases in combination with the existing custom_regex option.

If the option is not supplied, the default value is used, and the behavior is not changed from before.

Fixes #108

@praveenperera
Copy link
Member

Hey @Sett17 sorry I just saw this, could you resolve conflicts and add some tests and I’ll merge this in?

Thank you!

The regex for matching class attributes in HTML has been updated to include support for CSS variables and to optimize for scenarios where the "Name" part of "className" is optional. This enhancement ensures better performance and compatibility with modern CSS practices.
This update introduces a new feature that allows users to specify how individual classes are wrapped in the CLI. This enhancement includes changes in the CLI options to accept a new parameter for class wrapping, adjusts the core sorting logic to accommodate this new setting, and ensures compatibility with existing functionality. The addition of `HowClassesAreWrapped` enum and related functions facilitates handling different class wrapping styles, providing users with more flexibility in how they organize and process their CSS classes.
@dikkadev
Copy link
Author

add some tests

Hey, I've already added some tests:

// CLASS WRAPPING
#[test_case(
r#"flex-col inline flex"#,
HowClassesAreWrapped::NoWrapping,
vec![r#"flex-col"#, r#"inline"#, r#"flex"#]
; "no wrapping"
)]
#[test_case(
r#"'flex-col', 'inline', 'flex'"#,
HowClassesAreWrapped::CommaSingleQuotes,
vec![r#"flex-col"#, r#"inline"#, r#"flex"#]
; "comma single quotes"
)]
#[test_case(
r#""flex-col", "inline", "flex""#,
HowClassesAreWrapped::CommaDoubleQuotes,
vec![r#"flex-col"#, r#"inline"#, r#"flex"#]
; "comma double quotes"
)]
fn test_unwrap_wrapped_classes<'a>(
input: &str,
wrapping: HowClassesAreWrapped,
output: Vec<&str>,
) {
assert_eq!(unwrap_wrapped_classes(input, wrapping), output)
}
#[test_case(
vec![r#"flex-col"#, r#"inline"#, r#"flex"#],
HowClassesAreWrapped::NoWrapping,
r#"flex-col inline flex"#
; "no wrapping"
)]
#[test_case(
vec![r#"flex-col"#, r#"inline"#, r#"flex"#],
HowClassesAreWrapped::CommaSingleQuotes,
r#"'flex-col', 'inline', 'flex'"#
; "comma single quotes"
)]
#[test_case(
vec![r#"flex-col"#, r#"inline"#, r#"flex"#],
HowClassesAreWrapped::CommaDoubleQuotes,
r#""flex-col", "inline", "flex""#
; "comma double quotes"
)]
fn test_rewrap_wrapped_classes<'a>(
input: Vec<&'a str>,
wrapping: HowClassesAreWrapped,
output: &str,
) {
assert_eq!(rewrap_wrapped_classes(input, wrapping), output)
}
#[test_case(
None,
HowClassesAreWrapped::NoWrapping,
r#"<div class="flex-col inline flex"></div>"#,
r#"<div class="inline flex flex-col"></div>"#
; "normal HTML use case"
)]
#[test_case(
Some(r#"(?:\[)([_a-zA-Z0-9\.,\-'"\s]+)(?:\])"#),
HowClassesAreWrapped::CommaSingleQuotes,
r#"classes = ['flex-col', 'inline', 'flex']"#,
r#"classes = ['inline', 'flex', 'flex-col']"#
; "array with single quotes"
)]
#[test_case(
Some(r#"(?:\[)([_a-zA-Z0-9\.,\-'"\s]+)(?:\])"#),
HowClassesAreWrapped::CommaDoubleQuotes,
r#"classes = ["flex-col", "inline", "flex"]"#,
r#"classes = ["inline", "flex", "flex-col"]"#
; "array with double quotes"
)]
fn test_unusual_use_cases(
regex_overwrite: Option<&str>,
class_wrapping: HowClassesAreWrapped,
input: &str,
output: &str,
) {
let regex = match regex_overwrite {
Some(re) => FinderRegex::CustomRegex(Regex::new(re).unwrap()),
None => FinderRegex::DefaultRegex,
};
let opts = Options {
regex,
sorter: Sorter::DefaultSorter,
allow_duplicates: false,
class_wrapping,
};
assert_eq!(sort_file_contents(input, &opts), output);
}

Are more or different tests needed?

@dikkadev
Copy link
Author

Let me know if this works for you; I can remember having some problems with the workspace setup when I made the changes (although it was 2 months ago, so I probably don't remember it 100%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make viable for more use cases
2 participants