refactor(codegen): move options to its own file (#7053)

This commit is contained in:
Boshen 2024-11-01 09:28:59 +00:00
parent 7f1d1fe065
commit 0bb1aa4c64
2 changed files with 55 additions and 52 deletions

View file

@ -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<PathBuf>,
}
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.

View file

@ -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<PathBuf>,
}
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)
}
}