Skip to content

Commit 6d23ff3

Browse files
committed
refactor: add a lib
1 parent 8196deb commit 6d23ff3

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

benches/parser.rs

+4-23
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,19 @@ impl TheBencher for OxcBencher {
4444
const ID: &'static str = "oxc";
4545

4646
fn parse(path: &Path, source: &str) -> Self::ParseOutput {
47-
let allocator = oxc::allocator::Allocator::default();
48-
let source_type = oxc::span::SourceType::from_path(path).unwrap();
49-
_ = oxc::parser::Parser::new(&allocator, source, source_type).parse();
50-
allocator
47+
bench_parser::oxc::parse(path, source)
5148
}
5249
}
5350

5451
struct SwcBencher;
5552

5653
impl TheBencher for SwcBencher {
57-
type ParseOutput = swc_ecma_parser::PResult<swc_ecma_ast::Module>;
54+
type ParseOutput = swc_ecma_ast::Module;
5855

5956
const ID: &'static str = "swc";
6057

6158
fn parse(path: &Path, source: &str) -> Self::ParseOutput {
62-
use swc_ecma_parser::{EsConfig, Parser, StringInput, Syntax, TsConfig};
63-
let syntax = match path.extension().unwrap().to_str().unwrap() {
64-
"js" => Syntax::Es(EsConfig::default()),
65-
"tsx" => Syntax::Typescript(TsConfig {
66-
tsx: true,
67-
..TsConfig::default()
68-
}),
69-
_ => panic!("need to define syntax for swc"),
70-
};
71-
Parser::new(
72-
syntax,
73-
StringInput::new(source, Default::default(), Default::default()),
74-
None,
75-
)
76-
.parse_module()
59+
bench_parser::swc::parse(path, source)
7760
}
7861
}
7962

@@ -85,9 +68,7 @@ impl TheBencher for BiomeBencher {
8568
const ID: &'static str = "biome";
8669

8770
fn parse(path: &Path, source: &str) -> Self::ParseOutput {
88-
let options = biome_js_parser::JsParserOptions::default();
89-
let source_type = biome_js_syntax::JsFileSource::try_from(path).unwrap();
90-
biome_js_parser::parse(source, source_type, options)
71+
bench_parser::biome::parse(path, source)
9172
}
9273
}
9374

src/lib.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
pub mod oxc {
2+
use std::path::Path;
3+
4+
use oxc::{allocator::Allocator, parser::Parser, span::SourceType};
5+
6+
pub fn parse(path: &Path, source: &str) -> Allocator {
7+
let allocator = Allocator::default();
8+
let source_type = SourceType::from_path(path).unwrap();
9+
_ = Parser::new(&allocator, source, source_type).parse();
10+
allocator
11+
}
12+
}
13+
14+
pub mod swc {
15+
use std::path::Path;
16+
17+
use swc_ecma_ast::Module;
18+
use swc_ecma_parser::{EsConfig, Parser, StringInput, Syntax, TsConfig};
19+
20+
pub fn parse(path: &Path, source: &str) -> Module {
21+
let syntax = match path.extension().unwrap().to_str().unwrap() {
22+
"js" => Syntax::Es(EsConfig::default()),
23+
"tsx" => Syntax::Typescript(TsConfig {
24+
tsx: true,
25+
..TsConfig::default()
26+
}),
27+
_ => panic!("need to define syntax for swc"),
28+
};
29+
let input = StringInput::new(source, Default::default(), Default::default());
30+
Parser::new(syntax, input, None).parse_module().unwrap()
31+
}
32+
}
33+
34+
pub mod biome {
35+
use std::path::Path;
36+
37+
use biome_js_parser::{JsParserOptions, Parse};
38+
use biome_js_syntax::{AnyJsRoot, JsFileSource};
39+
40+
pub fn parse(path: &Path, source: &str) -> Parse<AnyJsRoot> {
41+
let options = JsParserOptions::default();
42+
let source_type = JsFileSource::try_from(path).unwrap();
43+
biome_js_parser::parse(source, source_type, options)
44+
}
45+
}

0 commit comments

Comments
 (0)