From c070b25275ae96057bccb0c29fff0081c4eea212 Mon Sep 17 00:00:00 2001 From: Eric MORAND Date: Tue, 3 Sep 2024 17:11:44 +0200 Subject: [PATCH] JS-322 Write the TypeScript variant of RSPEC S2301 --- rules/S2301/java/metadata.json | 2 +- rules/S2301/java/rule.adoc | 2 +- rules/S2301/java/why.adoc | 51 ++++++++++++++++++++++++++++ rules/S2301/javascript/metadata.json | 3 ++ rules/S2301/javascript/rule.adoc | 17 ++++++++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 rules/S2301/java/why.adoc create mode 100644 rules/S2301/javascript/metadata.json create mode 100644 rules/S2301/javascript/rule.adoc diff --git a/rules/S2301/java/metadata.json b/rules/S2301/java/metadata.json index 1797133380..ad5e0d9255 100644 --- a/rules/S2301/java/metadata.json +++ b/rules/S2301/java/metadata.json @@ -1,3 +1,3 @@ { - + "title": "Methods should not contain selector arguments" } diff --git a/rules/S2301/java/rule.adoc b/rules/S2301/java/rule.adoc index fe598e7f71..fd32d5792c 100644 --- a/rules/S2301/java/rule.adoc +++ b/rules/S2301/java/rule.adoc @@ -1,4 +1,4 @@ -include::../rule.adoc[] +include::./why.adoc[] ifdef::env-github,rspecator-view[] diff --git a/rules/S2301/java/why.adoc b/rules/S2301/java/why.adoc new file mode 100644 index 0000000000..8dc5b800ca --- /dev/null +++ b/rules/S2301/java/why.adoc @@ -0,0 +1,51 @@ +== Why is this an issue? + +A selector argument is a ``++boolean++`` argument that's used to determine which of two paths to take through a method. Specifying such a parameter may seem innocuous, particularly if it's well named. + + +Unfortunately, developers calling the method won't see the parameter name, only its value. They'll be forced either to guess at the meaning or to take extra time to look the method up. + + +Instead, separate methods should be written. + + +This rule finds methods with a ``++boolean++`` that's used to determine which path to take through the method. + + +=== Noncompliant code example + +[source,text,typescript] +---- +function tempt(name: string, ofAge: boolean) { + if (ofAge) { + offerLiquor(name); + } else { + offerCandy(name); + } +} + +// ... +function corrupt() { + tempt("Joe", false); // does this mean not to temp Joe? +} +---- + + +=== Compliant solution + +[source,text,typescript] +---- +function temptAdult(name: string) { + offerLiquor(name); +} + +function temptChild(name: string) { + offerCandy(name); +} + +// ... +function corrupt() { + age < legalAge ? temptChild("Joe") : temptAdult("Joe"); +} +---- + diff --git a/rules/S2301/javascript/metadata.json b/rules/S2301/javascript/metadata.json new file mode 100644 index 0000000000..1797133380 --- /dev/null +++ b/rules/S2301/javascript/metadata.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/rules/S2301/javascript/rule.adoc b/rules/S2301/javascript/rule.adoc new file mode 100644 index 0000000000..fe598e7f71 --- /dev/null +++ b/rules/S2301/javascript/rule.adoc @@ -0,0 +1,17 @@ +include::../rule.adoc[] + +ifdef::env-github,rspecator-view[] + +''' +== Implementation Specification +(visible only on this page) + +include::../message.adoc[] + +''' +== Comments And Links +(visible only on this page) + +include::../comments-and-links.adoc[] + +endif::env-github,rspecator-view[]