mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 12:51:57 +00:00
### Every structure has 2 builder methods:
1. `xxx` e.g. `block_statement`
```rust
#[inline]
pub fn block_statement(self, span: Span, body: Vec<'a, Statement<'a>>) -> BlockStatement<'a> {
BlockStatement { span, body, scope_id: Default::default() }
}
```
2. `alloc_xxx` e.g. `alloc_block_statement`
```rust
#[inline]
pub fn alloc_block_statement(
self,
span: Span,
body: Vec<'a, Statement<'a>>,
) -> Box<'a, BlockStatement<'a>> {
self.block_statement(span, body).into_in(self.allocator)
}
```
### We generate 3 types of methods for enums:
1. `yyy_xxx` e.g. `statement_block`
```rust
#[inline]
pub fn statement_block(self, span: Span, body: Vec<'a, Statement<'a>>) -> Statement<'a> {
Statement::BlockStatement(self.alloc(self.block_statement(span, body)))
}
```
2. `yyy_from_xxx` e.g. `statement_from_block`
```rust
#[inline]
pub fn statement_from_block<T>(self, inner: T) -> Statement<'a>
where
T: IntoIn<'a, Box<'a, BlockStatement<'a>>>,
{
Statement::BlockStatement(inner.into_in(self.allocator))
}
```
3. `yyy_xxx` where `xxx` is inherited e.g. `statement_declaration`
```rust
#[inline]
pub fn statement_declaration(self, inner: Declaration<'a>) -> Statement<'a> {
Statement::from(inner)
}
```
------------
### Generic parameters:
We no longer accept `Box<'a, ADT>`, `Atom` or `&'a str`, Instead we use `IntoIn<'a, Box<'a, ADT>>`, `IntoIn<'a, Atom<'a>>` and `IntoIn<'a, &'a str>` respectively.
It allows us to rewrite things like this:
```rust
let ident = IdentifierReference::new(SPAN, Atom::from("require"));
let number_literal_expr = self.ast.expression_numeric_literal(
right_expr.span(),
num,
raw,
self.ast.new_str(num.to_string().as_str()),
NumberBase::Decimal,
);
```
As this:
```rust
let ident = IdentifierReference::new(SPAN, "require");
let number_literal_expr = self.ast.expression_numeric_literal(
right_expr.span(),
num,
raw,
num.to_string(),
NumberBase::Decimal,
);
```
|
||
|---|---|---|
| .. | ||
| examples | ||
| src | ||
| tests | ||
| Cargo.toml | ||
| CHANGELOG.md | ||
| README.md | ||
Minifier
A JavaScript minifier has three components:
- printer
- mangler
- compressor
Mangler
The mangler implementation is part of the SymbolTable residing in oxc_semantic.
It is responsible for shortening variables. Its algorithm should be gzip friendly.
The printer is also responsible for printing out the shortened variable names.
Compressor
The compressor is responsible for rewriting statements and expressions for minimal text output. Terser is a good place to start for learning the fundamentals.
Terser Tests
The fixtures are copied from https://github.com/terser/terser/tree/master/test/compress