mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
fix(linter/no-control-regex): false negative for flags in template literals (#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`);
```
This commit is contained in:
parent
ecce5c53f8
commit
2b86de9e74
2 changed files with 9 additions and 3 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue