From ac3dc8cb6b1630c55b28bc2a22c24b7ba87187a2 Mon Sep 17 00:00:00 2001 From: Mario Carbajal Date: Thu, 25 Jan 2024 17:21:32 -0300 Subject: [PATCH] Support @layer at rules --- internal/stylance-core/src/parse.rs | 55 +++++++++++++++++++++++++--- stylance/tests/style.module.scss | 14 ++++++- stylance/tests/test_import_styles.rs | 4 ++ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/internal/stylance-core/src/parse.rs b/internal/stylance-core/src/parse.rs index 08253b0..e109e6e 100644 --- a/internal/stylance-core/src/parse.rs +++ b/internal/stylance-core/src/parse.rs @@ -204,12 +204,19 @@ fn at_rule<'s>(input: &mut &'s str) -> PResult>> { return Ok(vec![]); } - if identifier == "media" { - cut_err(terminated(style_rule_block_contents, '}')).parse_next(input) - } else { - cut_err(terminated(unknown_block_contents, '}')).parse_next(input)?; - Ok(vec![]) + match identifier { + "media" | "layer" => cut_err(terminated(style_rule_block_contents, '}')).parse_next(input), + _ => { + cut_err(terminated(unknown_block_contents, '}')).parse_next(input)?; + Ok(vec![]) + } } + // if identifier == "media" { + // cut_err(terminated(style_rule_block_contents, '}')).parse_next(input) + // } else { + // cut_err(terminated(unknown_block_contents, '}')).parse_next(input)?; + // Ok(vec![]) + // } } fn unknown_block_contents<'s>(input: &mut &'s str) -> PResult<&'s str> { @@ -359,6 +366,35 @@ mod tests { assert!(input.is_empty()); } + #[test] + fn test_at_rule_layer() { + let mut input = "@layer test { + .foo { + background-color: red; + } + + .bar { + color: blue; + + .baz { + color: green; + } + } + }"; + + let r = at_rule.parse_next(&mut input); + assert_eq!( + r, + Ok(vec![ + CssFragment::Class("foo"), + CssFragment::Class("bar"), + CssFragment::Class("baz") + ]) + ); + + assert!(input.is_empty()); + } + #[test] fn test_top_level() { let mut input = "// tool.module.scss @@ -376,6 +412,14 @@ mod tests { } } + @layer { + .foo { + color: blue; + } + } + + @layer foo; + @debug 1+2 * 3==1+(2 * 3); // true .container { @@ -401,6 +445,7 @@ mod tests { Ok(vec![ CssFragment::Class("default_border"), CssFragment::Class("foo"), + CssFragment::Class("foo"), CssFragment::Class("container"), CssFragment::Class("bar"), ]) diff --git a/stylance/tests/style.module.scss b/stylance/tests/style.module.scss index e005048..74217c6 100644 --- a/stylance/tests/style.module.scss +++ b/stylance/tests/style.module.scss @@ -67,4 +67,16 @@ $some-scss-variable: 10px; // Scss variable declarations in top scope. // scss placeholder class should parse %scss-class { color: red; -} \ No newline at end of file +} + +@layer test-layer { + .style8 { + color: red; + } +} + +@layer test-layer2; + +@layer; + +//eof \ No newline at end of file diff --git a/stylance/tests/test_import_styles.rs b/stylance/tests/test_import_styles.rs index 369e593..a297d66 100644 --- a/stylance/tests/test_import_styles.rs +++ b/stylance/tests/test_import_styles.rs @@ -10,6 +10,8 @@ fn test_import_crate_style() { assert_eq!(style::style4, "style4-a331da9"); assert_eq!(style::style5, "style5-a331da9"); assert_eq!(style::style6, "style6-a331da9"); + assert_eq!(style::style7, "style7-a331da9"); + assert_eq!(style::style8, "style8-a331da9"); assert_eq!(style::style_with_dashes, "style-with-dashes-a331da9"); assert_eq!(style::nested_style, "nested-style-a331da9"); @@ -36,6 +38,8 @@ fn test_import_style() { assert_eq!(style::style4, "style4-a331da9"); assert_eq!(style::style5, "style5-a331da9"); assert_eq!(style::style6, "style6-a331da9"); + assert_eq!(style::style7, "style7-a331da9"); + assert_eq!(style::style8, "style8-a331da9"); assert_eq!(style::style_with_dashes, "style-with-dashes-a331da9"); assert_eq!(style::nested_style, "nested-style-a331da9");