1use super::lexer::LexerOptions;
2
3#[allow(unused)]
5#[derive(Copy, Clone)]
6#[allow(clippy::struct_excessive_bools)]
8#[cfg_attr(feature = "serde-serialize", derive(serde::Deserialize), serde(default))]
9#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
10pub struct ParseOptions {
11 pub jsx: bool,
13 pub type_annotations: bool,
15 pub type_definition_module: bool,
17 pub special_jsx_attributes: bool,
19 pub decorators: bool,
21 pub comments: Comments,
23 pub is_expressions: bool,
25 pub custom_function_headers: bool,
27 pub buffer_size: usize,
29 pub stack_size: Option<usize>,
31 pub record_keyword_positions: bool,
33 pub interpolation_points: bool,
35 pub destructuring_type_annotation: bool,
37 pub extra_operators: bool,
39 pub retain_blank_lines: bool,
41 pub partial_syntax: bool,
43 pub top_level_html: bool,
45}
46
47impl ParseOptions {
48 pub(crate) fn get_lex_options(&self) -> LexerOptions {
49 LexerOptions {
50 comments: self.comments,
51 lex_jsx: self.jsx,
52 allow_unsupported_characters_in_jsx_attribute_keys: self.special_jsx_attributes,
53 allow_expressions_in_jsx: true,
54 top_level_html: self.top_level_html,
56 }
57 }
58
59 #[must_use]
60 pub fn all_features() -> Self {
61 Self {
62 jsx: true,
63 type_annotations: true,
64 type_definition_module: false,
65 special_jsx_attributes: true,
66 comments: Comments::All,
67 decorators: true,
68 custom_function_headers: true,
69 is_expressions: true,
70 buffer_size: 100,
71 stack_size: None,
72 record_keyword_positions: true,
73 interpolation_points: false,
75 partial_syntax: true,
76 destructuring_type_annotation: true,
77 extra_operators: true,
78 retain_blank_lines: true,
79 top_level_html: false,
80 }
81 }
82}
83
84impl Default for ParseOptions {
86 fn default() -> Self {
87 Self {
88 jsx: true,
89 type_annotations: true,
90 type_definition_module: false,
91 special_jsx_attributes: false,
92 comments: Comments::All,
93 decorators: true,
94 custom_function_headers: false,
95 is_expressions: false,
96 buffer_size: 100,
97 stack_size: None,
98 record_keyword_positions: false,
99 interpolation_points: false,
100 partial_syntax: false,
101 destructuring_type_annotation: false,
102 extra_operators: false,
103 retain_blank_lines: false,
104 top_level_html: false,
105 }
106 }
107}
108
109#[allow(clippy::struct_excessive_bools)]
112#[cfg_attr(feature = "serde-serialize", derive(serde::Deserialize), serde(default))]
113#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
114pub struct ToStringOptions {
115 pub pretty: bool,
117 pub trailing_semicolon: bool,
119 pub single_statement_on_new_line: bool,
121 pub include_type_annotations: bool,
123 pub include_decorators: bool,
125 pub comments: Comments,
126 pub indent_with: String,
127 pub expect_jsx: bool,
129 pub expect_markers: bool,
134 pub max_line_length: u8,
136}
137
138impl Default for ToStringOptions {
139 fn default() -> Self {
140 ToStringOptions {
141 pretty: true,
142 include_type_annotations: false,
143 single_statement_on_new_line: true,
144 include_decorators: false,
145 comments: Comments::All,
146 expect_jsx: false,
147 trailing_semicolon: false,
148 expect_markers: false,
149 indent_with: "\t".to_owned(),
150 max_line_length: u8::MAX,
151 }
152 }
153}
154
155impl ToStringOptions {
156 #[must_use]
157 pub fn minified() -> Self {
158 ToStringOptions {
159 pretty: false,
160 comments: Comments::None,
161 indent_with: String::new(),
162 ..Default::default()
163 }
164 }
165
166 #[must_use]
168 pub fn typescript() -> Self {
169 ToStringOptions { include_type_annotations: true, ..Default::default() }
170 }
171
172 pub(crate) fn should_add_comment(&self, content: &str) -> bool {
174 self.comments.should_add_comment(content)
175 }
176
177 pub(crate) fn add_indent<T: source_map::ToString>(&self, indent: u8, buf: &mut T) {
178 if self.pretty {
179 (0..indent).for_each(|_| buf.push_str(&self.indent_with));
180 }
181 }
182
183 pub(crate) fn push_gap_optionally<T: source_map::ToString>(&self, buf: &mut T) {
185 if self.pretty {
186 buf.push(' ');
187 }
188 }
189
190 pub(crate) fn enforce_limit_length_limit(&self) -> bool {
191 self.pretty && self.max_line_length != u8::MAX
192 }
193}
194
195#[derive(Debug, Default, Clone, Copy)]
196#[cfg_attr(feature = "serde-serialize", derive(serde::Deserialize))]
197#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
198pub enum Comments {
199 #[default]
200 All,
201 JustDocumentation,
203 None,
204}
205
206impl Comments {
207 pub(crate) fn should_add_comment(self, content: &str) -> bool {
209 match self {
210 Comments::All => true,
211 Comments::None => false,
212 Comments::JustDocumentation => {
213 content.starts_with('*') || content.trim_start().starts_with('@')
214 }
215 }
216 }
217}