Skip to main content

oxc_codegen/
options.rs

1use std::path::PathBuf;
2
3use oxc_data_structures::code_buffer::{DEFAULT_INDENT_WIDTH, IndentChar};
4
5/// Codegen Options.
6#[derive(Debug, Clone)]
7pub struct CodegenOptions {
8    /// Use single quotes instead of double quotes.
9    ///
10    /// Default is `false`.
11    pub single_quote: bool,
12
13    /// Remove whitespace.
14    ///
15    /// Default is `false`.
16    pub minify: bool,
17
18    /// Print comments?
19    ///
20    /// At present, only some leading comments are preserved.
21    ///
22    /// Default is [CommentOptions::default].
23    pub comments: CommentOptions,
24
25    /// Enable sourcemap.
26    ///
27    /// The provided path sets the `source` field in the returned sourcemap.
28    ///
29    /// Default is `None` - no sourcemap is produced.
30    pub source_map_path: Option<PathBuf>,
31
32    /// Indentation character.
33    ///
34    /// Default is [`IndentChar::Tab`].
35    pub indent_char: IndentChar,
36
37    /// Number of characters per indentation level.
38    ///
39    /// Default is `1`.
40    pub indent_width: usize,
41
42    /// Initial indentation level for generated code.
43    ///
44    /// Default is `0`.
45    pub initial_indent: u32,
46}
47
48impl Default for CodegenOptions {
49    fn default() -> Self {
50        Self {
51            single_quote: false,
52            minify: false,
53            comments: CommentOptions::default(),
54            source_map_path: None,
55            indent_char: IndentChar::default(),
56            indent_width: DEFAULT_INDENT_WIDTH,
57            initial_indent: 0,
58        }
59    }
60}
61
62impl CodegenOptions {
63    /// Minify whitespace and remove comments.
64    pub fn minify() -> Self {
65        Self {
66            single_quote: false,
67            minify: true,
68            comments: CommentOptions::disabled(),
69            source_map_path: None,
70            indent_char: IndentChar::default(),
71            indent_width: DEFAULT_INDENT_WIDTH,
72            initial_indent: 0,
73        }
74    }
75
76    #[inline]
77    pub(crate) fn print_normal_comment(&self) -> bool {
78        self.comments.normal
79    }
80
81    #[inline]
82    pub(crate) fn print_legal_comment(&self) -> bool {
83        self.comments.legal.is_inline()
84    }
85
86    #[inline]
87    pub(crate) fn print_jsdoc_comment(&self) -> bool {
88        self.comments.jsdoc
89    }
90
91    #[inline]
92    pub(crate) fn print_annotation_comment(&self) -> bool {
93        self.comments.annotation
94    }
95}
96
97#[derive(Debug, Clone, PartialEq, Eq)]
98/// Comment Options
99pub struct CommentOptions {
100    /// Print normal comments that do not have special meanings.
101    ///
102    /// At present only statement level comments are printed.
103    ///
104    /// Default is `true`.
105    pub normal: bool,
106
107    /// Print jsdoc comments.
108    ///
109    /// * jsdoc: `/** jsdoc */`
110    ///
111    /// Default is `true`.
112    pub jsdoc: bool,
113
114    /// Print annotation comments.
115    ///
116    /// * pure: `/* #__PURE__ */` and `/* #__NO_SIDE_EFFECTS__ */`
117    /// * webpack: `/* webpackChunkName */`
118    /// * vite: `/* @vite-ignore */`
119    /// * coverage: `v8 ignore`, `c8 ignore`, `node:coverage`, `istanbul ignore`
120    ///
121    /// Default is `true`.
122    pub annotation: bool,
123
124    /// Print legal comments.
125    ///
126    /// * starts with `//!` or `/*!`.
127    /// * contains `/* @license */` or `/* @preserve */`
128    ///
129    /// Default is [`LegalComment::Inline`].
130    pub legal: LegalComment,
131}
132
133impl Default for CommentOptions {
134    fn default() -> Self {
135        Self { normal: true, jsdoc: true, annotation: true, legal: LegalComment::default() }
136    }
137}
138
139impl CommentOptions {
140    /// Disable Comments.
141    pub fn disabled() -> Self {
142        Self { normal: false, jsdoc: false, annotation: false, legal: LegalComment::None }
143    }
144}
145
146/// Legal comment
147///
148/// <https://esbuild.github.io/api/#legal-comments>
149#[derive(Debug, Clone, Eq, PartialEq, Default)]
150pub enum LegalComment {
151    /// Do not preserve any legal comments.
152    None,
153    /// Preserve all legal comments (default).
154    #[default]
155    Inline,
156    /// Move all legal comments to the end of the file.
157    Eof,
158    /// Return all legal comments and link then to them with a comment to the provided string.
159    Linked(String),
160    /// Move all legal comments to a .LEGAL.txt file but to not link to them.
161    External,
162}
163
164impl LegalComment {
165    /// Is None.
166    pub fn is_none(&self) -> bool {
167        *self == Self::None
168    }
169
170    /// Is inline mode.
171    pub fn is_inline(&self) -> bool {
172        *self == Self::Inline
173    }
174
175    /// Is EOF mode.
176    pub fn is_eof(&self) -> bool {
177        *self == Self::Eof
178    }
179}