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

View File

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

View File

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

View File

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

View File

@ -2,17 +2,27 @@
mod macros; mod macros;
test!( test!(
utf8_input, charset_exists_when_output_not_ascii,
"a {\n color: 🦆;\n}\n", "a {\n color: 🦆;\n}\n",
"@charset \"UTF-8\";\na {\n color: 🦆;\n}\n" "@charset \"UTF-8\";\na {\n color: 🦆;\n}\n"
); );
test!( test!(
ascii_charset_utf8, charset_utf8_is_removed_when_ascii,
"@charset \"UTF-8\";\na {\n color: red;\n}\n", "@charset \"UTF-8\";\na {\n color: red;\n}\n",
"a {\n color: red;\n}\n" "a {\n color: red;\n}\n"
); );
test!( test!(
unknown_charset, unknown_charset_is_removed,
"@charset \"foo\";\na {\n color: red;\n}\n", "@charset \"foo\";\na {\n color: red;\n}\n",
"a {\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."
);