Skip to main content

oxc_css_parser/
config.rs

1#[cfg(feature = "config_serde")]
2use serde::{Deserialize, Serialize};
3
4/// Supported syntax.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6#[cfg_attr(feature = "config_serde", derive(Serialize, Deserialize))]
7#[cfg_attr(feature = "config_serde", serde(rename_all = "camelCase"))]
8pub enum Syntax {
9    #[default]
10    Css,
11    Scss,
12    /// Indented Sass Syntax
13    Sass,
14    Less,
15}
16
17/// Configuration for a backtick-delimited template placeholder.
18///
19/// A placeholder has the shape `` `<prefix><decimal index>` `` (e.g. with
20/// `prefix = "PLACEHOLDER-"`: `` `PLACEHOLDER-0` ``). The opening and closing
21/// backticks are fixed; only `prefix` is supplied by the downstream consumer, so
22/// oxc-css-parser stays agnostic to its content and only uses it to recognize and index
23/// the token.
24///
25/// Backtick is not valid CSS/SCSS/Sass syntax (it is only Less's inline-JS
26/// delimiter), so this is intended for SCSS parsing only; see
27/// [`ParserOptions::template_placeholder`].
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub struct TemplatePlaceholder {
30    /// Text after the opening backtick that marks the start of a placeholder,
31    /// before its decimal index (e.g. `"PLACEHOLDER-"`).
32    pub prefix: &'static str,
33}
34
35/// Parser options for customizing parser behaviors.
36#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
37#[cfg_attr(feature = "config_serde", derive(Serialize, Deserialize))]
38#[cfg_attr(feature = "config_serde", serde(rename_all = "camelCase"))]
39pub struct ParserOptions {
40    /// Enabling this will make parser attempt to parse
41    /// custom property value as normal declaration value instead of tokens.
42    /// It will fallback to parse as tokens if there're syntax errors
43    /// when parsing as values.
44    pub try_parsing_value_in_custom_property: bool,
45
46    /// If enabled, trailing semicolon of each statement will be treated
47    /// as recoverable errors, instead of raising a syntax error.
48    pub tolerate_semicolon_in_sass: bool,
49
50    /// If enabled, [`Syntax::Css`] accepts the `$variable` syntax handled by the
51    /// [`postcss-simple-vars`](https://github.com/postcss/postcss-simple-vars) plugin:
52    /// `$var: value;` declarations, `$var` references in property values,
53    /// and `$var` references inside `@media` (and similar) at-rule preludes.
54    ///
55    /// The resulting AST uses dedicated [`PostcssSimpleVar`](crate::ast::PostcssSimpleVar)
56    /// and [`PostcssSimpleVarDeclaration`](crate::ast::PostcssSimpleVarDeclaration)
57    /// nodes, separate from SCSS's [`SassVariable`](crate::ast::SassVariable) family.
58    ///
59    /// NOTE: Interpolation (`$(var)`), selector references (`.$prefix`),
60    /// and comment substitutions (`<<$(var)>>`) are not yet covered.
61    ///
62    /// Ignored for [`Syntax::Scss`], [`Syntax::Sass`], and [`Syntax::Less`]
63    /// (those dialects already accept `$variable` natively).
64    pub allow_postcss_simple_vars: bool,
65
66    /// If set, a backtick-delimited token of the shape `` `<prefix><decimal index>` ``
67    /// (see [`TemplatePlaceholder`]) is tokenized as an atomic
68    /// [`Token::Placeholder`](crate::token::Token) carrying the parsed index.
69    /// The token terminates at the closing backtick, so an immediately following
70    /// identifier (e.g. `` `PLACEHOLDER-0`px ``) is lexed as a separate suffix.
71    ///
72    /// This is designed for downstream formatters that substitute template
73    /// interpolations (e.g. CSS-in-JS `${expr}`) with such placeholders
74    /// before parsing, then re-substitute them in the output.
75    ///
76    /// Backtick is not valid CSS/SCSS/Sass syntax, so this MUST be used with
77    /// [`Syntax::Scss`] (the parser builder asserts this); in Less, backtick is
78    /// the inline-JS delimiter and would conflict.
79    ///
80    /// Not serialized: the affix is a `&'static str` supplied programmatically,
81    /// not loadable from a config file.
82    #[cfg_attr(feature = "config_serde", serde(skip))]
83    pub template_placeholder: Option<TemplatePlaceholder>,
84}