From 5133d580de10c294084377224fa2b375bb6737da Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Tue, 11 Aug 2020 02:00:10 -0400 Subject: [PATCH] improve error message for `selector-parse` and deny open curly brace --- src/builtin/functions/selector.rs | 3 ++- src/parse/mod.rs | 18 +++++++++++------- tests/selector-parse.rs | 8 ++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/builtin/functions/selector.rs b/src/builtin/functions/selector.rs index f7d50e9..dae7147 100644 --- a/src/builtin/functions/selector.rs +++ b/src/builtin/functions/selector.rs @@ -55,7 +55,8 @@ pub(crate) fn selector_parse(mut args: CallArgs, parser: &mut Parser<'_>) -> Sas args.max_args(1)?; Ok(args .get_err(0, "selector")? - .to_selector(parser, "selector", false)? + .to_selector(parser, "selector", false) + .map_err(|_| ("$selector: expected selector.", args.span()))? .into_value()) } diff --git a/src/parse/mod.rs b/src/parse/mod.rs index fe12241..f24252a 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -398,9 +398,9 @@ impl<'a> Parser<'a> { let mut optional = false; // we resolve interpolation and strip comments - while let Some(tok) = self.toks.next() { - span = span.merge(tok.pos()); - match tok.kind { + while let Some(Token { kind, pos }) = self.toks.next() { + span = span.merge(pos); + match kind { '#' => { if let Some(Token { kind: '{', .. }) = self.toks.peek().cloned() { self.toks.next(); @@ -411,14 +411,18 @@ impl<'a> Parser<'a> { } '/' => { if self.toks.peek().is_none() { - return Err(("Expected selector.", tok.pos()).into()); + return Err(("Expected selector.", pos).into()); } self.parse_comment()?; string.push(' '); } '{' => { - found_curly = true; - break; + if from_fn { + return Err(("Expected selector.", pos).into()); + } else { + found_curly = true; + break; + } } '\\' => { string.push('\\'); @@ -431,7 +435,7 @@ impl<'a> Parser<'a> { self.expect_identifier("optional")?; optional = true; } else { - return Err(("expected \"{\".", tok.pos).into()); + return Err(("expected \"{\".", pos).into()); } } c => string.push(c), diff --git a/tests/selector-parse.rs b/tests/selector-parse.rs index 12a0568..3984526 100644 --- a/tests/selector-parse.rs +++ b/tests/selector-parse.rs @@ -98,3 +98,11 @@ test!( "a {\n color: selector-parse(\"b c, d e, f g\");\n}\n", "a {\n color: b c, d e, f g;\n}\n" ); +error!( + invalid_selector, + "a {\n color: selector-parse(\"!!!!!!!!\");\n}\n", "Error: $selector: expected selector." +); +error!( + selector_contains_curly_brace, + "a {\n color: selector-parse(\"a {\");\n}\n", "Error: $selector: expected selector." +);