Skip to content

Commit

Permalink
fix: local with at-rules bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed May 4, 2024
1 parent 86d011e commit 6bccdfc
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Lexes CSS modules returning their dependencies metadata.

- [x] CSS:
- [x] @import
- [x] url, image-set
- [x] url(), image-set()
- [ ] CSS Modules
- [x] :local, :local(), :global, :global()
- [x] local scope by default
Expand Down
34 changes: 13 additions & 21 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl CssModulesModeData {
self.current = CssModulesMode::Global;
}

pub fn set_none(&mut self) {
pub fn set_default(&mut self) {
self.current = CssModulesMode::None;
}
}
Expand Down Expand Up @@ -575,18 +575,13 @@ impl<'s, D: HandleDependency<'s>, W: HandleWarning<'s>> Visitor<'s> for LexDepen
return Some(());
}
self.scope = Scope::InAtImport(ImportData::new(start));
} else if name == "@media"
|| name == "@supports"
|| name == "@layer"
|| name == "@container"
{
self.is_next_rule_prelude = true;
} else if self.mode_data.is_some() && name == "@property" {
self.lex_local_property_decl(lexer)?;
} else if self.mode_data.is_some() && name == "@scope" {
self.is_next_rule_prelude = true;
} else if self.mode_data.is_some() {
self.is_next_rule_prelude = false;
}
// else if self.allow_mode_switch {
// self.is_next_rule_prelude = false;
// }
Some(())
}

Expand Down Expand Up @@ -728,7 +723,7 @@ impl<'s, D: HandleDependency<'s>, W: HandleWarning<'s>> Visitor<'s> for LexDepen
Some(last) if matches!(last.kind, BalancedItemKind::Global) => {
mode_data.set_global()
}
_ => mode_data.set_none(),
_ => mode_data.set_default(),
};
self.handle_dependency
.handle_dependency(Dependency::Replace {
Expand Down Expand Up @@ -820,17 +815,15 @@ impl<'s, D: HandleDependency<'s>, W: HandleWarning<'s>> Visitor<'s> for LexDepen
self.allow_import_at_rule = false;
self.scope = Scope::InBlock;
self.block_nesting_level = 1;
if self.mode_data.is_some() {
self.is_next_rule_prelude = self.is_next_nested_syntax(lexer)?;
}
}
Scope::InBlock => {
self.block_nesting_level += 1;
if self.mode_data.is_some() {
self.is_next_rule_prelude = self.is_next_nested_syntax(lexer)?;
}
}
_ => {}
_ => return Some(()),
}
if let Some(mode_data) = &mut self.mode_data {
mode_data.set_default();
self.is_next_rule_prelude = self.is_next_nested_syntax(lexer)?;
}
Some(())
}
Expand All @@ -840,9 +833,8 @@ impl<'s, D: HandleDependency<'s>, W: HandleWarning<'s>> Visitor<'s> for LexDepen
self.block_nesting_level -= 1;
if self.block_nesting_level == 0 {
self.scope = Scope::TopLevel;
if let Some(mode_data) = &mut self.mode_data {
if self.mode_data.is_some() {
self.is_next_rule_prelude = true;
mode_data.set_none();
}
} else if self.mode_data.is_some() {
self.is_next_rule_prelude = self.is_next_nested_syntax(lexer)?;
Expand Down Expand Up @@ -908,7 +900,7 @@ impl<'s, D: HandleDependency<'s>, W: HandleWarning<'s>> Visitor<'s> for LexDepen

fn comma(&mut self, _: &mut Lexer, _: Pos, _: Pos) -> Option<()> {
if let Some(mode_data) = &mut self.mode_data {
mode_data.set_none();
mode_data.set_default();
}
Some(())
}
Expand Down
81 changes: 72 additions & 9 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,25 +790,33 @@ fn css_modules_pseudo() {
#[test]
fn css_modules_nesting() {
let input = indoc! {r#"
.first-nested {
.first-nested-nested {
.nested {
.nested-nested {
color: red;
}
}
.first-nested-at-rule {
.nested-at-rule {
@media screen {
.first-nested-nested-at-rule-deep {
.nested-nested-at-rule-deep {
color: red;
}
}
}
:global .nested2 {
.nested2-nested {
color: red;
}
}
"#};
let (dependencies, warnings) = collect_css_modules_dependencies(input);
assert!(warnings.is_empty());
assert_local_ident_dependency(input, &dependencies[0], "first-nested");
assert_local_ident_dependency(input, &dependencies[1], "first-nested-nested");
assert_local_ident_dependency(input, &dependencies[2], "first-nested-at-rule");
assert_local_ident_dependency(input, &dependencies[3], "first-nested-nested-at-rule-deep");
assert_local_ident_dependency(input, &dependencies[0], "nested");
assert_local_ident_dependency(input, &dependencies[1], "nested-nested");
assert_local_ident_dependency(input, &dependencies[2], "nested-at-rule");
assert_local_ident_dependency(input, &dependencies[3], "nested-nested-at-rule-deep");
assert_replace_dependency(input, &dependencies[4], "", ":global ");
assert_local_ident_dependency(input, &dependencies[5], "nested2-nested");
assert_eq!(dependencies.len(), 6);
}

#[test]
Expand Down Expand Up @@ -841,7 +849,6 @@ fn css_modules_local_var() {
assert_local_var_decl_dependency(input, &dependencies[2], "local-color");
assert_local_ident_dependency(input, &dependencies[3], "globalVars");
assert_replace_dependency(input, &dependencies[4], "", ":global ");
dbg!(dependencies, warnings);
}

#[test]
Expand Down Expand Up @@ -883,6 +890,62 @@ fn css_modules_property() {
assert_local_var_dependency(input, &dependencies[2], "my-color");
}

#[test]
fn css_modules_at_rule_1() {
let input = indoc! {r#"
@layer framework.container {
.class {
color: red;
}
}
"#};
let (dependencies, warnings) = collect_css_modules_dependencies(input);
assert!(warnings.is_empty());
assert_local_ident_dependency(input, &dependencies[0], "class");
assert_eq!(dependencies.len(), 1);
}

#[test]
fn css_modules_at_rule_2() {
let input = indoc! {r#"
@page {
.class {
color: red;
}
}
@page :left, :top {
.class2 {
color: red;
}
}
"#};
let (dependencies, warnings) = collect_css_modules_dependencies(input);
assert!(warnings.is_empty());
assert_local_ident_dependency(input, &dependencies[0], "class");
assert_local_ident_dependency(input, &dependencies[1], "class2");
assert_eq!(dependencies.len(), 2);
}

#[test]
fn css_modules_at_rule_3() {
let input = indoc! {r#"
.article-body {
color: red;
}
@scope (.article-body) to (figure) {
.img {
background-color: goldenrod;
}
}
"#};
let (dependencies, warnings) = collect_css_modules_dependencies(input);
assert!(warnings.is_empty());
assert_local_ident_dependency(input, &dependencies[0], "article-body");
assert_local_ident_dependency(input, &dependencies[1], "article-body");
assert_local_ident_dependency(input, &dependencies[2], "img");
assert_eq!(dependencies.len(), 3);
}

#[test]
fn icss_export_unexpected() {
let input = ":export {\n/sl/ash;";
Expand Down

0 comments on commit 6bccdfc

Please sign in to comment.