simplify interpolated ident parsing by changing if stmt to match

This commit is contained in:
Connor Skees 2020-07-12 13:18:31 -04:00
parent c2384595e8
commit 20fc9e8e6b
2 changed files with 11 additions and 21 deletions

View File

@ -240,8 +240,6 @@ fn index(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
let list = args.get_err(0, "list")?.as_list();
let value = args.get_err(1, "value")?;
// TODO: find a way to propagate any errors here
// Potential input to fuzz: index(1px 1in 1cm, 96px + 1rem)
let index = match list.into_iter().position(|v| v == value) {
Some(v) => Number::from(v + 1),
None => return Ok(Value::Null),

View File

@ -161,31 +161,23 @@ impl<'a> Parser<'a> {
None => return Err(("Expected identifier.", self.span_before).into()),
};
if is_name_start(first) {
text.push(self.toks.next().unwrap().kind);
} else if first == '\\' {
self.toks.next();
text.push_str(&self.escape(true)?);
// TODO: peekmore
// (first == '#' && scanner.peekChar(1) == $lbrace)
} else if first == '#' {
self.toks.next();
let Token { kind, pos } = if let Some(tok) = self.toks.peek() {
*tok
} else {
return Err(("Expected identifier.", pos).into());
};
if kind == '{' {
match first {
c if is_name_start(c) => {
text.push(self.toks.next().unwrap().kind);
}
'\\' => {
self.toks.next();
text.push_str(&self.escape(true)?);
}
'#' if matches!(self.toks.peek_forward(1), Some(Token { kind: '{', .. })) => {
self.toks.next();
self.toks.next();
match self.parse_interpolation()?.node {
Value::String(ref s, ..) => text.push_str(s),
v => text.push_str(v.to_css_string(self.span_before)?.borrow()),
}
} else {
return Err(("Expected identifier.", pos).into());
}
} else {
return Err(("Expected identifier.", pos).into());
_ => return Err(("Expected identifier.", pos).into()),
}
self.interpolated_ident_body(&mut text)?;