Environment
- SonarLint plugin for IntelliJ
7.4.0.60471
- Using this TSConfig
Description
The following is sample compliant code for typescript:S5860 as provided by Sonar:
const date = '01/02';
const datePattern = /(?<month>[0-9]{2})\/(?<year>[0-9]{2})/;
const dateMatched = date.match(datePattern);
if (dateMatched !== null) {
checkValidity(dateMatched.groups.month, dateMatched.groups.year);
}
With noPropertyAccessFromIndexSignature enabled, Typescript presents the following error:
TS4111: Property ‘month’ comes from an index signature, so it must be accessed with [‘month’].
TS4111: Property ‘year’ comes from an index signature, so it must be accessed with [‘year’].
This requires the above code to be:
checkValidity(dateMatched.groups['month'], dateMatched.groups['year']);
Unfortunately, sonar errors for S5860 are now presented (false positive):
SonarLint: Use the named groups of this regex or remove the names.
Sonar is not considering access via index syntax.
Side Note
The unmodified sample compliant code above has the following, unrelated, issues:
[typescript:S6353] Sonar: Use concise character class syntax '\d' instead of '[0-9]'.
TS18048: 'dateMatched.groups' is possibly 'undefined'.
Perhaps documentation could be improved along with addressing the above false positive?