Skip to content

Commit

Permalink
refactor(codegen): move options to its own file (#7053)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 1, 2024
1 parent 7f1d1fe commit 0bb1aa4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 52 deletions.
55 changes: 3 additions & 52 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
52 changes: 52 additions & 0 deletions crates/oxc_codegen/src/options.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 0bb1aa4

Please sign in to comment.