improve error message for selector-parse and deny open curly brace

This commit is contained in:
Connor Skees 2020-08-11 02:00:10 -04:00
parent 0c7e2017d5
commit 5133d580de
3 changed files with 21 additions and 8 deletions

View File

@ -55,7 +55,8 @@ pub(crate) fn selector_parse(mut args: CallArgs, parser: &mut Parser<'_>) -> Sas
args.max_args(1)?; args.max_args(1)?;
Ok(args Ok(args
.get_err(0, "selector")? .get_err(0, "selector")?
.to_selector(parser, "selector", false)? .to_selector(parser, "selector", false)
.map_err(|_| ("$selector: expected selector.", args.span()))?
.into_value()) .into_value())
} }

View File

@ -398,9 +398,9 @@ impl<'a> Parser<'a> {
let mut optional = false; let mut optional = false;
// we resolve interpolation and strip comments // we resolve interpolation and strip comments
while let Some(tok) = self.toks.next() { while let Some(Token { kind, pos }) = self.toks.next() {
span = span.merge(tok.pos()); span = span.merge(pos);
match tok.kind { match kind {
'#' => { '#' => {
if let Some(Token { kind: '{', .. }) = self.toks.peek().cloned() { if let Some(Token { kind: '{', .. }) = self.toks.peek().cloned() {
self.toks.next(); self.toks.next();
@ -411,14 +411,18 @@ impl<'a> Parser<'a> {
} }
'/' => { '/' => {
if self.toks.peek().is_none() { if self.toks.peek().is_none() {
return Err(("Expected selector.", tok.pos()).into()); return Err(("Expected selector.", pos).into());
} }
self.parse_comment()?; self.parse_comment()?;
string.push(' '); string.push(' ');
} }
'{' => { '{' => {
found_curly = true; if from_fn {
break; return Err(("Expected selector.", pos).into());
} else {
found_curly = true;
break;
}
} }
'\\' => { '\\' => {
string.push('\\'); string.push('\\');
@ -431,7 +435,7 @@ impl<'a> Parser<'a> {
self.expect_identifier("optional")?; self.expect_identifier("optional")?;
optional = true; optional = true;
} else { } else {
return Err(("expected \"{\".", tok.pos).into()); return Err(("expected \"{\".", pos).into());
} }
} }
c => string.push(c), c => string.push(c),

View File

@ -98,3 +98,11 @@ test!(
"a {\n color: selector-parse(\"b c, d e, f g\");\n}\n", "a {\n color: selector-parse(\"b c, d e, f g\");\n}\n",
"a {\n color: 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."
);