From 161fe0e409d0cc6698eaeacfc32404ab596ed062 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Sun, 12 May 2024 00:08:06 +0800 Subject: [PATCH] chore: rename local by default options --- src/postcss_modules.rs | 4 +- .../postcss_modules/local_by_default_test.rs | 57 ++++++++++++------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/postcss_modules.rs b/src/postcss_modules.rs index 36efef2..d9a402f 100644 --- a/src/postcss_modules.rs +++ b/src/postcss_modules.rs @@ -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) { +pub fn local_by_default(input: &str, options: LocalByDefaultOptions) -> (String, Vec) { let mut result = String::new(); let mut warnings = Vec::new(); let mut index = 0; diff --git a/tests/postcss_modules/local_by_default_test.rs b/tests/postcss_modules/local_by_default_test.rs index 2d02c5f..e316f86 100644 --- a/tests/postcss_modules/local_by_default_test.rs +++ b/tests/postcss_modules/local_by_default_test.rs @@ -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; @@ -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]); @@ -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!( @@ -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] @@ -557,7 +570,7 @@ fn use_correct_spacing() { :local(.a) .b {} :local(.a) .b {} "#}, - Options { mode: Mode::Global }, + LocalByDefaultOptions { mode: Mode::Global }, ) } @@ -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 }, ); } @@ -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 }, ); } @@ -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", ); } @@ -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", ); } @@ -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", ); } @@ -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", ); } @@ -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 }, ); } @@ -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 }, ); } @@ -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", ); } @@ -1193,7 +1206,7 @@ fn css_nesting() { } } "#}, - Options { mode: Mode::Pure }, + LocalByDefaultOptions { mode: Mode::Pure }, ); } @@ -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 }, ); } @@ -1376,7 +1389,7 @@ fn at_scope_at_rule() { } } "#}, - Options { mode: Mode::Pure }, + LocalByDefaultOptions { mode: Mode::Pure }, ); test( indoc! {r#" @@ -1437,6 +1450,6 @@ fn at_scope_at_rule() { } } "#}, - Options { mode: Mode::Pure }, + LocalByDefaultOptions { mode: Mode::Pure }, ); }