Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codegen)!: remove const generic MINIFY #5001

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{mem, ops::ControlFlow, path::Path};

use oxc_allocator::Allocator;
use oxc_ast::{ast::Program, Trivias};
use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions, WhitespaceRemover};
use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions};
use oxc_diagnostics::OxcDiagnostic;
use oxc_parser::{ParseOptions, Parser, ParserReturn};
use oxc_span::SourceType;
Expand Down Expand Up @@ -216,14 +216,10 @@ pub trait CompilerInterface {
) -> String {
let comment_options = CommentOptions { preserve_annotate_comments: true };

if self.remove_whitespace() {
WhitespaceRemover::new().with_options(options).build(program).source_text
} else {
CodeGenerator::new()
.with_options(options)
.enable_comment(source_text, trivias.clone(), comment_options)
.build(program)
.source_text
}
CodeGenerator::new()
.with_options(CodegenOptions { minify: self.remove_whitespace(), ..options })
.enable_comment(source_text, trivias.clone(), comment_options)
.build(program)
.source_text
}
}
7 changes: 5 additions & 2 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{env, path::Path};

use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CommentOptions, WhitespaceRemover};
use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
use pico_args::Arguments;
Expand Down Expand Up @@ -68,7 +68,10 @@ fn main() -> std::io::Result<()> {
if minify {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
let minified = WhitespaceRemover::new().build(&ret.program).source_text;
let minified = CodeGenerator::new()
.with_options(CodegenOptions { minify: true, ..CodegenOptions::default() })
.build(&ret.program)
.source_text;
println!("Minified:");
println!("{minified}");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/annotation_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl From<(Comment, AnnotationKind)> for AnnotationComment {
}
}

impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
impl<'a> Codegen<'a> {
pub(crate) fn get_leading_annotate_comments(
&mut self,
node_start: u32,
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_codegen/src/binary_expr_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub enum BinaryishOperator {
Logical(LogicalOperator),
}

impl<const MINIFY: bool> Gen<MINIFY> for BinaryishOperator {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
impl Gen for BinaryishOperator {
fn gen(&self, p: &mut Codegen, ctx: Context) {
match self {
Self::Binary(op) => op.gen(p, ctx),
Self::Logical(op) => op.gen(p, ctx),
Expand Down Expand Up @@ -89,7 +89,7 @@ pub struct BinaryExpressionVisitor<'a> {
}

impl<'a> BinaryExpressionVisitor<'a> {
pub fn gen_expr<const MINIFY: bool>(v: Self, p: &mut Codegen<'a, { MINIFY }>) {
pub fn gen_expr(v: Self, p: &mut Codegen<'a>) {
let mut v = v;
let stack_bottom = p.binary_expr_stack.len();
loop {
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'a> BinaryExpressionVisitor<'a> {
}
}

pub fn check_and_prepare<const MINIFY: bool>(&mut self, p: &mut Codegen<{ MINIFY }>) -> bool {
pub fn check_and_prepare(&mut self, p: &mut Codegen) -> bool {
let e = self.e;
self.operator = e.operator();

Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'a> BinaryExpressionVisitor<'a> {
true
}

pub fn visit_right_and_finish<const MINIFY: bool>(&self, p: &mut Codegen<{ MINIFY }>) {
pub fn visit_right_and_finish(&self, p: &mut Codegen) {
p.print_soft_space();
self.operator.gen(p, Context::empty());
p.print_soft_space();
Expand Down
Loading