Skip to content

Commit

Permalink
feat(linter): implement visitor pattern trait for regex AST
Browse files Browse the repository at this point in the history
  • Loading branch information
camchenry committed Sep 25, 2024
1 parent f866781 commit 4a33710
Show file tree
Hide file tree
Showing 3 changed files with 418 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/oxc_regular_expression/examples/visitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use oxc_allocator::Allocator;
use oxc_regular_expression::{
visit::{RegexAstKind, Visit},
Parser, ParserOptions,
};

struct TestVisitor;

impl Visit<'_> for TestVisitor {
fn enter_node(&mut self, kind: RegexAstKind) {
println!("enter_node: {:?}", kind);
}

fn leave_node(&mut self, kind: RegexAstKind) {
println!("leave_node: {:?}", kind);
}
}

fn main() {
let source_text = r#"/(https?:\/\/github\.com\/(([^\s]+)\/([^\s]+))\/([^\s]+\/)?(issues|pull)\/([0-9]+))|(([^\s]+)\/([^\s]+))?#([1-9][0-9]*)($|[\s\:\;\-\(\=])/"#;
let allocator = Allocator::default();
let parser = Parser::new(&allocator, source_text, ParserOptions::default());
let pattern = parser.parse().unwrap().pattern;
let mut visitor = TestVisitor;
visitor.visit_pattern(&pattern);
}
1 change: 1 addition & 0 deletions crates/oxc_regular_expression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod literal_parser;
mod options;
mod span;
mod surrogate_pair;
pub mod visit;

mod generated {
mod derive_clone_in;
Expand Down
Loading

0 comments on commit 4a33710

Please sign in to comment.