Skip to content

Commit

Permalink
fix(parser): Span for invalid regex flags (#5225)
Browse files Browse the repository at this point in the history
### Before
```
  x Flag u is mentioned twice in regular expression literal
   ,-[1:20]
 1 | const a = /\2(.)/uuxig;
   :                    ^
 2 | debugger;
   `----

  x Unexpected flag x in regular expression literal
   ,-[1:21]
 1 | const a = /\2(.)/uuxig;
   :                     ^
 2 | debugger;
   `----
```

### After
```
  x Flag u is mentioned twice in regular expression literal
   ,-[1:19]
 1 | const a = /\2(.)/uuxig;
   :                   ^
 2 | debugger;
   `----

  x Unexpected flag x in regular expression literal
   ,-[1:20]
 1 | const a = /\2(.)/uuxig;
   :                    ^
 2 | debugger;
   `----
```
  • Loading branch information
leaysgur committed Aug 26, 2024
1 parent 2a001a0 commit 1686920
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
10 changes: 8 additions & 2 deletions crates/oxc_parser/src/lexer/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ impl<'a> Lexer<'a> {
{
self.consume_char();
let Ok(flag) = RegExpFlags::try_from(b) else {
self.error(diagnostics::reg_exp_flag(b as char, self.current_offset()));
self.error(diagnostics::reg_exp_flag(
b as char,
self.current_offset().expand_left(1),
));
continue;
};
if flags.contains(flag) {
self.error(diagnostics::reg_exp_flag_twice(b as char, self.current_offset()));
self.error(diagnostics::reg_exp_flag_twice(
b as char,
self.current_offset().expand_left(1),
));
continue;
}
flags |= flag;
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3438,9 +3438,9 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
help: Did you mean to use a ':'? An '=' can only follow a property name when the containing object literal is part of a destructuring pattern.

× Flag i is mentioned twice in regular expression literal
╭─[babel/packages/babel-parser/test/fixtures/es2015/regex/duplicate-flags/input.js:1:7]
╭─[babel/packages/babel-parser/test/fixtures/es2015/regex/duplicate-flags/input.js:1:6]
1 │ /./gii;
·
·
╰────

× Unexpected token
Expand Down
8 changes: 4 additions & 4 deletions tasks/coverage/parser_test262.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20531,17 +20531,17 @@ Negative Passed: 4220/4220 (100.00%)
╰────

× Unexpected flag G in regular expression literal
╭─[test262/test/language/literals/regexp/early-err-bad-flag.js:18:5]
╭─[test262/test/language/literals/regexp/early-err-bad-flag.js:18:4]
17 │
18 │ /./G;
·
·
╰────

× Flag g is mentioned twice in regular expression literal
╭─[test262/test/language/literals/regexp/early-err-dup-flag.js:18:7]
╭─[test262/test/language/literals/regexp/early-err-dup-flag.js:18:6]
17 │
18 │ /./gig;
·
·
╰────

× Expected a semicolon or an implicit semicolon after a statement, but found none
Expand Down
48 changes: 24 additions & 24 deletions tasks/coverage/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10378,90 +10378,90 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro
╰────

× Unexpected flag a in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:12]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:11]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Unexpected flag l in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:13]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:12]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Flag s is mentioned twice in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:14]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:13]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Unexpected flag t in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:15]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:14]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Flag u is mentioned twice in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:16]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:15]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Flag i is mentioned twice in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:18]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:17]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Unexpected flag o in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:19]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:18]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Unexpected flag c in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:20]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:19]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Unexpected flag o in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:21]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:20]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Flag d is mentioned twice in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:22]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:21]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

× Unexpected flag e in regular expression literal
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:23]
╭─[typescript/tests/cases/compiler/regularExpressionScanning.ts:3:22]
2 │ // Flags
3 │ /foo/visualstudiocode,
·
·
4 │ // Pattern modifiers
╰────

Expand Down Expand Up @@ -20391,9 +20391,9 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/typeFro
help: Try insert a semicolon here

× Unexpected flag a in regular expression literal
╭─[typescript/tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpressionDivideAmbiguity3.ts:1:17]
╭─[typescript/tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpressionDivideAmbiguity3.ts:1:16]
1 │ if (1) /regexp/a.foo();
·
·
╰────

× Unterminated regular expression
Expand Down

0 comments on commit 1686920

Please sign in to comment.