Skip to content

Commit

Permalink
fix(linter/no-control-regex): false negative for flags in template li…
Browse files Browse the repository at this point in the history
…terals (#6531)

Updates regex flag extraction logic to handle no-substitution template literals, allowing cases like this to be correctly reported:
```js
let r = new RegExp('\\u{0}', `u`);
```
  • Loading branch information
DonIsaac committed Oct 14, 2024
1 parent ecce5c5 commit 2b86de9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 7 additions & 3 deletions crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,15 @@ pub fn extract_regex_flags<'a>(
if args.len() <= 1 {
return None;
}
let Argument::StringLiteral(flag_arg) = &args[1] else {
return None;
let flag_arg = match &args[1] {
Argument::StringLiteral(flag_arg) => flag_arg.value.clone(),
Argument::TemplateLiteral(template) if template.is_no_substitution_template() => {
template.quasi().expect("no-substitution templates always have a quasi")
}
_ => return None,
};
let mut flags = RegExpFlags::empty();
for ch in flag_arg.value.chars() {
for ch in flag_arg.chars() {
let flag = RegExpFlags::try_from(ch).ok()?;
flags |= flag;
}
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_control_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ mod tests {
],
vec![
r"let r = /\u{0}/u",
r"let r = new RegExp('\\u{0}', 'u');",
r"let r = new RegExp('\\u{0}', `u`);",
r"let r = /\u{c}/u",
r"let r = /\u{1F}/u",
r"let r = new RegExp('\\u{1F}', 'u');", // flags are known & contain u
Expand Down

0 comments on commit 2b86de9

Please sign in to comment.