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, [`Syntax::Css`] accepts the `$variable` syntax handled by the
47    /// [`postcss-simple-vars`](https://github.com/postcss/postcss-simple-vars) plugin:
48    /// `$var: value;` declarations, `$var` references in property values,
49    /// and `$var` references inside `@media` (and similar) at-rule preludes.
50    ///
51    /// The resulting AST uses dedicated [`PostcssSimpleVar`](crate::ast::PostcssSimpleVar)
52    /// and [`PostcssSimpleVarDeclaration`](crate::ast::PostcssSimpleVarDeclaration)
53    /// nodes, separate from SCSS's [`SassVariable`](crate::ast::SassVariable) family.
54    ///
55    /// NOTE: Interpolation (`$(var)`), selector references (`.$prefix`),
56    /// and comment substitutions (`<<$(var)>>`) are not yet covered.
57    ///
58    /// Ignored for [`Syntax::Scss`], [`Syntax::Sass`], and [`Syntax::Less`]
59    /// (those dialects already accept `$variable` natively).
60    pub allow_postcss_simple_vars: bool,
61
62    /// If set, a backtick-delimited token of the shape `` `<prefix><decimal index>` ``
63    /// (see [`TemplatePlaceholder`]) is tokenized as an atomic
64    /// [`Token::Placeholder`](crate::token::Token) carrying the parsed index.
65    /// The token terminates at the closing backtick, so an immediately following
66    /// identifier (e.g. `` `PLACEHOLDER-0`px ``) is lexed as a separate suffix.
67    ///
68    /// This is designed for downstream formatters that substitute template
69    /// interpolations (e.g. CSS-in-JS `${expr}`) with such placeholders
70    /// before parsing, then re-substitute them in the output.
71    ///
72    /// Backtick is not valid CSS/SCSS/Sass syntax, so this MUST be used with
73    /// [`Syntax::Scss`] (the parser builder asserts this); in Less, backtick is
74    /// the inline-JS delimiter and would conflict.
75    ///
76    /// Not serialized: the affix is a `&'static str` supplied programmatically,
77    /// not loadable from a config file.
78    #[cfg_attr(feature = "config_serde", serde(skip))]
79    pub template_placeholder: Option<TemplatePlaceholder>,
80}