All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S6323.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

Why is this an issue?

Alternation is used to match a single regular expression out of several possible regular expressions. If one of the alternatives is empty it would match any input, which is most probably a mistake.

Noncompliant code example

/Jack|Peter|/.test('John'); // Noncompliant - returns 'true'
/Jack||Peter/.test('John'); // Noncompliant - returns 'true'

Compliant solution

/Jack|Peter/.test('John'); // returns 'false'

Exceptions

One could use an empty alternation to make a regular expression group optional. Rule will not report on such cases.

/mandatory(-optional|)/.test('mandatory'); // returns 'true'
/mandatory(-optional|)/.test('mandatory-optional'); // returns 'true'

However, if there is a quantifier after the group the issue will be reported as using both | and quantifier is redundant.

/mandatory(-optional|)?/.test('mandatory'); // Noncompliant - using both `|` inside the group and `?` for the group.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy