Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no_invalid_regexp): expand invalid case #1302

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 55 additions & 21 deletions src/rules/no_invalid_regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use super::{Context, LintRule};
use crate::js_regex::*;
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::ast::{Callee, Expr};
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,18 +74,24 @@ 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);
}
_ => {
callee.visit_children_with(self);
args.visit_children_with(self);
}
}
}
Expand Down Expand Up @@ -120,19 +126,21 @@ impl<'c, 'view> Visit for NoInvalidRegexpVisitor<'c, 'view> {
}

fn visit_call_expr(&mut self, call_expr: &deno_ast::swc::ast::CallExpr) {
if let deno_ast::swc::ast::Callee::Expr(expr) = &call_expr.callee {
self.handle_call_or_new_expr(expr, &call_expr.args, call_expr.range());
match &call_expr.callee {
Callee::Super(_) => call_expr.args.visit_children_with(self),
Callee::Expr(expr) => {
self.handle_call_or_new_expr(expr, &call_expr.args, call_expr.range());
}
_ => {}
}
}

fn visit_new_expr(&mut self, new_expr: &deno_ast::swc::ast::NewExpr) {
if new_expr.args.is_some() {
self.handle_call_or_new_expr(
&new_expr.callee,
new_expr.args.as_ref().unwrap(),
new_expr.range(),
);
}
self.handle_call_or_new_expr(
&new_expr.callee,
new_expr.args.as_ref().unwrap_or(&vec![]),
new_expr.range(),
);
}
}

Expand Down Expand Up @@ -193,6 +201,32 @@ 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: 2, message: MESSAGE, hint: HINT },
{ line: 4, col: 2, message: MESSAGE, hint: HINT },
{ line: 5, col: 2, message: MESSAGE, hint: HINT },
{ line: 7, col: 2, message: MESSAGE, hint: HINT },
{ line: 8, col: 2, message: MESSAGE, hint: HINT },
{ line: 9, col: 2, message: MESSAGE, hint: HINT }],
r"
new function () {
return /+/;
};": [{ line: 3, col: 9, message: MESSAGE, hint: HINT }],
r"
class C extends RegExp {
constructor() {
super(/+/);
}
}": [{ line: 4, col: 10, message: MESSAGE, hint: HINT }],
}
}
}
Loading