refactor(Formatter): remove minification logic

This commit is contained in:
Boshen 2023-05-08 22:57:05 +08:00
parent fb6fdfa3ae
commit 0f57a512f5
No known key found for this signature in database
GPG key ID: 9C7A8C8AB22BEBD1
4 changed files with 24 additions and 110 deletions

1
Cargo.lock generated
View file

@ -1080,7 +1080,6 @@ dependencies = [
"oxc_allocator",
"oxc_ast",
"oxc_parser",
"oxc_semantic",
"oxc_syntax",
]

View file

@ -12,7 +12,6 @@ repository.workspace = true
[dependencies]
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_semantic = { workspace = true }
oxc_syntax = { workspace = true }
[dev_dependencies]

View file

@ -1,8 +1,7 @@
use oxc_allocator::{Box, Vec};
#[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*;
use oxc_semantic::Symbol;
use oxc_syntax::operator::{BinaryOperator, Operator};
use oxc_syntax::operator::BinaryOperator;
use crate::{Formatter, Separator};
@ -123,9 +122,9 @@ fn print_if(if_stmt: &IfStatement<'_>, p: &mut Formatter) {
}
if let Some(alternate) = if_stmt.alternate.as_ref() {
p.print_semicolon_if_needed();
p.print(b' ');
p.print_space();
p.print_str(b"else");
p.print(b' ');
p.print_space();
match alternate {
Statement::BlockStatement(block) => {
p.print_block1(block);
@ -214,9 +213,9 @@ fn gen_for_statement_brack_content<'a>(
p.print_space();
p.print(b'(');
left.gen(p);
p.print(b' ');
p.print_space();
p.print_str(key);
p.print(b' ');
p.print_space();
right.gen(p);
p.print(b')');
p.print_body(body);
@ -247,7 +246,7 @@ impl<'a> Gen for DoWhileStatement<'a> {
fn gen(&self, p: &mut Formatter) {
p.print_indent();
p.print_str(b"do");
p.print(b' ');
p.print_space();
if let Statement::BlockStatement(block) = &self.body {
p.print_space();
p.print_block1(block);
@ -323,7 +322,7 @@ impl<'a> Gen for SwitchCase<'a> {
match &self.test {
Some(test) => {
p.print_str(b"case");
p.print(b' ');
p.print_space();
test.gen(p);
}
None => p.print_str(b"default"),
@ -344,7 +343,7 @@ impl<'a> Gen for ReturnStatement<'a> {
p.print_indent();
p.print_str(b"return");
if let Some(arg) = &self.argument {
p.print(b' ');
p.print_space();
arg.gen(p);
}
p.print_semicolon_after_statement();
@ -393,7 +392,7 @@ impl<'a> Gen for ThrowStatement<'a> {
fn gen(&self, p: &mut Formatter) {
p.print_indent();
p.print_str(b"throw");
p.print(b' ');
p.print_space();
self.argument.gen(p);
p.print_semicolon_after_statement();
}
@ -465,7 +464,7 @@ impl<'a> Gen for VariableDeclaration<'a> {
VariableDeclarationKind::Let => b"let",
VariableDeclarationKind::Var => b"var",
});
p.print(b' ');
p.print_space();
p.print_list(&self.declarations);
}
}
@ -486,7 +485,7 @@ impl<'a> Gen for Function<'a> {
fn gen(&self, p: &mut Formatter) {
if self.r#async {
p.print_str(b"async");
p.print(b' ');
p.print_space();
}
p.print_str(b"function");
if self.generator {
@ -494,7 +493,7 @@ impl<'a> Gen for Function<'a> {
}
if let Some(id) = &self.id {
if !self.generator {
p.print(b' ');
p.print_space();
}
id.gen(p);
p.print_space();
@ -786,17 +785,7 @@ impl<'a> Gen for Expression<'a> {
impl Gen for IdentifierReference {
fn gen(&self, p: &mut Formatter) {
let symbols = &p.symbols;
let slot = symbols
.get_resolved_reference_for_id(self)
.map(|r| symbols[r.resolved_symbol_id].slot())
.filter(|slot| slot.is_some());
if let Some(slot) = slot {
let name = symbols.mangler().mangled_name(slot);
p.print_str(name.as_bytes());
} else {
p.print_str(self.name.as_bytes());
}
p.print_str(self.name.as_bytes());
}
}
@ -808,15 +797,7 @@ impl Gen for IdentifierName {
impl Gen for BindingIdentifier {
fn gen(&self, p: &mut Formatter) {
let symbols = &p.symbols;
let slot =
symbols.get_symbol_by_span(self.span).map(Symbol::slot).filter(|slot| slot.is_some());
if let Some(slot) = slot {
let name = symbols.mangler().mangled_name(slot);
p.print_str(name.as_bytes());
} else {
p.print_str(self.name.as_bytes());
}
p.print_str(self.name.as_bytes());
}
}
@ -1119,7 +1100,7 @@ impl<'a> Gen for YieldExpression<'a> {
}
if let Some(argument) = self.argument.as_ref() {
p.print(b' ');
p.print_space();
argument.gen(p);
}
}
@ -1129,16 +1110,12 @@ impl<'a> Gen for UpdateExpression<'a> {
fn gen(&self, p: &mut Formatter) {
let operator = self.operator.as_str().as_bytes();
if self.prefix {
p.print_space_before_operator(self.operator.into());
p.print_space();
p.print_str(operator);
p.prev_op = Some(self.operator.into());
p.prev_op_end = p.code().len();
self.argument.gen(p);
} else {
self.argument.gen(p);
p.print_str(operator);
p.prev_op = Some(self.operator.into());
p.prev_op_end = p.code().len();
}
}
}
@ -1148,12 +1125,10 @@ impl<'a> Gen for UnaryExpression<'a> {
let operator = self.operator.as_str().as_bytes();
if self.operator.is_keyword() {
p.print_str(operator);
p.print(b' ');
p.print_space();
} else {
p.print_space_before_operator(self.operator.into());
p.print_space();
p.print_str(operator);
p.prev_op = Some(self.operator.into());
p.prev_op_end = p.code().len();
}
self.argument.gen(p);
}
@ -1171,25 +1146,19 @@ impl Gen for BinaryOperator {
fn gen(&self, p: &mut Formatter) {
let operator = self.as_str().as_bytes();
if self.is_keyword() {
p.print(b' ');
p.print_str(operator);
p.print(b' ');
} else {
let op: Operator = (*self).into();
p.print_space_before_operator(op);
p.print_str(operator);
p.prev_op = Some(op);
p.prev_op_end = p.code().len();
p.print_space();
}
p.print_str(operator);
p.print_space();
}
}
impl<'a> Gen for PrivateInExpression<'a> {
fn gen(&self, p: &mut Formatter) {
self.left.gen(p);
p.print(b' ');
p.print_space();
p.print_str(self.operator.as_str().as_bytes());
p.print(b' ');
p.print_space();
self.right.gen(p);
}
}
@ -1454,7 +1423,7 @@ impl<'a> Gen for Class<'a> {
fn gen(&self, p: &mut Formatter) {
p.print_str(b"class");
if let Some(id) = &self.id {
p.print(b' ');
p.print_space();
id.gen(p);
}
if let Some(super_class) = self.super_class.as_ref() {

View file

@ -5,12 +5,8 @@
mod gen;
use std::rc::Rc;
#[allow(clippy::wildcard_imports)]
use oxc_ast::ast::*;
use oxc_semantic::SymbolTable;
use oxc_syntax::operator::{BinaryOperator, Operator, UnaryOperator, UpdateOperator};
pub use crate::gen::Gen;
@ -28,9 +24,6 @@ impl Default for FormatterOptions {
pub struct Formatter {
options: FormatterOptions,
/// Symbol Table for name mangling
symbols: Rc<SymbolTable>,
/// Output Code
code: Vec<u8>,
@ -39,8 +32,6 @@ pub struct Formatter {
// states
needs_semicolon: bool,
prev_op_end: usize,
prev_op: Option<Operator>,
}
#[derive(Debug, Clone, Copy)]
@ -56,24 +47,12 @@ impl Formatter {
pub fn new(source_len: usize, options: FormatterOptions) -> Self {
Self {
options,
symbols: Rc::new(SymbolTable::default()),
code: Vec::with_capacity(source_len),
indentation: 0,
needs_semicolon: false,
prev_op_end: 0,
prev_op: None,
}
}
#[must_use]
pub fn with_symbol_table(mut self, symbols: &Rc<SymbolTable>, yes: bool) -> Self {
if yes {
symbols.mangle();
self.symbols = Rc::clone(symbols);
}
self
}
#[must_use]
pub fn build(mut self, program: &Program<'_>) -> String {
program.gen(&mut self);
@ -133,38 +112,6 @@ impl Formatter {
self.print(b',');
}
fn print_space_before_operator(&mut self, next: Operator) {
if self.prev_op_end != self.code.len() {
return;
}
let Some(prev) = self.prev_op else { return };
// "+ + y" => "+ +y"
// "+ ++ y" => "+ ++y"
// "x + + y" => "x+ +y"
// "x ++ + y" => "x+++y"
// "x + ++ y" => "x+ ++y"
// "-- >" => "-- >"
// "< ! --" => "<! --"
let bin_op_add = Operator::BinaryOperator(BinaryOperator::Addition);
let bin_op_sub = Operator::BinaryOperator(BinaryOperator::Subtraction);
let un_op_pos = Operator::UnaryOperator(UnaryOperator::UnaryPlus);
let un_op_pre_inc = Operator::UpdateOperator(UpdateOperator::Increment);
let un_op_neg = Operator::UnaryOperator(UnaryOperator::UnaryNegation);
let un_op_pre_dec = Operator::UpdateOperator(UpdateOperator::Decrement);
let un_op_post_dec = Operator::UpdateOperator(UpdateOperator::Decrement);
let bin_op_gt = Operator::BinaryOperator(BinaryOperator::GreaterThan);
let un_op_not = Operator::UnaryOperator(UnaryOperator::LogicalNot);
if ((prev == bin_op_add || prev == un_op_pos)
&& (next == bin_op_add || next == un_op_pos || next == un_op_pre_inc))
|| ((prev == bin_op_sub || prev == un_op_neg)
&& (next == bin_op_sub || next == un_op_neg || next == un_op_pre_dec))
|| (prev == un_op_post_dec && next == bin_op_gt)
|| (prev == un_op_not && next == un_op_pre_dec && self.code().len() > 1/*&& p.js[len(p.js)-2] == '<'*/)
{
self.print(b' ');
}
}
fn print_semicolon_after_statement(&mut self) {
self.print_semicolon();
self.print(b'\n');