Commits
Click on a commit to change the comparison rangefeat(ast)!: generate `ast_builder.rs`. (#3890)
### 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,
);
```