Skip to main content

oxc_css_parser/parser/
postcss_simple_vars.rs

1use super::Parser;
2use crate::{
3    Parse,
4    ast::*,
5    config::Syntax,
6    error::PResult,
7    expect, peek,
8    pos::{Span, Spanned},
9    tokenizer::Token,
10};
11
12impl<'a> Parse<'a> for PostcssSimpleVar<'a> {
13    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
14        debug_assert!(input.syntax == Syntax::Css && input.options.allow_postcss_simple_vars);
15
16        let (name, span) = input.parse_dollar_var_ident()?;
17        Ok(PostcssSimpleVar { name, span })
18    }
19}
20
21impl<'a> Parse<'a> for PostcssSimpleVarDeclaration<'a> {
22    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
23        debug_assert!(input.syntax == Syntax::Css && input.options.allow_postcss_simple_vars);
24
25        let name = input.parse::<PostcssSimpleVar>()?;
26        let (_, colon_span) = expect!(input, Colon);
27        let mut value = input.parse_declaration_value()?;
28        // postcss-simple-vars is textual substitution; `!important` is just part
29        // of the value, not a structural declaration modifier (unlike CSS's
30        // `Declaration.important`). Keep it in the value stream.
31        if let Token::Exclamation(..) = &peek!(input).token {
32            let important = input.parse::<ImportantAnnotation>()?;
33            value.push(ComponentValue::ImportantAnnotation(important));
34        }
35
36        let end = value.last().map(|v| v.span().end).unwrap_or(colon_span.end);
37        let span = Span { start: name.span.start, end };
38
39        Ok(PostcssSimpleVarDeclaration { name, colon_span, value, span })
40    }
41}