Modify rule S6325: Adapt to LaYC

This commit is contained in:
cynthiabethea 2023-06-20 09:43:42 +01:00 committed by GitHub
parent 3b655b6b90
commit 2429edf9ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,6 @@
== Why is this an issue?
Regular expression literals should be preferred over the `RegExp` constructor calls when the pattern is a literal. Simply using a regular expression literal is more concise and easier to read and does not require escaping like a string literal does.
Using the `RegExp` constructor is suitable when the pattern is computed dynamically, e.g. when it is provided by the user.
=== Noncompliant code example
Using regular expression literals is recommended over using the `RegExp` constructor calls if the pattern is a literal. Regular expression literals are shorter, more readable, and do not need to be escaped like string literals.
[source,javascript]
----
@ -15,7 +11,7 @@ new RegExp("\\d+");
new RegExp(`qux|quuz`);
----
=== Compliant solution
Using the `RegExp` constructor is suitable when the pattern is computed dynamically, for example when it is provided by the user.
[source,javascript]
----
@ -26,3 +22,11 @@ new RegExp(`qux|quuz`);
/qux|quuz/;
new RegExp(`Dear ${title},`);
----
== Resources
=== Documentation
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp[MDN - RegExp]