Skip to content

Commit e87d7bd

Browse files
committed
1 parent d5738e5 commit e87d7bd

File tree

3 files changed

+31
-35
lines changed

3 files changed

+31
-35
lines changed

crates/oxc_linter/src/rules/eslint/no_control_regex.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ mod tests {
362362
// globalThis
363363
"var regex = new globalThis.RegExp('\\x1f\\x1e')",
364364
"var regex = globalThis.RegExp('\\x1f')",
365+
// inner expressions (parentheses and type expressions)
366+
"RegExp(('\\x1f'))",
367+
"new RegExp(('\\x1f'))",
368+
"new RegExp((('\\x1f')))",
369+
"new RegExp('\\x1f' as string)",
365370
],
366371
)
367372
.test_and_snapshot();
877 Bytes
Binary file not shown.

crates/oxc_linter/src/utils/regex.rs

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,36 @@ where
2020
}
2121
}
2222
AstKind::NewExpression(expr) if is_regexp_callee(&expr.callee, ctx) => {
23-
// note: improvements required for strings used via identifier references
24-
// Missing or non-string arguments will be runtime errors, but are not covered by this rule.
25-
match (&expr.arguments.first(), &expr.arguments.get(1)) {
26-
(Some(Argument::StringLiteral(pattern)), Some(Argument::StringLiteral(flags))) => {
27-
let allocator = Allocator::default();
28-
if let Some(pat) = parse_regex(&allocator, pattern.span, Some(flags.span), ctx)
29-
{
30-
cb(&pat, pattern.span);
31-
}
32-
}
33-
(Some(Argument::StringLiteral(pattern)), _) => {
34-
let allocator = Allocator::default();
35-
if let Some(pat) = parse_regex(&allocator, pattern.span, None, ctx) {
36-
cb(&pat, pattern.span);
37-
}
38-
}
39-
_ => {}
40-
}
23+
run_on_arguments(expr.arguments.first(), expr.arguments.get(1), ctx, cb);
4124
}
4225

4326
// RegExp()
4427
AstKind::CallExpression(expr) if is_regexp_callee(&expr.callee, ctx) => {
45-
// note: improvements required for strings used via identifier references
46-
// Missing or non-string arguments will be runtime errors, but are not covered by this rule.
47-
match (&expr.arguments.first(), &expr.arguments.get(1)) {
48-
(Some(Argument::StringLiteral(pattern)), Some(Argument::StringLiteral(flags))) => {
49-
let allocator = Allocator::default();
50-
if let Some(pat) = parse_regex(&allocator, pattern.span, Some(flags.span), ctx)
51-
{
52-
cb(&pat, pattern.span);
53-
}
54-
}
55-
(Some(Argument::StringLiteral(pattern)), _) => {
56-
let allocator = Allocator::default();
57-
if let Some(pat) = parse_regex(&allocator, pattern.span, None, ctx) {
58-
cb(&pat, pattern.span);
59-
}
60-
}
61-
_ => {}
28+
run_on_arguments(expr.arguments.first(), expr.arguments.get(1), ctx, cb);
29+
}
30+
_ => {}
31+
}
32+
}
33+
34+
fn run_on_arguments<M>(arg1: Option<&Argument>, arg2: Option<&Argument>, ctx: &LintContext, cb: M)
35+
where
36+
M: FnOnce(&Pattern<'_>, Span),
37+
{
38+
let arg1 = arg1.map(|arg| arg.to_expression().get_inner_expression());
39+
let arg2 = arg2.map(|arg| arg.to_expression().get_inner_expression());
40+
// note: improvements required for strings used via identifier references
41+
// Missing or non-string arguments will be runtime errors, but are not covered by this rule.
42+
match (&arg1, &arg2) {
43+
(Some(Expression::StringLiteral(pattern)), Some(Expression::StringLiteral(flags))) => {
44+
let allocator = Allocator::default();
45+
if let Some(pat) = parse_regex(&allocator, pattern.span, Some(flags.span), ctx) {
46+
cb(&pat, pattern.span);
47+
}
48+
}
49+
(Some(Expression::StringLiteral(pattern)), _) => {
50+
let allocator = Allocator::default();
51+
if let Some(pat) = parse_regex(&allocator, pattern.span, None, ctx) {
52+
cb(&pat, pattern.span);
6253
}
6354
}
6455
_ => {}

0 commit comments

Comments
 (0)