Skip to content

Commit

Permalink
oxc_codegen - build
Browse files Browse the repository at this point in the history
  • Loading branch information
topheman committed Oct 30, 2024
1 parent 11eebb2 commit 75288a0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/oxc/explore.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@

<https://github.com/oxc-project/oxc>

## oxc_ast

### Printing an AST

For this one, you will need to read [`oxc_codegen`](#oxc_codegen) before.

## oxc_codegen

[`oxc:oxc_codegen::Codegen`](https://github.com/oxc-project/oxc/blob/main/crates/oxc_codegen/src/lib.rs) is the struct that holds everything needed to transform an [`oxc::oxc_ast::ast::Program`](https://github.com/oxc-project/oxc/blob/main/crates/oxc_ast/src/ast/js.rs) into a `CodegenReturn` thanks to its `build` method.

The `CodegenReturn` contains:

- `code: String` (the code generated from the ast)
- `map: Option<oxc_sourcemap::SourceMap>` (the sourcemap if activated)

### Codegen::build

- prepares a buffer for the code that will be generated - `self.code.reserve(program.source_text.len())`
- creates a [`HashMap`](https://github.com/oxc-project/oxc/blob/main/crates/oxc_codegen/src/comment.rs) of the comments contained in the AST (if the comments are to be printed - like not in minified code)
- creates a [`oxc::oxc_codegen::SourcemapBuilder`](https://github.com/oxc-project/oxc/blob/main/crates/oxc_codegen/src/sourcemap_builder.rs) (if sourcemaps are active)

Finally, calls the `print` method on the AST, passing itself `&mut Codegen` and a default [`oxc::oxc_codegen::Context`](https://github.com/oxc-project/oxc/blob/main/crates/oxc_codegen/src/context.rs) which will be passed to each `print` calls of each AST node.

See the follow-up in [`oxc_ast`](#oxc_ast).

See more about sourcemaps on [`oxc::oxc_sourcemaps`](#oxc_sourcemap).

<a href="https://github.com/oxc-project/oxc/blob/main/crates/oxc_codegen/src/lib.rs" title="Source Code of oxc_codegen">📄</a>
Expand Down

0 comments on commit 75288a0

Please sign in to comment.