From 4513433fb113afb151b06ce161063fc0479a8d1b Mon Sep 17 00:00:00 2001 From: Mario Carbajal Date: Tue, 21 May 2024 16:32:51 -0300 Subject: [PATCH] Accept sass interpolation syntax in most places. --- internal/stylance-core/src/parse.rs | 37 ++++++++++++++++++++++++++++- stylance/tests/style.module.scss | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/internal/stylance-core/src/parse.rs b/internal/stylance-core/src/parse.rs index 5896fd6..b8a2ddb 100644 --- a/internal/stylance-core/src/parse.rs +++ b/internal/stylance-core/src/parse.rs @@ -58,6 +58,16 @@ fn block_comment<'s>(input: &mut &'s str) -> PResult<&'s str> { .parse_next(input) } +// matches a sass interpolation of the form #{...} +fn sass_interpolation<'s>(input: &mut &'s str) -> PResult<&'s str> { + ( + "#{", + cut_err(terminated(take_till(1.., ('{', '}', '\n')), '}')), + ) + .recognize() + .parse_next(input) +} + fn identifier<'s>(input: &mut &'s str) -> PResult<&'s str> { ( one_of(('_', '-', AsChar::is_alpha)), @@ -114,8 +124,10 @@ pub fn stuff_till<'s>( string.void(), block_comment.void(), line_comment.void(), + sass_interpolation.void(), '/'.void(), - take_till(1.., ('\'', '"', '/', list)).void(), + '#'.void(), + take_till(1.., ('\'', '"', '/', '#', list)).void(), )), ) } @@ -462,4 +474,27 @@ mod tests { println!("{input}"); assert!(input.is_empty()); } + + #[test] + fn test_sass_interpolation() { + let mut input = "#{$test-test}END"; + + let r = sass_interpolation.parse_next(&mut input); + assert_eq!(r, Ok("#{$test-test}")); + + assert_eq!(input, "END"); + + let mut input = "#{$test-test + }END"; + let r = sass_interpolation.parse_next(&mut input); + assert!(r.is_err()); + + let mut input = "#{$test-test"; + let r = sass_interpolation.parse_next(&mut input); + assert!(r.is_err()); + + let mut input = "#{$test-te{st}"; + let r = sass_interpolation.parse_next(&mut input); + assert!(r.is_err()); + } } diff --git a/stylance/tests/style.module.scss b/stylance/tests/style.module.scss index 840ac28..d30e00d 100644 --- a/stylance/tests/style.module.scss +++ b/stylance/tests/style.module.scss @@ -79,7 +79,7 @@ $some-scss-variable: 10px; // Scss variable declarations in top scope. @layer; -@container (width > 400px) { +@container (min-width: #{$screen-md}) { h2 { font-size: 1.5em; }