Skip to content

Commit

Permalink
fix(no_invalid_regexp): support inside function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Colt45s committed Jul 28, 2024
1 parent ac2aa57 commit c569185
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions src/rules/no_invalid_regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::Program;
use crate::ProgramRef;
use deno_ast::swc::ast::Expr;
use deno_ast::swc::ast::ExprOrSpread;
use deno_ast::swc::visit::noop_visit_type;
use deno_ast::swc::visit::Visit;
use deno_ast::swc::visit::{noop_visit_type, VisitWith};
use deno_ast::SourceRange;
use deno_ast::SourceRangedForSpanned;

Expand Down Expand Up @@ -74,19 +74,26 @@ impl<'c, 'view> NoInvalidRegexpVisitor<'c, 'view> {
args: &[ExprOrSpread],
range: SourceRange,
) {
if let Expr::Ident(ident) = callee {
if ident.sym != *"RegExp" || args.is_empty() {
return;
}
if let Some(pattern) = &check_expr_for_string_literal(&args[0].expr) {
if args.len() > 1 {
if let Some(flags) = &check_expr_for_string_literal(&args[1].expr) {
self.check_regex(pattern, flags, range);
return;
match callee {
Expr::Ident(ident) => {
if ident.sym != *"RegExp" || args.is_empty() {
return;
}
if let Some(pattern) = &check_expr_for_string_literal(&args[0].expr) {
if args.len() > 1 {
if let Some(flags) = &check_expr_for_string_literal(&args[1].expr) {
self.check_regex(pattern, flags, range);
return;
}
}
self.check_regex(pattern, "", range);
}
self.check_regex(pattern, "", range);
}
Expr::Paren(paren_expr) => {
paren_expr.visit_children_with(self);
args.visit_children_with(self);
}
_ => {}
}
}

Expand Down Expand Up @@ -193,6 +200,22 @@ let re = new RegExp('foo', x);",
r"/(?<a>a)\k</": [{ col: 0, message: MESSAGE, hint: HINT }],
r"/(?<!a){1}/": [{ col: 0, message: MESSAGE, hint: HINT }],
r"/(a)(a)(a)(a)(a)(a)(a)(a)(a)(a)\11/u": [{ col: 0, message: MESSAGE, hint: HINT }],
}
r"
((_) => [
/+/,
RegExp('+'),
new RegExp('+'),
])([
/+/,
RegExp('+'),
new RegExp('+'),
]);": [
{ line: 3, col: 4, message: MESSAGE, hint: HINT },
{ line: 4, col: 4, message: MESSAGE, hint: HINT },
{ line: 5, col: 4, message: MESSAGE, hint: HINT },
{ line: 7, col: 4, message: MESSAGE, hint: HINT },
{ line: 8, col: 4, message: MESSAGE, hint: HINT },
{ line: 9, col: 4, message: MESSAGE, hint: HINT }
]}
}
}

0 comments on commit c569185

Please sign in to comment.