emit proper error for missing quotes on @ import

This commit is contained in:
ConnorSkees 2020-04-24 20:00:22 -04:00
parent ecf36cd6d3
commit 2fb64934d7
2 changed files with 31 additions and 11 deletions

View File

@ -105,8 +105,8 @@ use crate::style::Style;
pub(crate) use crate::token::Token;
use crate::utils::{
devour_whitespace, eat_comment, eat_ident, eat_ident_no_interpolation, eat_variable_value,
parse_quoted_string, read_until_closing_curly_brace, read_until_newline, VariableDecl,
read_until_closing_paren
parse_quoted_string, read_until_closing_curly_brace, read_until_closing_paren,
read_until_newline, VariableDecl,
};
use crate::value::Value;
@ -358,12 +358,11 @@ impl<'a> StyleSheetParser<'a> {
AtRuleKind::Import => {
devour_whitespace(&mut self.lexer);
let mut file_name = String::new();
match self
.lexer
.next()
.unwrap()
.kind
{
let next = match self.lexer.next() {
Some(v) => v,
None => todo!("expected input after @import")
};
match next.kind {
q @ '"' | q @ '\'' => {
file_name.push_str(
&parse_quoted_string(
@ -373,12 +372,16 @@ impl<'a> StyleSheetParser<'a> {
&Selector::new())?
.node.unquote().to_css_string(span)?);
}
_ => todo!("expected ' or \" after @import"),
_ => return Err(("Expected string.", next.pos()).into()),
}
if self.lexer.next().unwrap().kind != ';' {
todo!("no semicolon after @import");
if let Some(t) = self.lexer.peek() {
if t.kind == ';' {
self.lexer.next();
}
}
devour_whitespace(&mut self.lexer);
let (new_rules, new_scope) = import(file_name)?;
rules.extend(new_rules);
GLOBAL_SCOPE.with(|s| {

View File

@ -41,6 +41,23 @@ fn import_no_semicolon() {
);
}
#[test]
fn import_no_quotes() {
let input = "@import import_no_quotes";
tempfile!("import_no_quotes", "$a: red;");
match grass::StyleSheet::new(input.to_string()) {
Ok(..) => panic!("did not fail"),
Err(e) => assert_eq!(
"Error: Expected string.",
e.to_string()
.chars()
.take_while(|c| *c != '\n')
.collect::<String>()
.as_str()
),
}
}
#[test]
fn single_quotes_import() {
let input = "@import 'single_quotes_import';\na {\n color: $a;\n}";