Skip to content

Commit ed08ef7

Browse files
Apply Suggested Changes
1 parent 2e62d25 commit ed08ef7

4 files changed

+50
-12
lines changed

src/compiler/scanner.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -2470,10 +2470,10 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
24702470
}
24712471
else if (
24722472
ch === CharacterCodes.openParen
2473-
&& charCodeUnchecked(p + 1) === CharacterCodes.question
2474-
&& charCodeUnchecked(p + 2) === CharacterCodes.lessThan
2475-
&& charCodeUnchecked(p + 3) !== CharacterCodes.equals
2476-
&& charCodeUnchecked(p + 3) !== CharacterCodes.exclamation
2473+
&& charCodeChecked(p + 1) === CharacterCodes.question
2474+
&& charCodeChecked(p + 2) === CharacterCodes.lessThan
2475+
&& charCodeChecked(p + 3) !== CharacterCodes.equals
2476+
&& charCodeChecked(p + 3) !== CharacterCodes.exclamation
24772477
) {
24782478
namedCaptureGroups = true;
24792479
}
@@ -2535,7 +2535,8 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
25352535
/** Grammar parameter */
25362536
var unicodeMode = !!(regExpFlags & RegularExpressionFlags.UnicodeMode);
25372537

2538-
// Annex B treats any unicode mode as the strict syntax.
2538+
// Regular expressions are checked more strictly when either in 'u' or 'v' mode, or
2539+
// when not using the looser interpretation of the syntax from ECMA-262 Annex B.
25392540
var anyUnicodeModeOrNonAnnexB = unicodeMode || !annexB;
25402541

25412542
/** @see {scanClassSetExpression} */
@@ -2699,7 +2700,7 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
26992700
break;
27002701
}
27012702
}
2702-
else if (max && Number.parseInt(min) > Number.parseInt(max) && (anyUnicodeModeOrNonAnnexB || text.charCodeAt(pos) === CharacterCodes.closeBrace)) {
2703+
else if (max && Number.parseInt(min) > Number.parseInt(max) && (anyUnicodeModeOrNonAnnexB || charCodeChecked(pos) === CharacterCodes.closeBrace)) {
27032704
error(Diagnostics.Numbers_out_of_order_in_quantifier, digitsStart, pos - digitsStart);
27042705
}
27052706
}
@@ -3501,9 +3502,10 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
35013502
}
35023503
});
35033504
forEach(decimalEscapes, escape => {
3504-
// in AnnexB, if a DecimalEscape is greater than the number of capturing groups then it is treated as
3505-
// either a LegacyOctalEscapeSequence or IdentityEscape
3506-
if (anyUnicodeModeOrNonAnnexB && escape.value > numberOfCapturingGroups) {
3505+
// Although a DecimalEscape with a value greater than the number of capturing groups
3506+
// is treated as either a LegacyOctalEscapeSequence or an IdentityEscape in Annex B,
3507+
// an error is nevertheless reported since it's most likely a mistake.
3508+
if (escape.value > numberOfCapturingGroups) {
35073509
if (numberOfCapturingGroups) {
35083510
error(Diagnostics.This_backreference_refers_to_a_group_that_does_not_exist_There_are_only_0_capturing_groups_in_this_regular_expression, escape.pos, escape.end - escape.pos, numberOfCapturingGroups);
35093511
}

tests/baselines/reference/regularExpressionScanning(target=es2015).errors.txt

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ regularExpressionScanning.ts(5,6): error TS1499: Unknown regular expression flag
1717
regularExpressionScanning.ts(5,7): error TS1509: This regular expression flag cannot be toggled within a subpattern.
1818
regularExpressionScanning.ts(5,10): error TS1509: This regular expression flag cannot be toggled within a subpattern.
1919
regularExpressionScanning.ts(5,11): error TS1500: Duplicate regular expression flag.
20+
regularExpressionScanning.ts(8,4): error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
21+
regularExpressionScanning.ts(9,4): error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
22+
regularExpressionScanning.ts(12,9): error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
2023
regularExpressionScanning.ts(12,24): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
2124
regularExpressionScanning.ts(12,26): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
2225
regularExpressionScanning.ts(12,29): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x53'.
2326
regularExpressionScanning.ts(12,37): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x03'.
27+
regularExpressionScanning.ts(12,42): error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
2428
regularExpressionScanning.ts(12,43): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x05'.
2529
regularExpressionScanning.ts(13,9): error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
2630
regularExpressionScanning.ts(13,24): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
@@ -207,7 +211,7 @@ regularExpressionScanning.ts(47,89): error TS1518: Anything that would possibly
207211
regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag is only available when targeting 'esnext' or later.
208212

209213

210-
==== regularExpressionScanning.ts (207 errors) ====
214+
==== regularExpressionScanning.ts (211 errors) ====
211215
const regexes: RegExp[] = [
212216
// Flags
213217
/foo/visualstudiocode,
@@ -254,10 +258,16 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
254258
// Capturing groups
255259
/\0/,
256260
/\1/,
261+
~
262+
!!! error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
257263
/\2/,
264+
~
265+
!!! error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
258266
/(hi)\1/,
259267
/(hi) (hello) \2/,
260268
/\2()(\12)(foo)\1\0[\0\1\01\123](\3\03)\5\005/,
269+
~~
270+
!!! error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
261271
~~
262272
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
263273
~~~
@@ -266,6 +276,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
266276
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x53'.
267277
~~~
268278
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x03'.
279+
~
280+
!!! error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
269281
~~~~
270282
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x05'.
271283
/\2()(\12)(foo)\1\0[\0\1\01\123](\3\03)\5\005/u,

tests/baselines/reference/regularExpressionScanning(target=es5).errors.txt

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ regularExpressionScanning.ts(5,6): error TS1499: Unknown regular expression flag
1717
regularExpressionScanning.ts(5,7): error TS1509: This regular expression flag cannot be toggled within a subpattern.
1818
regularExpressionScanning.ts(5,10): error TS1509: This regular expression flag cannot be toggled within a subpattern.
1919
regularExpressionScanning.ts(5,11): error TS1500: Duplicate regular expression flag.
20+
regularExpressionScanning.ts(8,4): error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
21+
regularExpressionScanning.ts(9,4): error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
22+
regularExpressionScanning.ts(12,9): error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
2023
regularExpressionScanning.ts(12,24): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
2124
regularExpressionScanning.ts(12,26): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
2225
regularExpressionScanning.ts(12,29): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x53'.
2326
regularExpressionScanning.ts(12,37): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x03'.
27+
regularExpressionScanning.ts(12,42): error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
2428
regularExpressionScanning.ts(12,43): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x05'.
2529
regularExpressionScanning.ts(13,9): error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
2630
regularExpressionScanning.ts(13,24): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
@@ -214,7 +218,7 @@ regularExpressionScanning.ts(47,89): error TS1518: Anything that would possibly
214218
regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag is only available when targeting 'esnext' or later.
215219

216220

217-
==== regularExpressionScanning.ts (214 errors) ====
221+
==== regularExpressionScanning.ts (218 errors) ====
218222
const regexes: RegExp[] = [
219223
// Flags
220224
/foo/visualstudiocode,
@@ -261,10 +265,16 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
261265
// Capturing groups
262266
/\0/,
263267
/\1/,
268+
~
269+
!!! error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
264270
/\2/,
271+
~
272+
!!! error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
265273
/(hi)\1/,
266274
/(hi) (hello) \2/,
267275
/\2()(\12)(foo)\1\0[\0\1\01\123](\3\03)\5\005/,
276+
~~
277+
!!! error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
268278
~~
269279
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
270280
~~~
@@ -273,6 +283,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
273283
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x53'.
274284
~~~
275285
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x03'.
286+
~
287+
!!! error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
276288
~~~~
277289
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x05'.
278290
/\2()(\12)(foo)\1\0[\0\1\01\123](\3\03)\5\005/u,

tests/baselines/reference/regularExpressionScanning(target=esnext).errors.txt

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ regularExpressionScanning.ts(5,6): error TS1499: Unknown regular expression flag
1414
regularExpressionScanning.ts(5,7): error TS1509: This regular expression flag cannot be toggled within a subpattern.
1515
regularExpressionScanning.ts(5,10): error TS1509: This regular expression flag cannot be toggled within a subpattern.
1616
regularExpressionScanning.ts(5,11): error TS1500: Duplicate regular expression flag.
17+
regularExpressionScanning.ts(8,4): error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
18+
regularExpressionScanning.ts(9,4): error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
19+
regularExpressionScanning.ts(12,9): error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
1720
regularExpressionScanning.ts(12,24): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
1821
regularExpressionScanning.ts(12,26): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
1922
regularExpressionScanning.ts(12,29): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x53'.
2023
regularExpressionScanning.ts(12,37): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x03'.
24+
regularExpressionScanning.ts(12,42): error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
2125
regularExpressionScanning.ts(12,43): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x05'.
2226
regularExpressionScanning.ts(13,9): error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
2327
regularExpressionScanning.ts(13,24): error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
@@ -181,7 +185,7 @@ regularExpressionScanning.ts(47,5): error TS1518: Anything that would possibly m
181185
regularExpressionScanning.ts(47,89): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
182186

183187

184-
==== regularExpressionScanning.ts (181 errors) ====
188+
==== regularExpressionScanning.ts (185 errors) ====
185189
const regexes: RegExp[] = [
186190
// Flags
187191
/foo/visualstudiocode,
@@ -222,10 +226,16 @@ regularExpressionScanning.ts(47,89): error TS1518: Anything that would possibly
222226
// Capturing groups
223227
/\0/,
224228
/\1/,
229+
~
230+
!!! error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
225231
/\2/,
232+
~
233+
!!! error TS1534: This backreference is invalid because the containing regular expression contains no capturing groups.
226234
/(hi)\1/,
227235
/(hi) (hello) \2/,
228236
/\2()(\12)(foo)\1\0[\0\1\01\123](\3\03)\5\005/,
237+
~~
238+
!!! error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
229239
~~
230240
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x01'.
231241
~~~
@@ -234,6 +244,8 @@ regularExpressionScanning.ts(47,89): error TS1518: Anything that would possibly
234244
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x53'.
235245
~~~
236246
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x03'.
247+
~
248+
!!! error TS1533: This backreference refers to a group that does not exist. There are only 4 capturing groups in this regular expression.
237249
~~~~
238250
!!! error TS1487: Octal escape sequences are not allowed. Use the syntax '\x05'.
239251
/\2()(\12)(foo)\1\0[\0\1\01\123](\3\03)\5\005/u,

0 commit comments

Comments
 (0)