mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(parser): refactor visitor in regexp example (#8524)
Refactor visitor in example to use `visit_*` methods instead of `enter_node`. This is our recommended style as it's more performant.
This commit is contained in:
parent
1860411656
commit
2857ae15cd
1 changed files with 43 additions and 47 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
use std::{env, fs, path::Path, sync::Arc};
|
use std::{env, fs, path::Path, sync::Arc};
|
||||||
|
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::Allocator;
|
||||||
use oxc_ast::{ast, AstKind, Visit};
|
use oxc_ast::{ast::*, Visit};
|
||||||
use oxc_parser::{ParseOptions, Parser};
|
use oxc_parser::{ParseOptions, Parser};
|
||||||
use oxc_regular_expression::{ConstructorParser as RegExpParser, Options as RegExpParserOptions};
|
use oxc_regular_expression::{ConstructorParser as RegExpParser, Options as RegExpParserOptions};
|
||||||
use oxc_span::SourceType;
|
use oxc_span::SourceType;
|
||||||
|
|
@ -43,55 +43,51 @@ struct RegularExpressionVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Visit<'a> for RegularExpressionVisitor {
|
impl<'a> Visit<'a> for RegularExpressionVisitor {
|
||||||
fn enter_node(&mut self, kind: AstKind<'a>) {
|
fn visit_reg_exp_literal(&mut self, re: &RegExpLiteral<'a>) {
|
||||||
let allocator = Allocator::default();
|
println!("🍀 {}", re.span.source_text(self.source_text.as_ref()));
|
||||||
|
|
||||||
match kind {
|
println!("{re:#?}");
|
||||||
AstKind::RegExpLiteral(re) => {
|
println!();
|
||||||
println!("🍀 {}", re.span.source_text(self.source_text.as_ref()));
|
}
|
||||||
|
|
||||||
println!("{re:#?}");
|
fn visit_new_expression(&mut self, new_expr: &NewExpression<'a>) {
|
||||||
println!();
|
if new_expr
|
||||||
|
.callee
|
||||||
|
.get_identifier_reference()
|
||||||
|
.filter(|ident| ident.name == "RegExp")
|
||||||
|
.is_some()
|
||||||
|
{
|
||||||
|
println!("🍀 {}", new_expr.span.source_text(&self.source_text));
|
||||||
|
|
||||||
|
let pattern_span = match new_expr.arguments.first() {
|
||||||
|
Some(Argument::StringLiteral(sl)) => sl.span,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
let flags_span = match new_expr.arguments.get(1) {
|
||||||
|
Some(Argument::StringLiteral(sl)) => Some(sl.span),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let allocator = Allocator::default();
|
||||||
|
let parsed = RegExpParser::new(
|
||||||
|
&allocator,
|
||||||
|
pattern_span.source_text(&self.source_text),
|
||||||
|
flags_span.map(|span| span.source_text(&self.source_text)),
|
||||||
|
RegExpParserOptions {
|
||||||
|
pattern_span_offset: pattern_span.start,
|
||||||
|
flags_span_offset: flags_span.map_or(0, |span| span.start),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.parse();
|
||||||
|
|
||||||
|
if let Err(error) = parsed {
|
||||||
|
let error = error.with_source_code(Arc::clone(&self.source_text));
|
||||||
|
println!("{error:?}");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
AstKind::NewExpression(new_expr)
|
println!("{parsed:#?}");
|
||||||
if new_expr
|
println!();
|
||||||
.callee
|
|
||||||
.get_identifier_reference()
|
|
||||||
.filter(|ident| ident.name == "RegExp")
|
|
||||||
.is_some() =>
|
|
||||||
{
|
|
||||||
println!("🍀 {}", new_expr.span.source_text(&self.source_text));
|
|
||||||
|
|
||||||
let pattern_span = match new_expr.arguments.first() {
|
|
||||||
Some(ast::Argument::StringLiteral(sl)) => sl.span,
|
|
||||||
_ => return,
|
|
||||||
};
|
|
||||||
|
|
||||||
let flags_span = match new_expr.arguments.get(1) {
|
|
||||||
Some(ast::Argument::StringLiteral(sl)) => Some(sl.span),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let parsed = RegExpParser::new(
|
|
||||||
&allocator,
|
|
||||||
pattern_span.source_text(&self.source_text),
|
|
||||||
flags_span.map(|span| span.source_text(&self.source_text)),
|
|
||||||
RegExpParserOptions {
|
|
||||||
pattern_span_offset: pattern_span.start,
|
|
||||||
flags_span_offset: flags_span.map_or(0, |span| span.start),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.parse();
|
|
||||||
|
|
||||||
if let Err(error) = parsed {
|
|
||||||
let error = error.with_source_code(Arc::clone(&self.source_text));
|
|
||||||
println!("{error:?}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
println!("{parsed:#?}");
|
|
||||||
println!();
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue