use super::lexer::LexerOptions;
#[allow(unused)]
#[derive(Copy, Clone)]
#[allow(clippy::struct_excessive_bools)]
#[cfg_attr(feature = "serde-serialize", derive(serde::Deserialize), serde(default))]
#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
pub struct ParseOptions {
pub jsx: bool,
pub type_annotations: bool,
pub type_definition_module: bool,
pub special_jsx_attributes: bool,
pub decorators: bool,
pub comments: Comments,
pub is_expressions: bool,
pub custom_function_headers: bool,
pub buffer_size: usize,
pub stack_size: Option<usize>,
pub record_keyword_positions: bool,
pub interpolation_points: bool,
pub destructuring_type_annotation: bool,
pub extra_operators: bool,
pub retain_blank_lines: bool,
pub partial_syntax: bool,
pub top_level_html: bool,
}
impl ParseOptions {
pub(crate) fn get_lex_options(&self) -> LexerOptions {
LexerOptions {
comments: self.comments,
lex_jsx: self.jsx,
allow_unsupported_characters_in_jsx_attribute_keys: self.special_jsx_attributes,
allow_expressions_in_jsx: true,
top_level_html: self.top_level_html,
}
}
#[must_use]
pub fn all_features() -> Self {
Self {
jsx: true,
type_annotations: true,
type_definition_module: false,
special_jsx_attributes: true,
comments: Comments::All,
decorators: true,
custom_function_headers: true,
is_expressions: true,
buffer_size: 100,
stack_size: None,
record_keyword_positions: true,
interpolation_points: false,
partial_syntax: true,
destructuring_type_annotation: true,
extra_operators: true,
retain_blank_lines: true,
top_level_html: false,
}
}
}
impl Default for ParseOptions {
fn default() -> Self {
Self {
jsx: true,
type_annotations: true,
type_definition_module: false,
special_jsx_attributes: false,
comments: Comments::All,
decorators: true,
custom_function_headers: false,
is_expressions: false,
buffer_size: 100,
stack_size: None,
record_keyword_positions: false,
interpolation_points: false,
partial_syntax: false,
destructuring_type_annotation: false,
extra_operators: false,
retain_blank_lines: false,
top_level_html: false,
}
}
}
#[allow(clippy::struct_excessive_bools)]
#[cfg_attr(feature = "serde-serialize", derive(serde::Deserialize), serde(default))]
#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
pub struct ToStringOptions {
pub pretty: bool,
pub trailing_semicolon: bool,
pub single_statement_on_new_line: bool,
pub include_type_annotations: bool,
pub include_decorators: bool,
pub comments: Comments,
pub indent_with: String,
pub expect_jsx: bool,
pub expect_markers: bool,
pub max_line_length: u8,
}
impl Default for ToStringOptions {
fn default() -> Self {
ToStringOptions {
pretty: true,
include_type_annotations: false,
single_statement_on_new_line: true,
include_decorators: false,
comments: Comments::All,
expect_jsx: false,
trailing_semicolon: false,
expect_markers: false,
indent_with: "\t".to_owned(),
max_line_length: u8::MAX,
}
}
}
impl ToStringOptions {
#[must_use]
pub fn minified() -> Self {
ToStringOptions {
pretty: false,
comments: Comments::None,
indent_with: String::new(),
..Default::default()
}
}
#[must_use]
pub fn typescript() -> Self {
ToStringOptions { include_type_annotations: true, ..Default::default() }
}
pub(crate) fn should_add_comment(&self, content: &str) -> bool {
self.comments.should_add_comment(content)
}
pub(crate) fn add_indent<T: source_map::ToString>(&self, indent: u8, buf: &mut T) {
if self.pretty {
(0..indent).for_each(|_| buf.push_str(&self.indent_with));
}
}
pub(crate) fn push_gap_optionally<T: source_map::ToString>(&self, buf: &mut T) {
if self.pretty {
buf.push(' ');
}
}
pub(crate) fn enforce_limit_length_limit(&self) -> bool {
self.pretty && self.max_line_length != u8::MAX
}
}
#[derive(Debug, Default, Clone, Copy)]
#[cfg_attr(feature = "serde-serialize", derive(serde::Deserialize))]
#[cfg_attr(target_family = "wasm", derive(tsify::Tsify))]
pub enum Comments {
#[default]
All,
JustDocumentation,
None,
}
impl Comments {
pub(crate) fn should_add_comment(self, content: &str) -> bool {
match self {
Comments::All => true,
Comments::None => false,
Comments::JustDocumentation => {
content.starts_with('*') || content.trim_start().starts_with('@')
}
}
}
}