typescript:S5860 not considering access via index syntax

Environment


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?

Hello Mike,

Welcome back, and thank you for your feedback :slight_smile:

As you rightfully pointed out, this is indeed a false positive. I created a ticket to address it as soon as possible.

Furthermore, a change is on its way to update the rule documentation and to fix the issue about the regular expressions.

Best,
Yassin

1 Like