Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S6535 (unnecessary-character-escapes): Disambiguate conflicting message ids #3987

Closed
ilia-kebets-sonarsource opened this issue Jun 27, 2023 · 0 comments · Fixed by #4108
Assignees
Labels
hardening type: bug Exceptions and blocking issues during analysis
Milestone

Comments

@ilia-kebets-sonarsource
Copy link
Contributor

ilia-kebets-sonarsource commented Jun 27, 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:

new RegExp(`\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:

Screenshot 2023-06-27 at 10 55 05

Special cases

We should take into account cases like that:

const pattern="\d";
const regex = new Regexp(pattern);
@ilia-kebets-sonarsource ilia-kebets-sonarsource added the type: enhancement Improving existing functionality label Jun 27, 2023
@yassin-kammoun-sonarsource yassin-kammoun-sonarsource added this to the 10.5 milestone Aug 17, 2023
@ilia-kebets-sonarsource ilia-kebets-sonarsource modified the milestones: 10.5, 10.6 Aug 23, 2023
@yassin-kammoun-sonarsource yassin-kammoun-sonarsource added type: bug Exceptions and blocking issues during analysis and removed type: enhancement Improving existing functionality labels Aug 25, 2023
@yassin-kammoun-sonarsource 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hardening type: bug Exceptions and blocking issues during analysis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants