Skip to content

Commit 4106eda

Browse files
committed
Fix regex class union string detection
Report TS1518 for string-matching operands regardless of their position in a Unicode set union. Preserve MayContainStrings across later and nested operands and add focused regression baselines.
1 parent b465fdb commit 4106eda

6 files changed

Lines changed: 75 additions & 3 deletions

src/compiler/scanner.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3214,6 +3214,10 @@ export function createScanner(
32143214
break;
32153215
default:
32163216
operand = scanClassSetOperand();
3217+
if (isCharacterComplement && mayContainStrings) {
3218+
error(Diagnostics.Anything_that_would_possibly_match_more_than_a_single_character_is_invalid_inside_a_negated_character_class, start, pos - start);
3219+
}
3220+
expressionMayContainStrings ||= mayContainStrings;
32173221
break;
32183222
}
32193223
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
regularExpressionClassUnionMayContainStrings.ts(2,8): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
2+
regularExpressionClassUnionMayContainStrings.ts(3,9): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
3+
regularExpressionClassUnionMayContainStrings.ts(4,11): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
4+
regularExpressionClassUnionMayContainStrings.ts(5,9): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
5+
6+
7+
==== regularExpressionClassUnionMayContainStrings.ts (4 errors) ====
8+
const regexes: RegExp[] = [
9+
/[^\q{xy}b]/v,
10+
~~~~~~
11+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
12+
/[^b\q{xy}]/v,
13+
~~~~~~
14+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
15+
/[^a-c\q{xy}]/v,
16+
~~~~~~
17+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
18+
/[^b[b\q{xy}]]/v,
19+
~~~~~~~~~
20+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
21+
];
22+

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,23 @@ regularExpressionScanning.ts(38,87): error TS1501: This regular expression flag
195195
regularExpressionScanning.ts(39,56): error TS1519: Operators must not be mixed within a character class. Wrap it in a nested class instead.
196196
regularExpressionScanning.ts(39,67): error TS1005: '&&' expected.
197197
regularExpressionScanning.ts(39,77): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
198+
regularExpressionScanning.ts(40,14): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
198199
regularExpressionScanning.ts(40,83): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
199200
regularExpressionScanning.ts(41,5): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
200201
regularExpressionScanning.ts(41,30): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
201202
regularExpressionScanning.ts(41,83): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
202203
regularExpressionScanning.ts(42,5): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
204+
regularExpressionScanning.ts(42,18): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
203205
regularExpressionScanning.ts(42,28): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
204206
regularExpressionScanning.ts(42,53): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
205207
regularExpressionScanning.ts(42,77): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
208+
regularExpressionScanning.ts(43,14): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
206209
regularExpressionScanning.ts(43,95): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
207210
regularExpressionScanning.ts(44,5): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
208211
regularExpressionScanning.ts(44,34): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
209212
regularExpressionScanning.ts(44,95): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
210213
regularExpressionScanning.ts(45,5): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
214+
regularExpressionScanning.ts(45,22): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
211215
regularExpressionScanning.ts(45,32): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
212216
regularExpressionScanning.ts(45,61): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
213217
regularExpressionScanning.ts(45,89): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
@@ -219,7 +223,7 @@ regularExpressionScanning.ts(47,89): error TS1518: Anything that would possibly
219223
regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
220224

221225

222-
==== regularExpressionScanning.ts (219 errors) ====
226+
==== regularExpressionScanning.ts (223 errors) ====
223227
const regexes: RegExp[] = [
224228
// Flags
225229
/foo/visualstudiocode,
@@ -654,6 +658,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
654658
~
655659
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
656660
/[^\p{Emoji}\p{RGI_Emoji}][^\p{Emoji}--\p{RGI_Emoji}][^\p{Emoji}&&\p{RGI_Emoji}]/v,
661+
~~~~~~~~~~~~~
662+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
657663
~
658664
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
659665
/[^\p{RGI_Emoji}\p{Emoji}][^\p{RGI_Emoji}--\p{Emoji}][^\p{RGI_Emoji}&&\p{Emoji}]/v,
@@ -665,6 +671,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
665671
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
666672
/[^\p{RGI_Emoji}\q{foo}][^\p{RGI_Emoji}--\q{foo}][^\p{RGI_Emoji}&&\q{foo}]/v,
667673
~~~~~~~~~~~~~
674+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
675+
~~~~~~~
668676
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
669677
~~~~~~~~~~~~~
670678
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
@@ -673,6 +681,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
673681
~
674682
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
675683
/[^\p{Emoji}[[\p{RGI_Emoji}]]][^\p{Emoji}--[[\p{RGI_Emoji}]]][^\p{Emoji}&&[[\p{RGI_Emoji}]]]/v,
684+
~~~~~~~~~~~~~~~~~
685+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
676686
~
677687
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
678688
/[^[[\p{RGI_Emoji}]]\p{Emoji}][^[[\p{RGI_Emoji}]]--\p{Emoji}][^[[\p{RGI_Emoji}]]&&\p{Emoji}]/v,
@@ -684,6 +694,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
684694
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
685695
/[^[[\p{RGI_Emoji}]]\q{foo}][^[[\p{RGI_Emoji}]]--\q{foo}][^[[\p{RGI_Emoji}]]&&\q{foo}]/v,
686696
~~~~~~~~~~~~~~~~~
697+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
698+
~~~~~~~
687699
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
688700
~~~~~~~~~~~~~~~~~
689701
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,23 @@ regularExpressionScanning.ts(38,87): error TS1501: This regular expression flag
203203
regularExpressionScanning.ts(39,56): error TS1519: Operators must not be mixed within a character class. Wrap it in a nested class instead.
204204
regularExpressionScanning.ts(39,67): error TS1005: '&&' expected.
205205
regularExpressionScanning.ts(39,77): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
206+
regularExpressionScanning.ts(40,14): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
206207
regularExpressionScanning.ts(40,83): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
207208
regularExpressionScanning.ts(41,5): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
208209
regularExpressionScanning.ts(41,30): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
209210
regularExpressionScanning.ts(41,83): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
210211
regularExpressionScanning.ts(42,5): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
212+
regularExpressionScanning.ts(42,18): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
211213
regularExpressionScanning.ts(42,28): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
212214
regularExpressionScanning.ts(42,53): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
213215
regularExpressionScanning.ts(42,77): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
216+
regularExpressionScanning.ts(43,14): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
214217
regularExpressionScanning.ts(43,95): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
215218
regularExpressionScanning.ts(44,5): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
216219
regularExpressionScanning.ts(44,34): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
217220
regularExpressionScanning.ts(44,95): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
218221
regularExpressionScanning.ts(45,5): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
222+
regularExpressionScanning.ts(45,22): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
219223
regularExpressionScanning.ts(45,32): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
220224
regularExpressionScanning.ts(45,61): error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
221225
regularExpressionScanning.ts(45,89): error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
@@ -228,7 +232,7 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
228232

229233

230234
!!! error TS5107: Option 'target=ES5' is deprecated and will stop functioning in TypeScript 7.0. Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
231-
==== regularExpressionScanning.ts (226 errors) ====
235+
==== regularExpressionScanning.ts (230 errors) ====
232236
const regexes: RegExp[] = [
233237
// Flags
234238
/foo/visualstudiocode,
@@ -677,6 +681,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
677681
~
678682
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
679683
/[^\p{Emoji}\p{RGI_Emoji}][^\p{Emoji}--\p{RGI_Emoji}][^\p{Emoji}&&\p{RGI_Emoji}]/v,
684+
~~~~~~~~~~~~~
685+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
680686
~
681687
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
682688
/[^\p{RGI_Emoji}\p{Emoji}][^\p{RGI_Emoji}--\p{Emoji}][^\p{RGI_Emoji}&&\p{Emoji}]/v,
@@ -688,6 +694,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
688694
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
689695
/[^\p{RGI_Emoji}\q{foo}][^\p{RGI_Emoji}--\q{foo}][^\p{RGI_Emoji}&&\q{foo}]/v,
690696
~~~~~~~~~~~~~
697+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
698+
~~~~~~~
691699
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
692700
~~~~~~~~~~~~~
693701
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
@@ -696,6 +704,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
696704
~
697705
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
698706
/[^\p{Emoji}[[\p{RGI_Emoji}]]][^\p{Emoji}--[[\p{RGI_Emoji}]]][^\p{Emoji}&&[[\p{RGI_Emoji}]]]/v,
707+
~~~~~~~~~~~~~~~~~
708+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
699709
~
700710
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
701711
/[^[[\p{RGI_Emoji}]]\p{Emoji}][^[[\p{RGI_Emoji}]]--\p{Emoji}][^[[\p{RGI_Emoji}]]&&\p{Emoji}]/v,
@@ -707,6 +717,8 @@ regularExpressionScanning.ts(47,101): error TS1501: This regular expression flag
707717
!!! error TS1501: This regular expression flag is only available when targeting 'es2024' or later.
708718
/[^[[\p{RGI_Emoji}]]\q{foo}][^[[\p{RGI_Emoji}]]--\q{foo}][^[[\p{RGI_Emoji}]]&&\q{foo}]/v,
709719
~~~~~~~~~~~~~~~~~
720+
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
721+
~~~~~~~
710722
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.
711723
~~~~~~~~~~~~~~~~~
712724
!!! error TS1518: Anything that would possibly match more than a single character is invalid inside a negated character class.

0 commit comments

Comments
 (0)