-
-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(regular_expression): Misc refactoring for body_parser (#5062)
- Add examples to list all `RegExp`s in source code - Refactor `MayContainStrings` related part
- Loading branch information
Showing
6 changed files
with
107 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#![allow(clippy::print_stdout)] | ||
use std::{env, fs, path::Path, sync::Arc}; | ||
|
||
use oxc_allocator::Allocator; | ||
use oxc_ast::AstKind; | ||
use oxc_parser::Parser; | ||
use oxc_semantic::SemanticBuilder; | ||
use oxc_span::SourceType; | ||
|
||
fn main() { | ||
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string()); | ||
let path = Path::new(&name); | ||
|
||
let source_text = Arc::new(fs::read_to_string(path).unwrap()); | ||
let source_type = SourceType::from_path(path).unwrap(); | ||
|
||
let allocator = Allocator::default(); | ||
|
||
let parser_ret = Parser::new(&allocator, &source_text, source_type).parse(); | ||
if !parser_ret.errors.is_empty() { | ||
println!("Parsing failed:"); | ||
for error in parser_ret.errors { | ||
let error = error.with_source_code(Arc::clone(&source_text)); | ||
println!("{error:?}"); | ||
} | ||
return; | ||
} | ||
|
||
let program = allocator.alloc(parser_ret.program); | ||
let semantic_ret = SemanticBuilder::new(&source_text, source_type).build(program); | ||
let semantic = semantic_ret.semantic; | ||
|
||
for node in semantic.nodes().iter() { | ||
match node.kind() { | ||
AstKind::RegExpLiteral(re) => { | ||
let literal = re.span.source_text(&source_text); | ||
let parsed = oxc_regular_expression::Parser::new( | ||
&allocator, | ||
literal, | ||
oxc_regular_expression::ParserOptions::default() | ||
.with_span_offset(re.span.start), | ||
) | ||
.parse(); | ||
|
||
println!("🍀 {literal}"); | ||
if let Err(error) = parsed { | ||
let error = error.with_source_code(Arc::clone(&source_text)); | ||
println!("{error:?}"); | ||
return; | ||
} | ||
println!("{parsed:#?}"); | ||
println!(); | ||
} | ||
AstKind::NewExpression(new_expr) => { | ||
if new_expr | ||
.callee | ||
.get_identifier_reference() | ||
.filter(|ident| ident.name == "RegExp") | ||
.is_some() | ||
{ | ||
println!("👻 TODO: new RegExp(...)"); | ||
println!(); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const re1 = /abc{1}/gsv; | ||
const re2 = new RegExp("ooo", "u"); | ||
const re3 = /[\w--[v]]/gsv; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters