Skip to content

Commit

Permalink
feat(rust): add print in OxcCompiler (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Nov 15, 2023
1 parent aea91ab commit ad962af
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT"
repository = "https://github.com/rolldown-rs/rolldown"

[workspace.dependencies]
oxc = { version = "0.3.0", features = ["semantic"] }
oxc = { version = "0.3.0" }
oxc_resolver = { version = "0.5.1" }
sugar_path = "0.0.12"
tokio = { version = "1.33.0", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion crates/rolldown_oxc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
oxc = { workspace = true }
oxc = { workspace = true, features = ["semantic", "codegen"] }

smallvec = { workspace = true }
13 changes: 12 additions & 1 deletion crates/rolldown_oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{fmt::Debug, pin::Pin};
use oxc::{
allocator::Allocator,
ast::ast,
codegen::{Codegen, CodegenOptions},
parser::Parser,
semantic::{Semantic, SemanticBuilder},
span::SourceType,
Expand Down Expand Up @@ -54,5 +55,15 @@ impl OxcCompiler {
OxcProgram { program, source, allocator }
}

pub fn print() {}
pub fn print(ast: &OxcProgram) -> String {
let codegen = Codegen::<false>::new(ast.source().len(), CodegenOptions);
codegen.build(&ast.program)
}
}

#[test]
fn basic_test() {
let ast = OxcCompiler::parse("const a = 1;".to_string(), SourceType::default());
let code = OxcCompiler::print(&ast);
assert_eq!(code, "const a = 1;\n");
}

0 comments on commit ad962af

Please sign in to comment.