TS1508: Unexpected '?'. Did you mean to escape it with backslash? shouldn't report even with `v` flag
Opened this issue ยท 5 comments
๐ Search Terms
TS1508, "Did you mean to escape it with backslash?"
๐ Version & Regression Information
- This changed between versions ______ and _______
- This changed in #55600 which introduced regular expression syntax checking
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
โฏ Playground Link
https://www.typescriptlang.org/play/?target=11#code/PQbQ-AZAusBuBQQ
๐ป Code
/[?&]/v๐ Actual behavior
The error "TS1508: Unexpected '?'. Did you mean to escape it with backslash?" is given.
๐ Expected behavior
No error because ? doesn't need to be escaped in a character class even with a v flag.
Additional information about the issue
Other characters like { do need escaping with the v flag, but ? gets no runtime errors, so I don't see why this has to err now.
This changed between versions 5.6.3 and 5.7.2
No, it was present in 5.6.3. Maybe you should edit it to "This changed in #55600 which introduced regular expression syntax checking"
A strange thing about this error is that it mentions ? but it is really about & (that's where the error squiggly is). If you escape the ? the error persists, and only goes away if you escape the &. Indeed it looks like the error talks about the first character (e.g., if you write /[\?&]/v to "fix" the error, the error stays but now it says "unexpected \"). So whether or not there's a bug around requiring & to be escaped (I think only && needs to be escaped, right?) there does seem to be a bug around the text of the message... so this is... two bug reports? Should we file another one (if neither are duplicates)?
This changed between versions 5.6.3 and 5.7.2
No, it was present in 5.6.3. Maybe you should edit it to "This changed in #55600 which introduced regular expression syntax checking"
I did check, but must have messed it up. Thanks!
A strange thing about this error is that it mentions
?but it is really about&(that's where the error squiggly is). If you escape the?the error persists, and only goes away if you escape the&. Indeed it looks like the error talks about the first character (e.g., if you write/[\?&]/vto "fix" the error, the error stays but now it says "unexpected\"). So whether or not there's a bug around requiring&to be escaped (I think only&&needs to be escaped, right?)
Right. /[?&&]/v gives an error in Chrome, for example, while /[?&]/v does not.
Paging @graphemecluster
The incorrectly reported character gets fixed by this patch:
diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts
index 343df39ad9..3619f57180 100644
--- a/src/compiler/scanner.ts
+++ b/src/compiler/scanner.ts
@@ -3103,7 +3103,7 @@ export function createScanner(
operand = scanClassSetOperand();
break;
}
- switch (charCodeChecked(pos)) {
+ switch (ch = charCodeChecked(pos)) {
case CharacterCodes.minus:
if (charCodeChecked(pos + 1) === CharacterCodes.minus) {
if (isCharacterComplement && mayContainStrings) {But the other part of the issue would have to be investigated further.
These 3 lines shouldn't have been there in the first place ๐
diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts
index 343df39ad9..986a3dcf5a 100644
--- a/src/compiler/scanner.ts
+++ b/src/compiler/scanner.ts
@@ -3125,9 +3125,6 @@ export function createScanner(
mayContainStrings = !isCharacterComplement && expressionMayContainStrings;
return;
}
- else {
- error(Diagnostics.Unexpected_0_Did_you_mean_to_escape_it_with_backslash, pos, 1, String.fromCharCode(ch));
- }
break;
default:
if (isCharacterComplement && mayContainStrings) {