You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using a Regexp() constructor and providing a string as parameter, you need to escape special characters like character classes with 2 backslashes \\.
Using strings is necessary when you want to make the regex pattern dynamic like:
newRegExp(`\d ${foo}`);
It can also happen, that the user started writing his regex in a literal, and copied it to a constructor when making it dynamic. In this case, the rule will flag the backslash with the message Unnecessary escape character: \d.
This can be confusing if you were indeed wishing to escape it correctly. There a quickfix in the no-useless-escape ESLint implementation that we use that proposes to add the missing backslash, but the error message could be improved to hint to it.
We could also make it clearer in the RSPEC.
The quickfix message in SL for VS code seems to fail to interpolate the variable name:
Special cases
We should take into account cases like that:
constpattern="\d";constregex=newRegexp(pattern);
The text was updated successfully, but these errors were encountered:
yassin-kammoun-sonarsource
changed the title
Improve S6535 (unnecessary-character-escapes) error message in new Regexp() context
Fix S6535 (unnecessary-character-escapes): Disambiguate conflicting message ids
Aug 25, 2023
When using a
Regexp()
constructor and providing a string as parameter, you need to escape special characters like character classes with 2 backslashes\\
.Using strings is necessary when you want to make the regex pattern dynamic like:
It can also happen, that the user started writing his regex in a literal, and copied it to a constructor when making it dynamic. In this case, the rule will flag the backslash with the message
Unnecessary escape character: \d.
This can be confusing if you were indeed wishing to escape it correctly. There a quickfix in the
no-useless-escape
ESLint implementation that we use that proposes to add the missing backslash, but the error message could be improved to hint to it.We could also make it clearer in the RSPEC.
The quickfix message in SL for VS code seems to fail to interpolate the variable name:
Special cases
We should take into account cases like that:
The text was updated successfully, but these errors were encountered: