From b42ae6143553c71521924bf6c39a74a47a00f5c7 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Wed, 8 Jul 2020 09:08:25 -0400 Subject: [PATCH] test for crazy interpolation inside `@if` --- src/parse/mod.rs | 3 +-- src/parse/throw_away.rs | 12 ++++++++---- tests/if.rs | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 7a21f2a..2c65919 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -545,9 +545,8 @@ impl<'a> Parser<'a> { self.throw_away_until_closing_curly_brace()?; } - self.whitespace_or_comment(); - loop { + self.whitespace_or_comment(); if let Some(Token { kind: '@', pos }) = self.toks.peek().cloned() { self.toks.peek_forward(1); let ident = peek_ident_no_interpolation(self.toks, false, pos)?; diff --git a/src/parse/throw_away.rs b/src/parse/throw_away.rs index 1ef0ae3..b3ee626 100644 --- a/src/parse/throw_away.rs +++ b/src/parse/throw_away.rs @@ -27,10 +27,14 @@ impl<'a> Parser<'a> { return Err((format!("Expected {}.", q), tok.pos).into()); } } - '#' => { - self.toks.next(); - self.throw_away_until_closing_curly_brace()?; - } + '#' => match self.toks.peek() { + Some(Token { kind: '{', .. }) => { + self.toks.next(); + self.throw_away_until_closing_curly_brace()?; + } + Some(..) => {} + None => return Err(("expected \"{\".", self.span_before).into()), + }, _ => {} } } diff --git a/tests/if.rs b/tests/if.rs index 55d2a9f..008021d 100644 --- a/tests/if.rs +++ b/tests/if.rs @@ -140,6 +140,29 @@ test!( "@mixin foo {\n color: red;\n}\n\n@if true {\n a {\n @include foo;\n }\n}\n", "a {\n color: red;\n}\n" ); +test!( + crazy_interpolation, + "a { + @if true { + a: #{\"#{\"\\\\}}}{{{\"}#\"}; + } + + @if false { + a: #{\"#{\"\\\\}}}{{{\"}#\"}; + } @else if true { + b: #{\"#{\"\\\\}}}{{{\"}#\"}; + } + + @if false { + a: #{\"#{\"\\\\}}}{{{\"}#\"}; + } @else if false { + b: #{\"#{\"\\\\}}}{{{\"}#\"}; + } @else { + c: #{\"#{\"\\\\}}}{{{\"}#\"}; + } + }", + "a {\n a: \\}}}{{{#;\n b: \\}}}{{{#;\n c: \\}}}{{{#;\n}\n" +); error!( nothing_after_escape, "@if \\", "Error: Expected expression."