oxc_codegen/
options.rs

1use std::path::PathBuf;
2
3/// Legal comment
4///
5/// <https://esbuild.github.io/api/#legal-comments>
6#[derive(Debug, Clone, Eq, PartialEq, Default)]
7pub enum LegalComment {
8    /// Do not preserve any legal comments (default).
9    #[default]
10    None,
11    /// Preserve all legal comments.
12    Inline,
13    /// Move all legal comments to the end of the file.
14    Eof,
15    /// Return all legal comments and link then to them with a comment to the provided string.
16    Linked(String),
17    /// Move all legal comments to a .LEGAL.txt file but to not link to them.
18    External,
19}
20
21impl LegalComment {
22    /// Is None.
23    pub fn is_none(&self) -> bool {
24        *self == Self::None
25    }
26
27    /// Is inline mode.
28    pub fn is_inline(&self) -> bool {
29        *self == Self::Inline
30    }
31
32    /// Is EOF mode.
33    pub fn is_eof(&self) -> bool {
34        *self == Self::Eof
35    }
36}
37
38/// Codegen Options.
39#[derive(Debug, Clone)]
40pub struct CodegenOptions {
41    /// Use single quotes instead of double quotes.
42    ///
43    /// Default is `false`.
44    pub single_quote: bool,
45
46    /// Remove whitespace.
47    ///
48    /// Default is `false`.
49    pub minify: bool,
50
51    /// Print all comments?
52    ///
53    /// Default is `true`.
54    pub comments: bool,
55
56    /// Print annotation comments, e.g. `/* #__PURE__ */` and `/* #__NO_SIDE_EFFECTS__ */`.
57    ///
58    /// Only takes into effect when `comments` is false.
59    ///
60    /// Default is `false`.
61    pub annotation_comments: bool,
62
63    /// Print legal comments.
64    ///
65    /// Only takes into effect when `comments` is false.
66    ///
67    /// <https://esbuild.github.io/api/#legal-comments>
68    ///
69    /// Default is [LegalComment::None].
70    pub legal_comments: LegalComment,
71
72    /// Override the source map path. This affects the `sourceMappingURL`
73    /// comment at the end of the generated code.
74    ///
75    /// By default, the source map path is the same as the input source code
76    /// (with a `.map` extension).
77    pub source_map_path: Option<PathBuf>,
78}
79
80impl Default for CodegenOptions {
81    fn default() -> Self {
82        Self {
83            single_quote: false,
84            minify: false,
85            comments: true,
86            annotation_comments: false,
87            legal_comments: LegalComment::default(),
88            source_map_path: None,
89        }
90    }
91}
92
93impl CodegenOptions {
94    pub(crate) fn print_comments(&self) -> bool {
95        !self.minify && (self.comments || self.legal_comments.is_inline())
96    }
97
98    pub(crate) fn print_annotation_comments(&self) -> bool {
99        !self.minify && (self.comments || self.annotation_comments)
100    }
101}