remove panic from unclosed call args

This commit is contained in:
ConnorSkees 2020-05-24 10:16:47 -04:00
parent 737a6ba90d
commit f52d784756
5 changed files with 17 additions and 8 deletions

View File

@ -324,13 +324,13 @@ pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
pub(crate) fn eat_call_args<I: Iterator<Item = Token>>( pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>, toks: &mut PeekMoreIterator<I>,
span_before: Span,
) -> SassResult<CallArgs> { ) -> SassResult<CallArgs> {
let mut args: HashMap<CallArg, Vec<Token>> = HashMap::new(); let mut args: HashMap<CallArg, Vec<Token>> = HashMap::new();
devour_whitespace_or_comment(toks)?; devour_whitespace_or_comment(toks)?;
let mut name = String::new(); let mut name = String::new();
let mut val: Vec<Token> = Vec::new(); let mut val: Vec<Token> = Vec::new();
// todo: panics on a { color:rgb(; } let mut span = toks.peek().ok_or(("expected \")\".", span_before))?.pos();
let mut span = toks.peek().unwrap().pos();
loop { loop {
match toks.peek().unwrap().kind { match toks.peek().unwrap().kind {
'$' => { '$' => {

View File

@ -200,7 +200,7 @@ pub(crate) fn eat_include<I: Iterator<Item = Token>>(
match tok.kind { match tok.kind {
';' => CallArgs::new(name.span), ';' => CallArgs::new(name.span),
'(' => { '(' => {
let tmp = eat_call_args(toks)?; let tmp = eat_call_args(toks, tok.pos)?;
devour_whitespace_or_comment(toks)?; devour_whitespace_or_comment(toks)?;
if let Some(tok) = toks.peek() { if let Some(tok) = toks.peek() {
match tok.kind { match tok.kind {

View File

@ -572,7 +572,8 @@ impl Value {
}); });
} }
if let Some(Token { kind: '(', .. }) = toks.peek() { if let Some(Token { kind: '(', pos }) = toks.peek() {
let pos = *pos;
let as_ident = Identifier::from(&s); let as_ident = Identifier::from(&s);
toks.next(); toks.next();
let func = match scope.get_fn(Spanned { let func = match scope.get_fn(Spanned {
@ -583,7 +584,7 @@ impl Value {
Err(_) => match GLOBAL_FUNCTIONS.get(as_ident.into_inner().as_str()) { Err(_) => match GLOBAL_FUNCTIONS.get(as_ident.into_inner().as_str()) {
Some(f) => { Some(f) => {
return Ok(IntermediateValue::Value(f.0( return Ok(IntermediateValue::Value(f.0(
eat_call_args(toks)?, eat_call_args(toks, pos)?,
scope, scope,
super_selector, super_selector,
)?) )?)
@ -600,11 +601,11 @@ impl Value {
"url" => match try_eat_url(toks, scope, super_selector)? { "url" => match try_eat_url(toks, scope, super_selector)? {
Some(val) => s = val, Some(val) => s = val,
None => s.push_str( None => s.push_str(
&eat_call_args(toks)?.to_css_string(scope, super_selector)?, &eat_call_args(toks, pos)?.to_css_string(scope, super_selector)?,
), ),
}, },
_ => s.push_str( _ => s.push_str(
&eat_call_args(toks)?.to_css_string(scope, super_selector)?, &eat_call_args(toks, pos)?.to_css_string(scope, super_selector)?,
), ),
} }
return Ok( return Ok(
@ -614,7 +615,7 @@ impl Value {
}, },
}; };
return Ok(IntermediateValue::Value(func.eval( return Ok(IntermediateValue::Value(func.eval(
eat_call_args(toks)?, eat_call_args(toks, pos)?,
scope, scope,
super_selector, super_selector,
)?) )?)

View File

@ -45,3 +45,10 @@ test!(
"@function foo($a, $b: $a) {\n @return $b;\n}\n\na {\n color: foo(2);\n}\n", "@function foo($a, $b: $a) {\n @return $b;\n}\n\na {\n color: foo(2);\n}\n",
"a {\n color: 2;\n}\n" "a {\n color: 2;\n}\n"
); );
// todo: this should have a space after :
// and should be "expected \")\"."
error!(
nothing_after_open,
"a { color:rgb(; }",
"Error: expected \"{\"."
);

View File

@ -128,3 +128,4 @@ error!(
); );
error!(unclosed_dbl_quote, "@if true \" {}", "Error: Expected \"."); error!(unclosed_dbl_quote, "@if true \" {}", "Error: Expected \".");
error!(unclosed_sgl_quote, "@if true ' {}", "Error: Expected '."); error!(unclosed_sgl_quote, "@if true ' {}", "Error: Expected '.");
error!(unclosed_call_args, "@if a({}", "Error: expected \")\".");