diff --git a/crates/oxc/src/compiler.rs b/crates/oxc/src/compiler.rs index 1b6414b88074a..4bad3a894efa7 100644 --- a/crates/oxc/src/compiler.rs +++ b/crates/oxc/src/compiler.rs @@ -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; @@ -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 } } diff --git a/crates/oxc_codegen/examples/codegen.rs b/crates/oxc_codegen/examples/codegen.rs index 46df21a933d98..6bf41157875f7 100644 --- a/crates/oxc_codegen/examples/codegen.rs +++ b/crates/oxc_codegen/examples/codegen.rs @@ -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; @@ -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}"); } diff --git a/crates/oxc_codegen/src/annotation_comment.rs b/crates/oxc_codegen/src/annotation_comment.rs index a47c916438ead..395bf564d53a7 100644 --- a/crates/oxc_codegen/src/annotation_comment.rs +++ b/crates/oxc_codegen/src/annotation_comment.rs @@ -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, diff --git a/crates/oxc_codegen/src/binary_expr_visitor.rs b/crates/oxc_codegen/src/binary_expr_visitor.rs index bb30d2c2c87bf..1cdaf7b449ff1 100644 --- a/crates/oxc_codegen/src/binary_expr_visitor.rs +++ b/crates/oxc_codegen/src/binary_expr_visitor.rs @@ -47,8 +47,8 @@ pub enum BinaryishOperator { Logical(LogicalOperator), } -impl Gen 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), @@ -89,7 +89,7 @@ pub struct BinaryExpressionVisitor<'a> { } impl<'a> BinaryExpressionVisitor<'a> { - pub fn gen_expr(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 { @@ -133,7 +133,7 @@ impl<'a> BinaryExpressionVisitor<'a> { } } - pub fn check_and_prepare(&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(); @@ -181,7 +181,7 @@ impl<'a> BinaryExpressionVisitor<'a> { true } - pub fn visit_right_and_finish(&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(); diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 65c7c2b5a6d21..aaea2df95574c 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -17,34 +17,34 @@ use crate::{ Codegen, Context, Operator, }; -pub trait Gen { - fn gen(&self, _p: &mut Codegen<{ MINIFY }>, _ctx: Context) {} +pub trait Gen { + fn gen(&self, _p: &mut Codegen, _ctx: Context) {} } -pub trait GenExpr { - fn gen_expr(&self, _p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, _ctx: Context) {} +pub trait GenExpr { + fn gen_expr(&self, _p: &mut Codegen, _precedence: Precedence, _ctx: Context) {} } -impl<'a, const MINIFY: bool, T> Gen for Box<'a, T> +impl<'a, T> Gen for Box<'a, T> where - T: Gen, + T: Gen, { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { + fn gen(&self, p: &mut Codegen, ctx: Context) { (**self).gen(p, ctx); } } -impl<'a, const MINIFY: bool, T> GenExpr for Box<'a, T> +impl<'a, T> GenExpr for Box<'a, T> where - T: GenExpr, + T: GenExpr, { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { (**self).gen_expr(p, precedence, ctx); } } -impl<'a, const MINIFY: bool> Gen for Program<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for Program<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if let Some(hashbang) = &self.hashbang { hashbang.gen(p, ctx); } @@ -58,15 +58,15 @@ impl<'a, const MINIFY: bool> Gen for Program<'a> { } } -impl<'a, const MINIFY: bool> Gen for Hashbang<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for Hashbang<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.print_str("#!"); p.print_str(self.value.as_str()); } } -impl<'a, const MINIFY: bool> Gen for Directive<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for Directive<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); // A Use Strict Directive may not contain an EscapeSequence or LineContinuation. @@ -80,8 +80,8 @@ impl<'a, const MINIFY: bool> Gen for Directive<'a> { } } -impl<'a, const MINIFY: bool> Gen for Statement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for Statement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::BlockStatement(stmt) => stmt.gen(p, ctx), Self::BreakStatement(stmt) => stmt.gen(p, ctx), @@ -162,8 +162,8 @@ impl<'a, const MINIFY: bool> Gen for Statement<'a> { } } -impl<'a, const MINIFY: bool> Gen for ExpressionStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for ExpressionStatement<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.start_of_stmt = p.code_len(); @@ -176,19 +176,15 @@ impl<'a, const MINIFY: bool> Gen for ExpressionStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for IfStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for IfStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); print_if(self, p, ctx); } } -fn print_if( - if_stmt: &IfStatement<'_>, - p: &mut Codegen<{ MINIFY }>, - ctx: Context, -) { +fn print_if(if_stmt: &IfStatement<'_>, p: &mut Codegen, ctx: Context) { p.print_str("if"); p.print_soft_space(); p.print_char(b'('); @@ -261,16 +257,16 @@ fn wrap_to_avoid_ambiguous_else(stmt: &Statement) -> bool { } } -impl<'a, const MINIFY: bool> Gen for BlockStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for BlockStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_indent(); p.print_block_statement(self, ctx); p.print_soft_newline(); } } -impl<'a, const MINIFY: bool> Gen for ForStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ForStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("for"); @@ -300,8 +296,8 @@ impl<'a, const MINIFY: bool> Gen for ForStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for ForInStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ForInStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("for"); @@ -318,8 +314,8 @@ impl<'a, const MINIFY: bool> Gen for ForInStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for ForOfStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ForOfStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("for"); @@ -338,8 +334,8 @@ impl<'a, const MINIFY: bool> Gen for ForOfStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for ForStatementInit<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ForStatementInit<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::UsingDeclaration(decl) => decl.gen(p, ctx), match_expression!(ForStatementInit) => { @@ -350,8 +346,8 @@ impl<'a, const MINIFY: bool> Gen for ForStatementInit<'a> { } } -impl<'a, const MINIFY: bool> Gen for ForStatementLeft<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ForStatementLeft<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { ForStatementLeft::UsingDeclaration(var) => var.gen(p, ctx), ForStatementLeft::VariableDeclaration(var) => var.gen(p, ctx), @@ -366,8 +362,8 @@ impl<'a, const MINIFY: bool> Gen for ForStatementLeft<'a> { } } -impl<'a, const MINIFY: bool> Gen for WhileStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for WhileStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("while"); @@ -379,8 +375,8 @@ impl<'a, const MINIFY: bool> Gen for WhileStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for DoWhileStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for DoWhileStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("do "); @@ -404,8 +400,8 @@ impl<'a, const MINIFY: bool> Gen for DoWhileStatement<'a> { } } -impl Gen for EmptyStatement { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for EmptyStatement { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_semicolon(); @@ -413,8 +409,8 @@ impl Gen for EmptyStatement { } } -impl<'a, const MINIFY: bool> Gen for ContinueStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ContinueStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("continue"); @@ -426,8 +422,8 @@ impl<'a, const MINIFY: bool> Gen for ContinueStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for BreakStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for BreakStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("break"); @@ -439,8 +435,8 @@ impl<'a, const MINIFY: bool> Gen for BreakStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for SwitchStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for SwitchStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("switch"); @@ -460,8 +456,8 @@ impl<'a, const MINIFY: bool> Gen for SwitchStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for SwitchCase<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for SwitchCase<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_semicolon_if_needed(); p.print_indent(); match &self.test { @@ -488,8 +484,8 @@ impl<'a, const MINIFY: bool> Gen for SwitchCase<'a> { } } -impl<'a, const MINIFY: bool> Gen for ReturnStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for ReturnStatement<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("return"); @@ -501,9 +497,9 @@ impl<'a, const MINIFY: bool> Gen for ReturnStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for LabeledStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { - if !MINIFY && (p.indent > 0 || p.print_next_indent_as_space) { +impl<'a> Gen for LabeledStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { + if !p.options.minify && (p.indent > 0 || p.print_next_indent_as_space) { p.add_source_mapping(self.span.start); p.print_indent(); } @@ -514,8 +510,8 @@ impl<'a, const MINIFY: bool> Gen for LabeledStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for TryStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TryStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_space_before_identifier(); @@ -547,8 +543,8 @@ impl<'a, const MINIFY: bool> Gen for TryStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for ThrowStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for ThrowStatement<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("throw "); @@ -557,8 +553,8 @@ impl<'a, const MINIFY: bool> Gen for ThrowStatement<'a> { } } -impl<'a, const MINIFY: bool> Gen for WithStatement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for WithStatement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("with"); @@ -569,8 +565,8 @@ impl<'a, const MINIFY: bool> Gen for WithStatement<'a> { } } -impl Gen for DebuggerStatement { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for DebuggerStatement { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("debugger"); @@ -578,8 +574,8 @@ impl Gen for DebuggerStatement { } } -impl<'a, const MINIFY: bool> Gen for UsingDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for UsingDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.is_await { p.print_str("await "); } @@ -588,8 +584,8 @@ impl<'a, const MINIFY: bool> Gen for UsingDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for VariableDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for VariableDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); if self.declare { p.print_str("declare "); @@ -612,8 +608,8 @@ impl<'a, const MINIFY: bool> Gen for VariableDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for VariableDeclarator<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for VariableDeclarator<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.id.gen(p, ctx); if let Some(init) = &self.init { p.print_soft_space(); @@ -624,8 +620,8 @@ impl<'a, const MINIFY: bool> Gen for VariableDeclarator<'a> { } } -impl<'a, const MINIFY: bool> Gen for Function<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for Function<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { let n = p.code_len(); let wrap = self.is_expression() && (p.start_of_stmt == n || p.start_of_default_export == n); p.gen_comments(self.span.start); @@ -674,8 +670,8 @@ impl<'a, const MINIFY: bool> Gen for Function<'a> { } } -impl<'a, const MINIFY: bool> Gen for FunctionBody<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for FunctionBody<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_curly_braces(self.span, self.is_empty(), |p| { for directive in &self.directives { directive.gen(p, ctx); @@ -689,8 +685,8 @@ impl<'a, const MINIFY: bool> Gen for FunctionBody<'a> { } } -impl<'a, const MINIFY: bool> Gen for FormalParameter<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for FormalParameter<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.decorators.gen(p, ctx); if let Some(accessibility) = self.accessibility { accessibility.gen(p, ctx); @@ -702,8 +698,8 @@ impl<'a, const MINIFY: bool> Gen for FormalParameter<'a> { } } -impl<'a, const MINIFY: bool> Gen for FormalParameters<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for FormalParameters<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_list(&self.items, ctx); if let Some(rest) = &self.rest { if !self.items.is_empty() { @@ -715,8 +711,8 @@ impl<'a, const MINIFY: bool> Gen for FormalParameters<'a> { } } -impl<'a, const MINIFY: bool> Gen for ImportDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ImportDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("import "); @@ -824,16 +820,16 @@ impl<'a, const MINIFY: bool> Gen for ImportDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for Option> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for Option> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if let Some(with_clause) = self { with_clause.gen(p, ctx); } } } -impl<'a, const MINIFY: bool> Gen for WithClause<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for WithClause<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); self.attributes_keyword.gen(p, ctx); p.print_soft_space(); @@ -843,8 +839,8 @@ impl<'a, const MINIFY: bool> Gen for WithClause<'a> { } } -impl<'a, const MINIFY: bool> Gen for ImportAttribute<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ImportAttribute<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match &self.key { ImportAttributeKey::Identifier(identifier) => { p.print_str(identifier.name.as_str()); @@ -857,8 +853,8 @@ impl<'a, const MINIFY: bool> Gen for ImportAttribute<'a> { } } -impl<'a, const MINIFY: bool> Gen for ExportNamedDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ExportNamedDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); @@ -925,8 +921,8 @@ impl<'a, const MINIFY: bool> Gen for ExportNamedDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSExportAssignment<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSExportAssignment<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_indent(); p.print_str("export = "); self.expression.gen_expr(p, Precedence::Lowest, ctx); @@ -934,8 +930,8 @@ impl<'a, const MINIFY: bool> Gen for TSExportAssignment<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSNamespaceExportDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSNamespaceExportDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_indent(); p.print_str("export as namespace "); self.id.gen(p, ctx); @@ -943,8 +939,8 @@ impl<'a, const MINIFY: bool> Gen for TSNamespaceExportDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for ExportSpecifier<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ExportSpecifier<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.export_kind.is_type() { p.print_str("type "); } @@ -956,8 +952,8 @@ impl<'a, const MINIFY: bool> Gen for ExportSpecifier<'a> { } } -impl<'a, const MINIFY: bool> Gen for ModuleExportName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ModuleExportName<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::IdentifierName(identifier) => p.print_str(identifier.name.as_str()), Self::IdentifierReference(identifier) => identifier.gen(p, ctx), @@ -966,8 +962,8 @@ impl<'a, const MINIFY: bool> Gen for ModuleExportName<'a> { } } -impl<'a, const MINIFY: bool> Gen for ExportAllDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ExportAllDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("export "); @@ -991,16 +987,16 @@ impl<'a, const MINIFY: bool> Gen for ExportAllDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for ExportDefaultDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ExportDefaultDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_indent(); p.print_str("export default "); self.declaration.gen(p, ctx); } } -impl<'a, const MINIFY: bool> Gen for ExportDefaultDeclarationKind<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ExportDefaultDeclarationKind<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { match_expression!(Self) => { p.start_of_default_export = p.code_len(); @@ -1020,8 +1016,8 @@ impl<'a, const MINIFY: bool> Gen for ExportDefaultDeclarationKind<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for Expression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for Expression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { match self { Self::BooleanLiteral(lit) => lit.gen(p, ctx), Self::NullLiteral(lit) => lit.gen(p, ctx), @@ -1072,14 +1068,14 @@ impl<'a, const MINIFY: bool> GenExpr for Expression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for ParenthesizedExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for ParenthesizedExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { self.expression.gen_expr(p, precedence, ctx); } } -impl<'a, const MINIFY: bool> Gen for IdentifierReference<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for IdentifierReference<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { let name = p.get_identifier_reference_name(self); p.print_space_before_identifier(); p.add_source_mapping_for_name(self.span, name); @@ -1087,38 +1083,38 @@ impl<'a, const MINIFY: bool> Gen for IdentifierReference<'a> { } } -impl<'a, const MINIFY: bool> Gen for IdentifierName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for IdentifierName<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_str(self.name.as_str()); } } -impl<'a, const MINIFY: bool> Gen for BindingIdentifier<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for BindingIdentifier<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { let name = p.get_binding_identifier_name(self); p.add_source_mapping_for_name(self.span, name); p.print_str(name); } } -impl<'a, const MINIFY: bool> Gen for LabelIdentifier<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for LabelIdentifier<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping_for_name(self.span, &self.name); p.print_str(self.name.as_str()); } } -impl Gen for BooleanLiteral { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for BooleanLiteral { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_space_before_identifier(); p.print_str(self.as_str()); } } -impl Gen for NullLiteral { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for NullLiteral { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.print_space_before_identifier(); p.add_source_mapping(self.span.start); p.print_str("null"); @@ -1126,17 +1122,17 @@ impl Gen for NullLiteral { } // Need a space before "." if it could be parsed as a decimal point. -fn need_space_before_dot(s: &str, p: &mut Codegen<{ MINIFY }>) { +fn need_space_before_dot(s: &str, p: &mut Codegen) { if !s.bytes().any(|b| matches!(b, b'.' | b'e' | b'x')) { p.need_space_before_dot = p.code_len(); } } -impl<'a, const MINIFY: bool> Gen for NumericLiteral<'a> { +impl<'a> Gen for NumericLiteral<'a> { #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); - if self.value != f64::INFINITY && (MINIFY || self.raw.is_empty()) { + if self.value != f64::INFINITY && (p.options.minify || self.raw.is_empty()) { p.print_space_before_identifier(); let abs_value = self.value.abs(); @@ -1162,7 +1158,7 @@ impl<'a, const MINIFY: bool> Gen for NumericLiteral<'a> { // TODO: refactor this with less allocations // #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] -fn print_non_negative_float(value: f64, _p: &Codegen<{ MINIFY }>) -> String { +fn print_non_negative_float(value: f64, p: &Codegen) -> String { use oxc_syntax::number::ToJsString; if value < 1000.0 && value.fract() == 0.0 { return value.to_js_string(); @@ -1220,7 +1216,7 @@ fn print_non_negative_float(value: f64, _p: &Codegen<{ MINIF } } - if MINIFY && value.fract() == 0.0 { + if p.options.minify && value.fract() == 0.0 { let value = value as u64; if (1_000_000_000_000..=0xFFFF_FFFF_FFFF_F800).contains(&value) { let hex = format!("{value:#x}"); @@ -1233,15 +1229,15 @@ fn print_non_negative_float(value: f64, _p: &Codegen<{ MINIF result } -impl<'a, const MINIFY: bool> Gen for BigIntLiteral<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for BigIntLiteral<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_str(self.raw.as_str()); } } -impl<'a, const MINIFY: bool> Gen for RegExpLiteral<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for RegExpLiteral<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); let last = p.peek_nth(0); // Avoid forming a single-line comment or " Gen for RegExpLiteral<'a> { } } -fn print_unquoted_str(s: &str, quote: u8, p: &mut Codegen<{ MINIFY }>) { +fn print_unquoted_str(s: &str, quote: u8, p: &mut Codegen) { let mut chars = s.chars().peekable(); while let Some(c) = chars.next() { @@ -1340,8 +1336,8 @@ fn print_unquoted_str(s: &str, quote: u8, p: &mut Codegen<{ } } -impl<'a, const MINIFY: bool> Gen for StringLiteral<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for StringLiteral<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); let s = self.value.as_str(); p.wrap_quote(|p, quote| { @@ -1350,16 +1346,16 @@ impl<'a, const MINIFY: bool> Gen for StringLiteral<'a> { } } -impl Gen for ThisExpression { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for ThisExpression { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_space_before_identifier(); p.print_str("this"); } } -impl<'a, const MINIFY: bool> GenExpr for MemberExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for MemberExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { match self { Self::ComputedMemberExpression(expr) => expr.gen_expr(p, precedence, ctx), Self::StaticMemberExpression(expr) => expr.gen_expr(p, precedence, ctx), @@ -1368,8 +1364,8 @@ impl<'a, const MINIFY: bool> GenExpr for MemberExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for ComputedMemberExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for ComputedMemberExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, _precedence: Precedence, ctx: Context) { self.object.gen_expr(p, Precedence::Prefix, ctx.intersection(Context::FORBID_CALL)); if self.optional { p.print_str("?."); @@ -1380,8 +1376,8 @@ impl<'a, const MINIFY: bool> GenExpr for ComputedMemberExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for StaticMemberExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for StaticMemberExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, _precedence: Precedence, ctx: Context) { self.object.gen_expr(p, Precedence::Postfix, ctx.intersection(Context::FORBID_CALL)); if self.optional { p.print_char(b'?'); @@ -1394,8 +1390,8 @@ impl<'a, const MINIFY: bool> GenExpr for StaticMemberExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for PrivateFieldExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for PrivateFieldExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, _precedence: Precedence, ctx: Context) { self.object.gen_expr(p, Precedence::Prefix, ctx.intersection(Context::FORBID_CALL)); if self.optional { p.print_str("?"); @@ -1405,8 +1401,8 @@ impl<'a, const MINIFY: bool> GenExpr for PrivateFieldExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for CallExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for CallExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { let mut wrap = precedence >= Precedence::New || ctx.intersects(Context::FORBID_CALL); let annotate_comments = p.get_leading_annotate_comments(self.span.start); if !annotate_comments.is_empty() && precedence >= Precedence::Postfix { @@ -1430,8 +1426,8 @@ impl<'a, const MINIFY: bool> GenExpr for CallExpression<'a> { } } -impl<'a, const MINIFY: bool> Gen for Argument<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for Argument<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::SpreadElement(elem) => elem.gen(p, ctx), match_expression!(Self) => { @@ -1441,8 +1437,8 @@ impl<'a, const MINIFY: bool> Gen for Argument<'a> { } } -impl<'a, const MINIFY: bool> Gen for ArrayExpressionElement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ArrayExpressionElement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { match_expression!(Self) => { self.to_expression().gen_expr(p, Precedence::Comma, Context::empty()); @@ -1453,16 +1449,16 @@ impl<'a, const MINIFY: bool> Gen for ArrayExpressionElement<'a> { } } -impl<'a, const MINIFY: bool> Gen for SpreadElement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for SpreadElement<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_ellipsis(); self.argument.gen_expr(p, Precedence::Comma, Context::empty()); } } -impl<'a, const MINIFY: bool> Gen for ArrayExpression<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ArrayExpression<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_char(b'['); p.print_list(&self.elements, ctx); @@ -1474,8 +1470,8 @@ impl<'a, const MINIFY: bool> Gen for ArrayExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for ObjectExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, _precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for ObjectExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, _precedence: Precedence, ctx: Context) { let n = p.code_len(); let len = self.properties.len(); let is_multi_line = len > 1; @@ -1511,8 +1507,8 @@ impl<'a, const MINIFY: bool> GenExpr for ObjectExpression<'a> { } } -impl<'a, const MINIFY: bool> Gen for ObjectPropertyKind<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ObjectPropertyKind<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::ObjectProperty(prop) => prop.gen(p, ctx), Self::SpreadProperty(elem) => elem.gen(p, ctx), @@ -1520,8 +1516,8 @@ impl<'a, const MINIFY: bool> Gen for ObjectPropertyKind<'a> { } } -impl<'a, const MINIFY: bool> Gen for ObjectProperty<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ObjectProperty<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if let Expression::FunctionExpression(func) = &self.value { p.add_source_mapping(self.span.start); let is_accessor = match &self.kind { @@ -1591,8 +1587,8 @@ impl<'a, const MINIFY: bool> Gen for ObjectProperty<'a> { } } -impl<'a, const MINIFY: bool> Gen for PropertyKey<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for PropertyKey<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::StaticIdentifier(ident) => ident.gen(p, ctx), Self::PrivateIdentifier(ident) => ident.gen(p, ctx), @@ -1603,8 +1599,8 @@ impl<'a, const MINIFY: bool> Gen for PropertyKey<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for ArrowFunctionExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for ArrowFunctionExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { p.wrap(precedence >= Precedence::Assign, |p| { p.gen_comments(self.span.start); if self.r#async { @@ -1643,8 +1639,8 @@ impl<'a, const MINIFY: bool> GenExpr for ArrowFunctionExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for YieldExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, _ctx: Context) { +impl<'a> GenExpr for YieldExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, _ctx: Context) { p.wrap(precedence >= Precedence::Assign, |p| { p.add_source_mapping(self.span.start); p.print_space_before_identifier(); @@ -1663,8 +1659,8 @@ impl<'a, const MINIFY: bool> GenExpr for YieldExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for UpdateExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for UpdateExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { let operator = self.operator.as_str(); p.wrap(precedence >= self.precedence(), |p| { if self.prefix { @@ -1685,8 +1681,8 @@ impl<'a, const MINIFY: bool> GenExpr for UpdateExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for UnaryExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for UnaryExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { p.wrap(precedence >= self.precedence(), |p| { let operator = self.operator.as_str(); if self.operator.is_keyword() { @@ -1704,8 +1700,8 @@ impl<'a, const MINIFY: bool> GenExpr for UnaryExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for BinaryExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for BinaryExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { let v = BinaryExpressionVisitor { // SAFETY: // The pointer is stored on the heap and all will be consumed in the binary expression visitor. @@ -1724,14 +1720,14 @@ impl<'a, const MINIFY: bool> GenExpr for BinaryExpression<'a> { } } -impl Gen for LogicalOperator { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for LogicalOperator { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.print_str(self.as_str()); } } -impl Gen for BinaryOperator { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for BinaryOperator { + fn gen(&self, p: &mut Codegen, _ctx: Context) { let operator = self.as_str(); if self.is_keyword() { p.print_space_before_identifier(); @@ -1746,8 +1742,8 @@ impl Gen for BinaryOperator { } } -impl<'a, const MINIFY: bool> GenExpr for PrivateInExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for PrivateInExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { p.wrap(precedence >= Precedence::Compare, |p| { self.left.gen(p, ctx); p.print_str(" in "); @@ -1756,8 +1752,8 @@ impl<'a, const MINIFY: bool> GenExpr for PrivateInExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for LogicalExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for LogicalExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { let v = BinaryExpressionVisitor { // SAFETY: // The pointer is stored on the heap and all will be consumed in the binary expression visitor. @@ -1776,8 +1772,8 @@ impl<'a, const MINIFY: bool> GenExpr for LogicalExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for ConditionalExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for ConditionalExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { let mut ctx = ctx; let wrap = precedence >= self.precedence(); if wrap { @@ -1797,8 +1793,8 @@ impl<'a, const MINIFY: bool> GenExpr for ConditionalExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for AssignmentExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for AssignmentExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { // Destructuring assignment let n = p.code_len(); @@ -1834,8 +1830,8 @@ impl<'a, const MINIFY: bool> GenExpr for AssignmentExpression<'a> { } } -impl<'a, const MINIFY: bool> Gen for AssignmentTarget<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AssignmentTarget<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { match_simple_assignment_target!(Self) => { self.to_simple_assignment_target().gen_expr(p, Precedence::Comma, Context::empty()); @@ -1847,8 +1843,8 @@ impl<'a, const MINIFY: bool> Gen for AssignmentTarget<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for SimpleAssignmentTarget<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for SimpleAssignmentTarget<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { match self { Self::AssignmentTargetIdentifier(ident) => ident.gen(p, ctx), match_member_expression!(Self) => { @@ -1863,8 +1859,8 @@ impl<'a, const MINIFY: bool> GenExpr for SimpleAssignmentTarget<'a> { } } -impl<'a, const MINIFY: bool> Gen for AssignmentTargetPattern<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AssignmentTargetPattern<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::ArrayAssignmentTarget(target) => target.gen(p, ctx), Self::ObjectAssignmentTarget(target) => target.gen(p, ctx), @@ -1872,8 +1868,8 @@ impl<'a, const MINIFY: bool> Gen for AssignmentTargetPattern<'a> { } } -impl<'a, const MINIFY: bool> Gen for ArrayAssignmentTarget<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ArrayAssignmentTarget<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_char(b'['); p.print_list(&self.elements, ctx); @@ -1892,16 +1888,16 @@ impl<'a, const MINIFY: bool> Gen for ArrayAssignmentTarget<'a> { } } -impl<'a, const MINIFY: bool> Gen for Option> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for Option> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if let Some(arg) = self { arg.gen(p, ctx); } } } -impl<'a, const MINIFY: bool> Gen for ObjectAssignmentTarget<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ObjectAssignmentTarget<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_char(b'{'); p.print_list(&self.properties, ctx); @@ -1917,8 +1913,8 @@ impl<'a, const MINIFY: bool> Gen for ObjectAssignmentTarget<'a> { } } -impl<'a, const MINIFY: bool> Gen for AssignmentTargetMaybeDefault<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AssignmentTargetMaybeDefault<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { match_assignment_target!(Self) => self.to_assignment_target().gen(p, ctx), Self::AssignmentTargetWithDefault(target) => target.gen(p, ctx), @@ -1926,8 +1922,8 @@ impl<'a, const MINIFY: bool> Gen for AssignmentTargetMaybeDefault<'a> { } } -impl<'a, const MINIFY: bool> Gen for AssignmentTargetWithDefault<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AssignmentTargetWithDefault<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.binding.gen(p, ctx); p.print_soft_space(); p.print_equal(); @@ -1936,8 +1932,8 @@ impl<'a, const MINIFY: bool> Gen for AssignmentTargetWithDefault<'a> { } } -impl<'a, const MINIFY: bool> Gen for AssignmentTargetProperty<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AssignmentTargetProperty<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::AssignmentTargetPropertyIdentifier(ident) => ident.gen(p, ctx), Self::AssignmentTargetPropertyProperty(prop) => prop.gen(p, ctx), @@ -1945,8 +1941,8 @@ impl<'a, const MINIFY: bool> Gen for AssignmentTargetProperty<'a> { } } -impl<'a, const MINIFY: bool> Gen for AssignmentTargetPropertyIdentifier<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AssignmentTargetPropertyIdentifier<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { let ident_name = p.get_identifier_reference_name(&self.binding).to_owned(); if ident_name == self.binding.name.as_str() { self.binding.gen(p, ctx); @@ -1966,8 +1962,8 @@ impl<'a, const MINIFY: bool> Gen for AssignmentTargetPropertyIdentifier< } } -impl<'a, const MINIFY: bool> Gen for AssignmentTargetPropertyProperty<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AssignmentTargetPropertyProperty<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match &self.name { PropertyKey::StaticIdentifier(ident) => { ident.gen(p, ctx); @@ -1987,23 +1983,23 @@ impl<'a, const MINIFY: bool> Gen for AssignmentTargetPropertyProperty<'a } } -impl<'a, const MINIFY: bool> Gen for AssignmentTargetRest<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AssignmentTargetRest<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_ellipsis(); self.target.gen(p, ctx); } } -impl<'a, const MINIFY: bool> GenExpr for SequenceExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, _ctx: Context) { +impl<'a> GenExpr for SequenceExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, _ctx: Context) { p.wrap(precedence >= self.precedence(), |p| { p.print_expressions(&self.expressions, Precedence::Lowest, Context::empty()); }); } } -impl<'a, const MINIFY: bool> GenExpr for ImportExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for ImportExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { let wrap = precedence >= Precedence::New || ctx.intersects(Context::FORBID_CALL); p.wrap(wrap, |p| { p.add_source_mapping(self.span.start); @@ -2018,8 +2014,8 @@ impl<'a, const MINIFY: bool> GenExpr for ImportExpression<'a> { } } -impl<'a, const MINIFY: bool> Gen for TemplateLiteral<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for TemplateLiteral<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.print_char(b'`'); let mut expressions = self.expressions.iter(); @@ -2038,23 +2034,23 @@ impl<'a, const MINIFY: bool> Gen for TemplateLiteral<'a> { } } -impl<'a, const MINIFY: bool> Gen for TaggedTemplateExpression<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TaggedTemplateExpression<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); self.tag.gen_expr(p, Precedence::Postfix, Context::empty()); self.quasi.gen(p, ctx); } } -impl Gen for Super { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for Super { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_str("super"); } } -impl<'a, const MINIFY: bool> GenExpr for AwaitExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for AwaitExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { p.wrap(precedence >= self.precedence(), |p| { p.add_source_mapping(self.span.start); p.print_str("await "); @@ -2063,8 +2059,8 @@ impl<'a, const MINIFY: bool> GenExpr for AwaitExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for ChainExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for ChainExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { match &self.expression { ChainElement::CallExpression(expr) => expr.gen_expr(p, precedence, ctx), match_member_expression!(ChainElement) => { @@ -2074,8 +2070,8 @@ impl<'a, const MINIFY: bool> GenExpr for ChainExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for NewExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for NewExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { let mut wrap = precedence >= self.precedence(); let annotate_comment = p.get_leading_annotate_comments(self.span.start); if !annotate_comment.is_empty() && precedence >= Precedence::Postfix { @@ -2094,8 +2090,8 @@ impl<'a, const MINIFY: bool> GenExpr for NewExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for TSAsExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for TSAsExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { p.print_char(b'('); p.print_char(b'('); self.expression.gen_expr(p, precedence, Context::default()); @@ -2106,8 +2102,8 @@ impl<'a, const MINIFY: bool> GenExpr for TSAsExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for TSSatisfiesExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for TSSatisfiesExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { p.print_char(b'('); p.print_char(b'('); self.expression.gen_expr(p, precedence, Context::default()); @@ -2118,30 +2114,30 @@ impl<'a, const MINIFY: bool> GenExpr for TSSatisfiesExpression<'a> { } } -impl<'a, const MINIFY: bool> GenExpr for TSNonNullExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for TSNonNullExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { p.wrap(matches!(self.expression, Expression::ParenthesizedExpression(_)), |p| { self.expression.gen_expr(p, precedence, ctx); }); p.print_char(b'!'); - if MINIFY { + if p.options.minify { p.print_hard_space(); } } } -impl<'a, const MINIFY: bool> GenExpr for TSInstantiationExpression<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for TSInstantiationExpression<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { self.expression.gen_expr(p, precedence, ctx); self.type_parameters.gen(p, ctx); - if MINIFY { + if p.options.minify { p.print_hard_space(); } } } -impl<'a, const MINIFY: bool> GenExpr for TSTypeAssertion<'a> { - fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { +impl<'a> GenExpr for TSTypeAssertion<'a> { + fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { p.wrap(precedence >= self.precedence(), |p| { p.print_str("<"); // var r = < (x: T) => T > ((x) => { return null; }); @@ -2156,8 +2152,8 @@ impl<'a, const MINIFY: bool> GenExpr for TSTypeAssertion<'a> { } } -impl<'a, const MINIFY: bool> Gen for MetaProperty<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for MetaProperty<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); self.meta.gen(p, ctx); p.print_char(b'.'); @@ -2165,8 +2161,8 @@ impl<'a, const MINIFY: bool> Gen for MetaProperty<'a> { } } -impl<'a, const MINIFY: bool> Gen for Class<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for Class<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); if self.declare { p.print_str("declare "); @@ -2204,8 +2200,8 @@ impl<'a, const MINIFY: bool> Gen for Class<'a> { } } -impl<'a, const MINIFY: bool> Gen for ClassBody<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ClassBody<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_curly_braces(self.span, self.body.is_empty(), |p| { for item in &self.body { p.print_semicolon_if_needed(); @@ -2216,8 +2212,8 @@ impl<'a, const MINIFY: bool> Gen for ClassBody<'a> { } } -impl<'a, const MINIFY: bool> Gen for ClassElement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ClassElement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::StaticBlock(elem) => { elem.gen(p, ctx); @@ -2243,15 +2239,15 @@ impl<'a, const MINIFY: bool> Gen for ClassElement<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXIdentifier<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for JSXIdentifier<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping_for_name(self.span, &self.name); p.print_str(self.name.as_str()); } } -impl<'a, const MINIFY: bool> Gen for JSXMemberExpressionObject<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXMemberExpressionObject<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::Identifier(ident) => ident.gen(p, ctx), Self::MemberExpression(member_expr) => member_expr.gen(p, ctx), @@ -2259,16 +2255,16 @@ impl<'a, const MINIFY: bool> Gen for JSXMemberExpressionObject<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXMemberExpression<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXMemberExpression<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.object.gen(p, ctx); p.print_char(b'.'); self.property.gen(p, ctx); } } -impl<'a, const MINIFY: bool> Gen for JSXElementName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXElementName<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::Identifier(identifier) => identifier.gen(p, ctx), Self::NamespacedName(namespaced_name) => namespaced_name.gen(p, ctx), @@ -2277,16 +2273,16 @@ impl<'a, const MINIFY: bool> Gen for JSXElementName<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXNamespacedName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXNamespacedName<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.namespace.gen(p, ctx); p.print_colon(); self.property.gen(p, ctx); } } -impl<'a, const MINIFY: bool> Gen for JSXAttributeName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXAttributeName<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::Identifier(ident) => ident.gen(p, ctx), Self::NamespacedName(namespaced_name) => namespaced_name.gen(p, ctx), @@ -2294,8 +2290,8 @@ impl<'a, const MINIFY: bool> Gen for JSXAttributeName<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXAttribute<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXAttribute<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.name.gen(p, ctx); if let Some(value) = &self.value { p.print_equal(); @@ -2304,12 +2300,12 @@ impl<'a, const MINIFY: bool> Gen for JSXAttribute<'a> { } } -impl Gen for JSXEmptyExpression { - fn gen(&self, _: &mut Codegen<{ MINIFY }>, _ctx: Context) {} +impl Gen for JSXEmptyExpression { + fn gen(&self, _: &mut Codegen, _ctx: Context) {} } -impl<'a, const MINIFY: bool> Gen for JSXExpression<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXExpression<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { match_expression!(Self) => p.print_expression(self.to_expression()), Self::EmptyExpression(expr) => expr.gen(p, ctx), @@ -2317,16 +2313,16 @@ impl<'a, const MINIFY: bool> Gen for JSXExpression<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXExpressionContainer<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXExpressionContainer<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_char(b'{'); self.expression.gen(p, ctx); p.print_char(b'}'); } } -impl<'a, const MINIFY: bool> Gen for JSXAttributeValue<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXAttributeValue<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::Fragment(fragment) => fragment.gen(p, ctx), Self::Element(el) => el.gen(p, ctx), @@ -2341,16 +2337,16 @@ impl<'a, const MINIFY: bool> Gen for JSXAttributeValue<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXSpreadAttribute<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for JSXSpreadAttribute<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.print_str("{..."); self.argument.gen_expr(p, Precedence::Comma, Context::empty()); p.print_char(b'}'); } } -impl<'a, const MINIFY: bool> Gen for JSXAttributeItem<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXAttributeItem<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::Attribute(attr) => attr.gen(p, ctx), Self::SpreadAttribute(spread_attr) => spread_attr.gen(p, ctx), @@ -2358,8 +2354,8 @@ impl<'a, const MINIFY: bool> Gen for JSXAttributeItem<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXOpeningElement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXOpeningElement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_char(b'<'); self.name.gen(p, ctx); @@ -2382,8 +2378,8 @@ impl<'a, const MINIFY: bool> Gen for JSXOpeningElement<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXClosingElement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXClosingElement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_str(" Gen for JSXClosingElement<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXElement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXElement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.opening_element.gen(p, ctx); for child in &self.children { child.gen(p, ctx); @@ -2403,36 +2399,36 @@ impl<'a, const MINIFY: bool> Gen for JSXElement<'a> { } } -impl Gen for JSXOpeningFragment { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for JSXOpeningFragment { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_str("<>"); } } -impl Gen for JSXClosingFragment { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for JSXClosingFragment { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_str(""); } } -impl<'a, const MINIFY: bool> Gen for JSXText<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for JSXText<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping(self.span.start); p.print_str(self.value.as_str()); } } -impl<'a, const MINIFY: bool> Gen for JSXSpreadChild<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for JSXSpreadChild<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.print_str("..."); p.print_expression(&self.expression); } } -impl<'a, const MINIFY: bool> Gen for JSXChild<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXChild<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::Fragment(fragment) => fragment.gen(p, ctx), Self::Element(el) => el.gen(p, ctx), @@ -2443,8 +2439,8 @@ impl<'a, const MINIFY: bool> Gen for JSXChild<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSXFragment<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSXFragment<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.opening_fragment.gen(p, ctx); for child in &self.children { child.gen(p, ctx); @@ -2453,8 +2449,8 @@ impl<'a, const MINIFY: bool> Gen for JSXFragment<'a> { } } -impl<'a, const MINIFY: bool> Gen for StaticBlock<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for StaticBlock<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_str("static"); p.print_soft_space(); @@ -2468,8 +2464,8 @@ impl<'a, const MINIFY: bool> Gen for StaticBlock<'a> { } } -impl<'a, const MINIFY: bool> Gen for MethodDefinition<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for MethodDefinition<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); self.decorators.gen(p, ctx); @@ -2531,8 +2527,8 @@ impl<'a, const MINIFY: bool> Gen for MethodDefinition<'a> { } } -impl<'a, const MINIFY: bool> Gen for PropertyDefinition<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for PropertyDefinition<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); self.decorators.gen(p, ctx); if self.declare { @@ -2574,8 +2570,8 @@ impl<'a, const MINIFY: bool> Gen for PropertyDefinition<'a> { } } -impl<'a, const MINIFY: bool> Gen for AccessorProperty<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AccessorProperty<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); if self.r#type.is_abstract() { p.print_str("abstract "); @@ -2601,16 +2597,16 @@ impl<'a, const MINIFY: bool> Gen for AccessorProperty<'a> { } } -impl<'a, const MINIFY: bool> Gen for PrivateIdentifier<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for PrivateIdentifier<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { p.add_source_mapping_for_name(self.span, &self.name); p.print_char(b'#'); p.print_str(self.name.as_str()); } } -impl<'a, const MINIFY: bool> Gen for BindingPattern<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for BindingPattern<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match &self.kind { BindingPatternKind::BindingIdentifier(ident) => ident.gen(p, ctx), BindingPatternKind::ObjectPattern(pattern) => pattern.gen(p, ctx), @@ -2628,8 +2624,8 @@ impl<'a, const MINIFY: bool> Gen for BindingPattern<'a> { } } -impl<'a, const MINIFY: bool> Gen for ObjectPattern<'a> { - fn gen(&self, p: &mut Codegen, ctx: Context) { +impl<'a> Gen for ObjectPattern<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_char(b'{'); p.print_soft_space(); @@ -2646,8 +2642,8 @@ impl<'a, const MINIFY: bool> Gen for ObjectPattern<'a> { } } -impl<'a, const MINIFY: bool> Gen for BindingProperty<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for BindingProperty<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); if self.computed { p.print_char(b'['); @@ -2688,16 +2684,16 @@ impl<'a, const MINIFY: bool> Gen for BindingProperty<'a> { } } -impl<'a, const MINIFY: bool> Gen for BindingRestElement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for BindingRestElement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_ellipsis(); self.argument.gen(p, ctx); } } -impl<'a, const MINIFY: bool> Gen for ArrayPattern<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for ArrayPattern<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.add_source_mapping(self.span.start); p.print_char(b'['); for (index, item) in self.elements.iter().enumerate() { @@ -2721,8 +2717,8 @@ impl<'a, const MINIFY: bool> Gen for ArrayPattern<'a> { } } -impl<'a, const MINIFY: bool> Gen for AssignmentPattern<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for AssignmentPattern<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.left.gen(p, ctx); p.print_soft_space(); p.print_equal(); @@ -2731,8 +2727,8 @@ impl<'a, const MINIFY: bool> Gen for AssignmentPattern<'a> { } } -impl<'a, const MINIFY: bool> Gen for Vec<'a, Decorator<'a>> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for Vec<'a, Decorator<'a>> { + fn gen(&self, p: &mut Codegen, ctx: Context) { for decorator in self { decorator.gen(p, ctx); p.print_hard_space(); @@ -2740,8 +2736,8 @@ impl<'a, const MINIFY: bool> Gen for Vec<'a, Decorator<'a>> { } } -impl<'a, const MINIFY: bool> Gen for Decorator<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl<'a> Gen for Decorator<'a> { + fn gen(&self, p: &mut Codegen, _ctx: Context) { fn need_wrap(expr: &Expression) -> bool { match expr { // "@foo" @@ -2767,8 +2763,8 @@ impl<'a, const MINIFY: bool> Gen for Decorator<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSClassImplements<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSClassImplements<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.expression.gen(p, ctx); if let Some(type_parameters) = self.type_parameters.as_ref() { type_parameters.gen(p, ctx); @@ -2776,22 +2772,22 @@ impl<'a, const MINIFY: bool> Gen for TSClassImplements<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypeParameterDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeParameterDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("<"); p.print_list(&self.params, ctx); p.print_str(">"); } } -impl<'a, const MINIFY: bool> Gen for TSTypeAnnotation<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeAnnotation<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.type_annotation.gen(p, ctx); } } -impl<'a, const MINIFY: bool> Gen for TSType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::TSFunctionType(ty) => ty.gen(p, ctx), Self::TSConstructorType(ty) => ty.gen(p, ctx), @@ -2835,23 +2831,23 @@ impl<'a, const MINIFY: bool> Gen for TSType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSArrayType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSArrayType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.element_type.gen(p, ctx); p.print_str("[]"); } } -impl<'a, const MINIFY: bool> Gen for TSTupleType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTupleType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("["); p.print_list(&self.element_types, ctx); p.print_str("]"); } } -impl<'a, const MINIFY: bool> Gen for TSUnionType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSUnionType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.types.len() == 1 { self.types[0].gen(p, ctx); return; @@ -2867,16 +2863,16 @@ impl<'a, const MINIFY: bool> Gen for TSUnionType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSParenthesizedType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSParenthesizedType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_char(b'('); self.type_annotation.gen(p, ctx); p.print_char(b')'); } } -impl<'a, const MINIFY: bool> Gen for TSIntersectionType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSIntersectionType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.types.len() == 1 { self.types[0].gen(p, ctx); return; @@ -2892,8 +2888,8 @@ impl<'a, const MINIFY: bool> Gen for TSIntersectionType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSConditionalType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSConditionalType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.check_type.gen(p, ctx); p.print_str(" extends "); self.extends_type.gen(p, ctx); @@ -2904,15 +2900,15 @@ impl<'a, const MINIFY: bool> Gen for TSConditionalType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSInferType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSInferType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("infer "); self.type_parameter.gen(p, ctx); } } -impl<'a, const MINIFY: bool> Gen for TSIndexedAccessType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSIndexedAccessType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.object_type.gen(p, ctx); p.print_str("["); self.index_type.gen(p, ctx); @@ -2920,8 +2916,8 @@ impl<'a, const MINIFY: bool> Gen for TSIndexedAccessType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSMappedType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSMappedType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("{"); match self.readonly { TSMappedTypeModifierOperator::True => { @@ -2973,16 +2969,16 @@ impl<'a, const MINIFY: bool> Gen for TSMappedType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSQualifiedName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSQualifiedName<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.left.gen(p, ctx); p.print_str("."); self.right.gen(p, ctx); } } -impl<'a, const MINIFY: bool> Gen for TSTypeOperator<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeOperator<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self.operator { TSTypeOperatorOperator::Keyof => { p.print_str("keyof "); @@ -2998,8 +2994,8 @@ impl<'a, const MINIFY: bool> Gen for TSTypeOperator<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypePredicate<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypePredicate<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.asserts { p.print_str("asserts "); } @@ -3018,8 +3014,8 @@ impl<'a, const MINIFY: bool> Gen for TSTypePredicate<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypeReference<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeReference<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.type_name.gen(p, ctx); if let Some(type_parameters) = &self.type_parameters { type_parameters.gen(p, ctx); @@ -3027,8 +3023,8 @@ impl<'a, const MINIFY: bool> Gen for TSTypeReference<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSDocNullableType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSDocNullableType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.postfix { self.type_annotation.gen(p, ctx); p.print_str("?"); @@ -3039,8 +3035,8 @@ impl<'a, const MINIFY: bool> Gen for JSDocNullableType<'a> { } } -impl<'a, const MINIFY: bool> Gen for JSDocNonNullableType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for JSDocNonNullableType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.postfix { self.type_annotation.gen(p, ctx); p.print_str("!"); @@ -3051,8 +3047,8 @@ impl<'a, const MINIFY: bool> Gen for JSDocNonNullableType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTemplateLiteralType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTemplateLiteralType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("`"); for (index, item) in self.quasis.iter().enumerate() { if index != 0 { @@ -3068,8 +3064,8 @@ impl<'a, const MINIFY: bool> Gen for TSTemplateLiteralType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypeLiteral<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeLiteral<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { let single_line = self.members.len() <= 1; p.print_curly_braces(self.span, single_line, |p| { for item in &self.members { @@ -3084,8 +3080,8 @@ impl<'a, const MINIFY: bool> Gen for TSTypeLiteral<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypeName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeName<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::IdentifierReference(ident) => { ident.gen(p, ctx); @@ -3099,8 +3095,8 @@ impl<'a, const MINIFY: bool> Gen for TSTypeName<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSLiteral<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSLiteral<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::BooleanLiteral(decl) => decl.gen(p, ctx), Self::NullLiteral(decl) => decl.gen(p, ctx), @@ -3114,8 +3110,8 @@ impl<'a, const MINIFY: bool> Gen for TSLiteral<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypeParameter<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeParameter<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.r#const { p.print_str("const "); } @@ -3131,8 +3127,8 @@ impl<'a, const MINIFY: bool> Gen for TSTypeParameter<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSFunctionType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSFunctionType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if let Some(type_parameters) = &self.type_parameters { type_parameters.gen(p, ctx); } @@ -3153,8 +3149,8 @@ impl<'a, const MINIFY: bool> Gen for TSFunctionType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSThisParameter<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSThisParameter<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.this.gen(p, ctx); if let Some(type_annotation) = &self.type_annotation { p.print_str(": "); @@ -3163,8 +3159,8 @@ impl<'a, const MINIFY: bool> Gen for TSThisParameter<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSSignature<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSSignature<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::TSIndexSignature(signature) => signature.gen(p, ctx), Self::TSPropertySignature(signature) => { @@ -3280,8 +3276,8 @@ impl<'a, const MINIFY: bool> Gen for TSSignature<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypeQuery<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeQuery<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("typeof "); self.expr_name.gen(p, ctx); if let Some(type_params) = &self.type_parameters { @@ -3290,8 +3286,8 @@ impl<'a, const MINIFY: bool> Gen for TSTypeQuery<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypeQueryExprName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeQueryExprName<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { match_ts_type_name!(Self) => self.to_ts_type_name().gen(p, ctx), Self::TSImportType(decl) => decl.gen(p, ctx), @@ -3299,8 +3295,8 @@ impl<'a, const MINIFY: bool> Gen for TSTypeQueryExprName<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSImportType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSImportType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.is_type_of { p.print_str("typeof "); } @@ -3321,8 +3317,8 @@ impl<'a, const MINIFY: bool> Gen for TSImportType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSImportAttributes<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSImportAttributes<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_char(b'{'); p.print_soft_space(); self.attributes_keyword.gen(p, ctx); @@ -3338,16 +3334,16 @@ impl<'a, const MINIFY: bool> Gen for TSImportAttributes<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSImportAttribute<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSImportAttribute<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.name.gen(p, ctx); p.print_str(": "); self.value.gen_expr(p, Precedence::Member, ctx); } } -impl<'a, const MINIFY: bool> Gen for TSImportAttributeName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSImportAttributeName<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { TSImportAttributeName::Identifier(ident) => ident.gen(p, ctx), TSImportAttributeName::StringLiteral(literal) => literal.gen(p, ctx), @@ -3355,16 +3351,16 @@ impl<'a, const MINIFY: bool> Gen for TSImportAttributeName<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypeParameterInstantiation<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeParameterInstantiation<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("<"); p.print_list(&self.params, ctx); p.print_str(">"); } } -impl<'a, const MINIFY: bool> Gen for TSIndexSignature<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSIndexSignature<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.readonly { p.print_str("readonly "); } @@ -3385,8 +3381,8 @@ impl<'a, const MINIFY: bool> Gen for TSIndexSignature<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTupleElement<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTupleElement<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { match_ts_type!(TSTupleElement) => self.to_ts_type().gen(p, ctx), TSTupleElement::TSOptionalType(ts_type) => { @@ -3401,8 +3397,8 @@ impl<'a, const MINIFY: bool> Gen for TSTupleElement<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSNamedTupleMember<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSNamedTupleMember<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.label.gen(p, ctx); if self.optional { p.print_str("?"); @@ -3413,8 +3409,8 @@ impl<'a, const MINIFY: bool> Gen for TSNamedTupleMember<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSModuleDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSModuleDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.declare { p.print_str("declare "); } @@ -3450,8 +3446,8 @@ impl<'a, const MINIFY: bool> Gen for TSModuleDeclaration<'a> { } } -impl Gen for TSModuleDeclarationKind { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _: Context) { +impl Gen for TSModuleDeclarationKind { + fn gen(&self, p: &mut Codegen, _: Context) { match self { TSModuleDeclarationKind::Global => { p.print_str("global"); @@ -3466,8 +3462,8 @@ impl Gen for TSModuleDeclarationKind { } } -impl<'a, const MINIFY: bool> Gen for TSModuleDeclarationName<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSModuleDeclarationName<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::Identifier(ident) => ident.gen(p, ctx), Self::StringLiteral(s) => s.gen(p, ctx), @@ -3475,8 +3471,8 @@ impl<'a, const MINIFY: bool> Gen for TSModuleDeclarationName<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSModuleBlock<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSModuleBlock<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { let is_empty = self.directives.is_empty() && self.body.is_empty(); p.print_curly_braces(self.span, is_empty, |p| { for directive in &self.directives { @@ -3491,8 +3487,8 @@ impl<'a, const MINIFY: bool> Gen for TSModuleBlock<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSTypeAliasDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSTypeAliasDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.declare { p.print_str("declare "); } @@ -3509,8 +3505,8 @@ impl<'a, const MINIFY: bool> Gen for TSTypeAliasDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSInterfaceDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSInterfaceDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("interface"); p.print_hard_space(); self.id.gen(p, ctx); @@ -3535,8 +3531,8 @@ impl<'a, const MINIFY: bool> Gen for TSInterfaceDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSInterfaceHeritage<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSInterfaceHeritage<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { self.expression.gen_expr(p, Precedence::Call, ctx); if let Some(type_parameters) = &self.type_parameters { type_parameters.gen(p, ctx); @@ -3544,8 +3540,8 @@ impl<'a, const MINIFY: bool> Gen for TSInterfaceHeritage<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSEnumDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSEnumDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_indent(); if self.declare { p.print_str("declare "); @@ -3568,8 +3564,8 @@ impl<'a, const MINIFY: bool> Gen for TSEnumDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSEnumMember<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSEnumMember<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match &self.id { TSEnumMemberName::StaticIdentifier(decl) => decl.gen(p, ctx), TSEnumMemberName::StaticStringLiteral(decl) => decl.gen(p, ctx), @@ -3590,8 +3586,8 @@ impl<'a, const MINIFY: bool> Gen for TSEnumMember<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSConstructorType<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSConstructorType<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { if self.r#abstract { p.print_str("abstract "); } @@ -3609,8 +3605,8 @@ impl<'a, const MINIFY: bool> Gen for TSConstructorType<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSImportEqualsDeclaration<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSImportEqualsDeclaration<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { p.print_str("import "); self.id.gen(p, ctx); p.print_str(" = "); @@ -3618,8 +3614,8 @@ impl<'a, const MINIFY: bool> Gen for TSImportEqualsDeclaration<'a> { } } -impl<'a, const MINIFY: bool> Gen for TSModuleReference<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) { +impl<'a> Gen for TSModuleReference<'a> { + fn gen(&self, p: &mut Codegen, ctx: Context) { match self { Self::ExternalModuleReference(decl) => { p.print_str("require("); @@ -3631,8 +3627,8 @@ impl<'a, const MINIFY: bool> Gen for TSModuleReference<'a> { } } -impl Gen for TSAccessibility { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { +impl Gen for TSAccessibility { + fn gen(&self, p: &mut Codegen, _ctx: Context) { match self { Self::Public => p.print_str("public "), Self::Private => p.print_str("private "), diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index 5a33dc1720a39..6c6b23d3944f6 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -34,15 +34,14 @@ pub use crate::{ }; /// Code generator without whitespace removal. -pub type CodeGenerator<'a> = Codegen<'a, false>; - -/// Code generator with whitespace removal. -pub type WhitespaceRemover<'a> = Codegen<'a, true>; +pub type CodeGenerator<'a> = Codegen<'a>; #[derive(Default, Clone, Copy)] pub struct CodegenOptions { /// Use single quotes instead of double quotes. pub single_quote: bool, + + pub minify: bool, } #[derive(Default, Clone, Copy)] @@ -56,7 +55,7 @@ pub struct CodegenReturn { pub source_map: Option, } -pub struct Codegen<'a, const MINIFY: bool> { +pub struct Codegen<'a> { options: CodegenOptions, comment_options: CommentOptions, @@ -97,26 +96,26 @@ pub struct Codegen<'a, const MINIFY: bool> { latest_consumed_comment_end: u32, } -impl<'a, const MINIFY: bool> Default for Codegen<'a, MINIFY> { +impl<'a> Default for Codegen<'a> { fn default() -> Self { Self::new() } } -impl<'a, const MINIFY: bool> From> for String { - fn from(mut val: Codegen<'a, MINIFY>) -> Self { +impl<'a> From> for String { + fn from(mut val: Codegen<'a>) -> Self { val.into_source_text() } } -impl<'a, const MINIFY: bool> From> for Cow<'a, str> { - fn from(mut val: Codegen<'a, MINIFY>) -> Self { +impl<'a> From> for Cow<'a, str> { + fn from(mut val: Codegen<'a>) -> Self { Cow::Owned(val.into_source_text()) } } // Public APIs -impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { +impl<'a> Codegen<'a> { #[must_use] pub fn new() -> Self { Self { @@ -147,7 +146,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { /// Minification will reduce by at least half of the original size. #[must_use] pub fn with_capacity(mut self, source_text_len: usize) -> Self { - let capacity = if MINIFY { source_text_len / 2 } else { source_text_len }; + let capacity = if self.options.minify { source_text_len / 2 } else { source_text_len }; self.code = Vec::with_capacity(capacity); self } @@ -215,7 +214,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { } // Private APIs -impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { +impl<'a> Codegen<'a> { fn code(&self) -> &Vec { &self.code } @@ -226,7 +225,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { #[inline] fn print_soft_space(&mut self) { - if !MINIFY { + if !self.options.minify { self.print_char(b' '); } } @@ -238,7 +237,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { #[inline] fn print_soft_newline(&mut self) { - if !MINIFY { + if !self.options.minify { self.print_char(b'\n'); } } @@ -271,21 +270,21 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { #[inline] fn indent(&mut self) { - if !MINIFY { + if !self.options.minify { self.indent += 1; } } #[inline] fn dedent(&mut self) { - if !MINIFY { + if !self.options.minify { self.indent -= 1; } } #[inline] fn print_indent(&mut self) { - if MINIFY { + if self.options.minify { return; } if self.print_next_indent_as_space { @@ -298,7 +297,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { #[inline] fn print_semicolon_after_statement(&mut self) { - if MINIFY { + if self.options.minify { self.needs_semicolon = true; } else { self.print_str(";\n"); @@ -328,7 +327,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { self.print_char(b'='); } - fn print_sequence>(&mut self, items: &[T], ctx: Context) { + fn print_sequence(&mut self, items: &[T], ctx: Context) { for item in items { item.gen(self, ctx); self.print_comma(); @@ -377,7 +376,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { self.print_soft_newline(); } stmt => { - if need_space && MINIFY { + if need_space && self.options.minify { self.print_hard_space(); } self.print_next_indent_as_space = true; @@ -396,7 +395,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { self.needs_semicolon = false; } - fn print_list>(&mut self, items: &[T], ctx: Context) { + fn print_list(&mut self, items: &[T], ctx: Context) { for (index, item) in items.iter().enumerate() { if index != 0 { self.print_comma(); @@ -411,12 +410,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { expr.gen_expr(self, Precedence::Lowest, Context::empty()); } - fn print_expressions>( - &mut self, - items: &[T], - precedence: Precedence, - ctx: Context, - ) { + fn print_expressions(&mut self, items: &[T], precedence: Precedence, ctx: Context) { for (index, item) in items.iter().enumerate() { if index != 0 { self.print_comma(); @@ -513,7 +507,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { } // Comment related -impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> { +impl<'a> Codegen<'a> { /// Avoid issue related to rustc borrow checker . /// Since if you want to print a range of source code, you need to borrow the source code /// as immutable first, and call the [Self::print_str] which is a mutable borrow. diff --git a/crates/oxc_codegen/tests/integration/tester.rs b/crates/oxc_codegen/tests/integration/tester.rs index f57c89adb8cb1..cc2955aa28516 100644 --- a/crates/oxc_codegen/tests/integration/tester.rs +++ b/crates/oxc_codegen/tests/integration/tester.rs @@ -1,5 +1,5 @@ use oxc_allocator::Allocator; -use oxc_codegen::{CodeGenerator, CommentOptions, WhitespaceRemover}; +use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions}; use oxc_parser::Parser; use oxc_span::SourceType; @@ -25,7 +25,10 @@ pub fn test_minify(source_text: &str, expected: &str) { let source_type = SourceType::default().with_module(true).with_jsx(true); let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let result = WhitespaceRemover::new().build(&ret.program).source_text; + let result = CodeGenerator::new() + .with_options(CodegenOptions { minify: true, ..CodegenOptions::default() }) + .build(&ret.program) + .source_text; assert_eq!( result, expected, "\nfor minify source {source_text}\nexpect {expected}\ngot {result:?}" diff --git a/crates/oxc_codegen/tests/integration/ts.rs b/crates/oxc_codegen/tests/integration/ts.rs index b5552c09c178b..326e0da8efc09 100644 --- a/crates/oxc_codegen/tests/integration/ts.rs +++ b/crates/oxc_codegen/tests/integration/ts.rs @@ -10,7 +10,7 @@ fn codegen(source_text: &str) -> String { let source_type = SourceType::default().with_typescript(true).with_module(true); let ret = Parser::new(&allocator, source_text, source_type).parse(); CodeGenerator::new() - .with_options(CodegenOptions { single_quote: true }) + .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) .build(&ret.program) .source_text } diff --git a/crates/oxc_linter/src/fixer/mod.rs b/crates/oxc_linter/src/fixer/mod.rs index 0947d08feb6a9..d1562ef6f6489 100644 --- a/crates/oxc_linter/src/fixer/mod.rs +++ b/crates/oxc_linter/src/fixer/mod.rs @@ -170,7 +170,8 @@ impl<'c, 'a: 'c> RuleFixer<'c, 'a> { #[allow(clippy::unused_self)] pub fn codegen(self) -> CodeGenerator<'a> { - CodeGenerator::new().with_options(CodegenOptions { single_quote: true }) + CodeGenerator::new() + .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) } #[allow(clippy::unused_self)] diff --git a/crates/oxc_minifier/tests/mod.rs b/crates/oxc_minifier/tests/mod.rs index 3f72d4cdf2144..03b1b40625b5a 100644 --- a/crates/oxc_minifier/tests/mod.rs +++ b/crates/oxc_minifier/tests/mod.rs @@ -26,7 +26,7 @@ fn run(source_text: &str, source_type: SourceType, options: Option TransformContext<'a> { self.program.borrow_mut() } - pub fn codegen(&self) -> Codegen<'a, MINIFY> { - let codegen = Codegen::::new(); + pub fn codegen(&self) -> Codegen<'a> { + let codegen = Codegen::new(); if self.source_map { codegen.enable_source_map(self.file_name(), self.source_text()) } else { diff --git a/napi/transform/src/isolated_declaration.rs b/napi/transform/src/isolated_declaration.rs index e5b8cebed31ae..c9609bfde3067 100644 --- a/napi/transform/src/isolated_declaration.rs +++ b/napi/transform/src/isolated_declaration.rs @@ -32,5 +32,5 @@ pub fn isolated_declaration(filename: String, source_text: String) -> IsolatedDe pub(crate) fn build_declarations(ctx: &TransformContext<'_>) -> CodegenReturn { let transformed_ret = IsolatedDeclarations::new(ctx.allocator).build(&ctx.program()); ctx.add_diagnostics(transformed_ret.errors); - ctx.codegen::().build(&transformed_ret.program) + ctx.codegen().build(&transformed_ret.program) } diff --git a/napi/transform/src/transformer.rs b/napi/transform/src/transformer.rs index ca0b39d287513..6bdbc9de64818 100644 --- a/napi/transform/src/transformer.rs +++ b/napi/transform/src/transformer.rs @@ -108,5 +108,5 @@ fn transpile(ctx: &TransformContext<'_>) -> CodegenReturn { .build(&mut ctx.program_mut()); ctx.add_diagnostics(ret.errors); - ctx.codegen::().build(&ctx.program()) + ctx.codegen().build(&ctx.program()) } diff --git a/tasks/minsize/src/lib.rs b/tasks/minsize/src/lib.rs index 7bb3101905b7b..b976a26fc5663 100644 --- a/tasks/minsize/src/lib.rs +++ b/tasks/minsize/src/lib.rs @@ -8,7 +8,7 @@ use std::{ use flate2::{write::GzEncoder, Compression}; use humansize::{format_size, DECIMAL}; use oxc_allocator::Allocator; -use oxc_codegen::WhitespaceRemover; +use oxc_codegen::{CodeGenerator, CodegenOptions}; use oxc_minifier::{CompressOptions, Minifier, MinifierOptions}; use oxc_parser::Parser; use oxc_span::SourceType; @@ -111,7 +111,11 @@ fn minify(source_text: &str, source_type: SourceType, options: MinifierOptions) let ret = Parser::new(&allocator, source_text, source_type).parse(); let program = allocator.alloc(ret.program); let ret = Minifier::new(options).build(&allocator, program); - WhitespaceRemover::new().with_mangler(ret.mangler).build(program).source_text + CodeGenerator::new() + .with_options(CodegenOptions { minify: true, ..CodegenOptions::default() }) + .with_mangler(ret.mangler) + .build(program) + .source_text } fn gzip_size(s: &str) -> usize {