Skip to content

Commit

Permalink
chore: rename local by default options
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed May 11, 2024
1 parent 9cfecee commit 161fe0e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/postcss_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use crate::Range;
use crate::Warning;

#[derive(Debug, Default, Clone, Hash, PartialEq, Eq)]
pub struct Options {
pub struct LocalByDefaultOptions {
pub mode: Mode,
}

pub fn local_by_default(input: &str, options: Options) -> (String, Vec<Warning>) {
pub fn local_by_default(input: &str, options: LocalByDefaultOptions) -> (String, Vec<Warning>) {
let mut result = String::new();
let mut warnings = Vec::new();
let mut index = 0;
Expand Down
57 changes: 35 additions & 22 deletions tests/postcss_modules/local_by_default_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use css_module_lexer::postcss_modules::local_by_default;
use css_module_lexer::postcss_modules::Options;
use css_module_lexer::postcss_modules::LocalByDefaultOptions;
use css_module_lexer::Mode;
use indoc::indoc;

Expand All @@ -9,7 +9,7 @@ fn test(input: &str, expected: &str) {
assert!(warnings.is_empty(), "{}", &warnings[0]);
}

fn test_with_options(input: &str, expected: &str, options: Options) {
fn test_with_options(input: &str, expected: &str, options: LocalByDefaultOptions) {
let (actual, warnings) = local_by_default(input, options);
assert_eq!(expected, actual);
assert!(warnings.is_empty(), "{}", &warnings[0]);
Expand All @@ -25,7 +25,12 @@ fn test_with_warning(input: &str, expected: &str, warning: &str) {
);
}

fn test_with_options_warning(input: &str, expected: &str, options: Options, warning: &str) {
fn test_with_options_warning(
input: &str,
expected: &str,
options: LocalByDefaultOptions,
warning: &str,
) {
let (actual, warnings) = local_by_default(input, options);
assert_eq!(expected, actual);
assert!(
Expand Down Expand Up @@ -522,12 +527,20 @@ fn handle_constructor_as_animation_name() {

#[test]
fn default_to_global_when_mode_provided() {
test_with_options(".foo {}", ".foo {}", Options { mode: Mode::Global });
test_with_options(
".foo {}",
".foo {}",
LocalByDefaultOptions { mode: Mode::Global },
);
}

#[test]
fn default_to_local_when_mode_provided() {
test_with_options(".foo {}", ":local(.foo) {}", Options { mode: Mode::Local });
test_with_options(
".foo {}",
":local(.foo) {}",
LocalByDefaultOptions { mode: Mode::Local },
);
}

#[test]
Expand Down Expand Up @@ -557,7 +570,7 @@ fn use_correct_spacing() {
:local(.a) .b {}
:local(.a) .b {}
"#},
Options { mode: Mode::Global },
LocalByDefaultOptions { mode: Mode::Global },
)
}

Expand Down Expand Up @@ -590,7 +603,7 @@ fn localize_keyframes_in_global_default_mode() {
test_with_options(
"@keyframes foo {}",
"@keyframes foo {}",
Options { mode: Mode::Global },
LocalByDefaultOptions { mode: Mode::Global },
);
}

Expand Down Expand Up @@ -628,7 +641,7 @@ fn compile_in_pure_mode() {
test_with_options(
":global(.foo).bar, [type=\"radio\"] ~ .label, :not(.foo), #bar {}",
".foo:local(.bar), [type=\"radio\"] ~ :local(.label), :not(:local(.foo)), :local(#bar) {}",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
);
}

Expand Down Expand Up @@ -712,7 +725,7 @@ fn throw_on_not_pure_selector_global_class() {
test_with_options_warning(
":global(.foo) {}",
".foo {}",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
"Selector is not pure",
);
}
Expand All @@ -722,13 +735,13 @@ fn throw_on_not_pure_selector_with_multiple() {
test_with_options_warning(
".foo, :global(.bar) {}",
":local(.foo), .bar {}",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
"Selector is not pure",
);
test_with_options_warning(
":global(.bar), .foo {}",
".bar, :local(.foo) {}",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
"Selector is not pure",
);
}
Expand All @@ -738,13 +751,13 @@ fn throw_on_not_pure_selector_element() {
test_with_options_warning(
"input {}",
"input {}",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
"Selector is not pure",
);
test_with_options_warning(
"[type=\"radio\"] {}",
"[type=\"radio\"] {}",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
"Selector is not pure",
);
}
Expand All @@ -754,7 +767,7 @@ fn throw_on_not_pure_keyframes() {
test_with_options_warning(
"@keyframes :global(foo) {}",
"@keyframes foo {}",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
"'@keyframes :global' is not allowed in pure mode",
);
}
Expand Down Expand Up @@ -1004,7 +1017,7 @@ fn consider_nesting_statements_as_pure() {
test_with_options(
".foo { &:hover { a_value: some-value; } }",
":local(.foo) { &:hover { a_value: some-value; } }",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
);
}

Expand All @@ -1013,12 +1026,12 @@ fn consider_selector_nesting_statements_as_pure() {
test_with_options(
".foo { html &:hover { a_value: some-value; } }",
":local(.foo) { html &:hover { a_value: some-value; } }",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
);
test_with_options(
".foo { &:global(.bar) { a_value: some-value; } }",
":local(.foo) { &.bar { a_value: some-value; } }",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
);
}

Expand All @@ -1027,7 +1040,7 @@ fn throw_on_nested_nesting_selectors_without_a_local_selector() {
test_with_options_warning(
":global(.foo) { &:hover { a_value: some-value; } }",
".foo { &:hover { a_value: some-value; } }",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
"Selector is not pure",
);
}
Expand Down Expand Up @@ -1193,7 +1206,7 @@ fn css_nesting() {
}
}
"#},
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
);
}

Expand All @@ -1211,7 +1224,7 @@ fn consider_export_statements_pure() {
test_with_options(
":export { foo: __foo; }",
":export { foo: __foo; }",
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
);
}

Expand Down Expand Up @@ -1376,7 +1389,7 @@ fn at_scope_at_rule() {
}
}
"#},
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
);
test(
indoc! {r#"
Expand Down Expand Up @@ -1437,6 +1450,6 @@ fn at_scope_at_rule() {
}
}
"#},
Options { mode: Mode::Pure },
LocalByDefaultOptions { mode: Mode::Pure },
);
}

0 comments on commit 161fe0e

Please sign in to comment.