diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index bf36c4fb6..ec97b58ce 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -10,9 +10,10 @@ mod comment; mod context; mod gen; mod operator; +mod options; mod sourcemap_builder; -use std::{borrow::Cow, path::PathBuf}; +use std::borrow::Cow; use oxc_ast::ast::{ BindingIdentifier, BlockStatement, Expression, IdentifierReference, Program, Statement, @@ -32,62 +33,12 @@ use crate::{ pub use crate::{ context::Context, gen::{Gen, GenExpr}, + options::CodegenOptions, }; /// Code generator without whitespace removal. pub type CodeGenerator<'a> = Codegen<'a>; -/// Options for [`Codegen`]. -#[derive(Debug, Clone)] -pub struct CodegenOptions { - /// Use single quotes instead of double quotes. - /// - /// Default is `false`. - pub single_quote: bool, - - /// Remove whitespace. - /// - /// Default is `false`. - pub minify: bool, - - /// Print comments? - /// - /// Default is `true`. - pub comments: bool, - - /// Print annotation comments, e.g. `/* #__PURE__ */` and `/* #__NO_SIDE_EFFECTS__ */`. - /// - /// Only takes into effect when `comments` is false. - /// - /// Default is `false`. - pub annotation_comments: bool, - - /// Override the source map path. This affects the `sourceMappingURL` - /// comment at the end of the generated code. - /// - /// By default, the source map path is the same as the input source code - /// (with a `.map` extension). - pub source_map_path: Option, -} - -impl Default for CodegenOptions { - fn default() -> Self { - Self { - single_quote: false, - minify: false, - comments: true, - annotation_comments: false, - source_map_path: None, - } - } -} - -impl CodegenOptions { - fn print_annotation_comments(&self) -> bool { - !self.minify && (self.comments || self.annotation_comments) - } -} - /// Output from [`Codegen::build`] pub struct CodegenReturn { /// The generated source code. diff --git a/crates/oxc_codegen/src/options.rs b/crates/oxc_codegen/src/options.rs new file mode 100644 index 000000000..b692751c0 --- /dev/null +++ b/crates/oxc_codegen/src/options.rs @@ -0,0 +1,52 @@ +use std::path::PathBuf; + +/// Codegen Options. +#[derive(Debug, Clone)] +pub struct CodegenOptions { + /// Use single quotes instead of double quotes. + /// + /// Default is `false`. + pub single_quote: bool, + + /// Remove whitespace. + /// + /// Default is `false`. + pub minify: bool, + + /// Print comments? + /// + /// Default is `true`. + pub comments: bool, + + /// Print annotation comments, e.g. `/* #__PURE__ */` and `/* #__NO_SIDE_EFFECTS__ */`. + /// + /// Only takes into effect when `comments` is false. + /// + /// Default is `false`. + pub annotation_comments: bool, + + /// Override the source map path. This affects the `sourceMappingURL` + /// comment at the end of the generated code. + /// + /// By default, the source map path is the same as the input source code + /// (with a `.map` extension). + pub source_map_path: Option, +} + +impl Default for CodegenOptions { + fn default() -> Self { + Self { + single_quote: false, + minify: false, + comments: true, + annotation_comments: false, + source_map_path: None, + } + } +} + +impl CodegenOptions { + pub(crate) fn print_annotation_comments(&self) -> bool { + !self.minify && (self.comments || self.annotation_comments) + } +}