Skip to content

Commit be0030c

Browse files
committedSep 28, 2024·
fix(linter): allow whitespace control characters in no-control-regex (#6140)
- fixes #6136 The original eslint rule checks if chars start with `\u` or `\x`, but our character spans are currently busted and only report 1 char for chars like `\u{0a}`. I've made whitespace an exception to the rule, so we don't report `\x0a` currently, which is fine for now I think
1 parent 14ba263 commit be0030c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed
 

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@ struct ControlCharacterFinder {
181181
impl<'a> Visit<'a> for ControlCharacterFinder {
182182
fn visit_character(&mut self, ch: &Character) {
183183
// Control characters are in the range 0x00 to 0x1F
184-
if ch.value <= 0x1F {
184+
if ch.value <= 0x1F &&
185+
// tab
186+
ch.value != 0x09 &&
187+
// line feed
188+
ch.value != 0x0A &&
189+
// carriage return
190+
ch.value != 0x0D
191+
{
192+
// TODO: check if starts with \x or \u when char spans work correctly
185193
self.control_chars.push(ch.to_string());
186194
}
187195
}
@@ -279,6 +287,15 @@ mod tests {
279287
r"new RegExp('\\u{1F}')",
280288
r"new RegExp('\\u{1F}', 'g')",
281289
r"new RegExp('\\u{1F}', flags)", // unknown flags, we assume no 'u'
290+
// https://github.com/oxc-project/oxc/issues/6136
291+
r"/---\n([\s\S]+?)\n---/",
292+
r"/import \{((?:.|\n)*)\} from '@romejs\/js-ast';/",
293+
r"/^\t+/",
294+
r"/\n/g",
295+
r"/\r\n|\r|\n/",
296+
r"/[\n\r\p{Z}\p{P}]/u",
297+
r"/[\n\t]+/g",
298+
r"/^expected `string`\.\n {2}in Foo \(at (.*)[/\\]debug[/\\]test[/\\]browser[/\\]debug\.test\.js:[0-9]+\)$/",
282299
],
283300
vec![
284301
r"var regex = /\x1f/",
@@ -296,6 +313,14 @@ mod tests {
296313
r"/\u{1F}/ugi",
297314
r"new RegExp('\\u{1F}', 'u')",
298315
r"new RegExp('\\u{1F}', 'ugi')",
316+
// https://github.com/oxc-project/oxc/issues/6136
317+
// TODO: uncomment when char spans work correctly
318+
// r"/\u{0a}/u",
319+
// r"/\x0a/u",
320+
// r"/\u{0d}/u",
321+
// r"/\x0d/u",
322+
// r"/\u{09}/u",
323+
// r"/\x09/u",
299324
],
300325
)
301326
.test_and_snapshot();

0 commit comments

Comments
 (0)
Please sign in to comment.