simplify parsing of @function and throw error on invalid @charset

This commit is contained in:
Connor Skees 2021-07-20 20:41:21 -04:00
parent 584b42f00f
commit 7979158465
5 changed files with 31 additions and 18 deletions

View File

@ -7,9 +7,8 @@ use crate::{
error::SassResult,
lexer::Lexer,
scope::Scopes,
utils::{read_until_closing_curly_brace, read_until_semicolon_or_closing_curly_brace},
utils::{read_until_closing_curly_brace},
value::{SassFunction, Value},
Token,
};
use super::{common::ContextFlags, Parser, Stmt};
@ -72,11 +71,10 @@ impl<'a> Parser<'a> {
}
pub(super) fn parse_return(&mut self) -> SassResult<Box<Value>> {
let toks = read_until_semicolon_or_closing_curly_brace(self.toks)?;
let v = self.parse_value_from_vec(toks, true)?;
if let Some(Token { kind: ';', .. }) = self.toks.peek() {
self.toks.next();
}
let v = self.parse_value(true, &|_| false)?;
self.consume_char_if_exists(';');
Ok(Box::new(v.node))
}

View File

@ -131,10 +131,7 @@ impl<'a> Parser<'a> {
None
};
// todo: self.consume_if_exists
if let Some(Token { kind: ';', .. }) = self.toks.peek() {
self.toks.next();
}
self.consume_char_if_exists(';');
let UserDefinedMixin {
body,

View File

@ -245,10 +245,14 @@ impl<'a> Parser<'a> {
.into());
}
read_until_semicolon_or_closing_curly_brace(self.toks)?;
if let Some(Token { kind: ';', .. }) = self.toks.peek() {
self.toks.next();
let val = self.parse_value(false, &|_| false)?;
self.consume_char_if_exists(';');
if !val.node.is_quoted_string() {
return Err(("Expected string.", val.span).into());
}
continue;
}
AtRuleKind::Media => stmts.push(self.parse_media()?),

View File

@ -572,4 +572,8 @@ impl Value {
_ => return Ok(None),
}))
}
pub fn is_quoted_string(&self) -> bool {
matches!(self, Value::String(_, QuoteKind::Quoted))
}
}

View File

@ -2,17 +2,27 @@
mod macros;
test!(
utf8_input,
charset_exists_when_output_not_ascii,
"a {\n color: 🦆;\n}\n",
"@charset \"UTF-8\";\na {\n color: 🦆;\n}\n"
);
test!(
ascii_charset_utf8,
charset_utf8_is_removed_when_ascii,
"@charset \"UTF-8\";\na {\n color: red;\n}\n",
"a {\n color: red;\n}\n"
);
test!(
unknown_charset,
unknown_charset_is_removed,
"@charset \"foo\";\na {\n color: red;\n}\n",
"a {\n color: red;\n}\n"
);
error!(
invalid_charset_value,
"@charset 1;",
"Error: Expected string."
);
error!(
invalid_charset_value_unquoted_string,
"@charset a;",
"Error: Expected string."
);