Skip to content

Commit

Permalink
Update rule metadata (#4044)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource authored Aug 4, 2023
1 parent 8790f2f commit 29853cc
Show file tree
Hide file tree
Showing 48 changed files with 144 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ <h2>Why is this an issue?</h2>
<p>An empty code block is confusing. It will require some effort from maintainers to determine if it is intentional or indicates the implementation is
incomplete.</p>
<pre>
for (var i = 0; i &lt; length; i++) {} // Noncompliant: is the block empty on purpose, or is code missing?
for (let i = 0; i &lt; length; i++) {} // Noncompliant: is the block empty on purpose, or is code missing?
</pre>
<p>Removing or filling the empty code blocks takes away ambiguity and generally results in a more straightforward and less surprising code.</p>
<h3>Exceptions</h3>
<p>The rule ignores: * code blocks that contain comments * <code>catch</code> blocks</p>
<p>The rule ignores:</p>
<ul>
<li> code blocks that contain comments </li>
<li> <code>catch</code> blocks </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ <h2>Why is this an issue?</h2>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import">MDN - <code>import</code></a> === Related rules
</li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import">MDN - <code>import</code></a> </li>
</ul>
<h3>Related rules</h3>
<ul>
<li> {rule:javascript:S1481} - Unused local variables and functions should be removed </li>
</ul>

Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
<h2>Why is this an issue?</h2>
<p><code>TODO</code> tags are commonly used to mark places where some more code is required, but which the developer wants to implement later.</p>
<p>Sometimes the developer will not have the time or will simply forget to get back to that tag.</p>
<p>This rule is meant to track those tags and to ensure that they do not go unnoticed.</p>
<p>Developers often use <code>TOOO</code> tags to mark areas in the code where additional work or improvements are needed but are not implemented
immediately. However, these <code>TODO</code> tags sometimes get overlooked or forgotten, leading to incomplete or unfinished code. This code smell
class aims to identify and address such unattended <code>TODO</code> tags to ensure a clean and maintainable codebase. This description will explore
why this is a problem and how it can be fixed to improve the overall code quality.</p>
<h3>What is the potential impact?</h3>
<p>Unattended <code>TODO</code> tags in code can have significant implications for the development process and the overall codebase.</p>
<p>Incomplete Functionality: When developers leave <code>TODO</code> tags without implementing the corresponding code, it results in incomplete
functionality within the software. This can lead to unexpected behavior or missing features, adversely affecting the end-user experience.</p>
<p>Missed Bug Fixes: If developers do not promptly address <code>TODO</code> tags, they might overlook critical bug fixes and security updates.
Delayed bug fixes can result in more severe issues and increase the effort required to resolve them later.</p>
<p>Impact on Collaboration: In team-based development environments, unattended <code>TODO</code> tags can hinder collaboration. Other team members
might not be aware of the intended changes, leading to conflicts or redundant efforts in the codebase.</p>
<p>Codebase Bloat: Accumulation of unattended <code>TODO</code> tags over time can clutter the codebase and make it difficult to distinguish between
work in progress and completed code. This bloat can make it challenging to maintain an organized and efficient codebase.</p>
<p>Addressing this code smell is essential to ensure a maintainable, readable, reliable codebase and promote effective collaboration among
developers.</p>
<h3>Noncompliant code example</h3>
<pre>
function doSomething() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ <h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#ignoring_some_returned_values">MDN
Destructuring assignment</a> - Ignoring some values </li>
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#ignoring_some_returned_values">MDN -
Destructuring assignment / Ignoring some values</a> </li>
</ul>
<h3>Articles &amp; blog posts</h3>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h2>Why is this an issue?</h2>
}
</pre>
<p>Identify and remove unreachable statements from your code.</p>
<pre data-diff-id="1" data-diff-type="noncompliant">
<pre data-diff-id="1" data-diff-type="compliant">
function func(a) {
let i = 10;
return i + a;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Loop counters should not be assigned to from within the loop body",
"title": "Loop counters should not be assigned within the loop body",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<h2>Why is this an issue?</h2>
<p>Most checks against an <code>indexOf</code> value compare it with -1 because 0 is a valid index. Checking against <code>&gt; 0</code> ignores the
first element, which is likely a bug.</p>
<pre>
var arr = ["blue", "red"];
<pre data-diff-id="1" data-diff-type="noncompliant">
let arr = ["blue", "red"];

if (arr.indexOf("blue") &gt; 0) { // Noncompliant
// ...
}
</pre>
<p>Moreover, if the intent is merely to check the presence of the element, and if your browser version supports it, consider using
<code>includes</code> instead.</p>
<pre>
var arr = ["blue", "red"];
<pre data-diff-id="1" data-diff-type="compliant">
let arr = ["blue", "red"];

if (arr.includes("blue")) {
// ...
}
</pre>
<p>This rule raises an issue when an <code>indexOf</code> value retrieved from an array is tested against <code>&gt; 0</code>.</p>
<h2>Resources</h2>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes">Array.prototype.includes()</a>
documentation at MDN</p>
<h3>Documentation</h3>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes">MDN -
<code>Array.prototype.includes()</code></a></p>

Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ <h2>Why is this an issue?</h2>
throw ex;
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch">MDN - <code>try...catch</code></a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw">MDN - <code>throw</code></a> </li>
</ul>

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "\"new\" operators should be used with functions",
"title": "\"new\" should only be used with functions and classes",
"type": "BUG",
"status": "ready",
"remediation": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Variables declared with \"const\" should not be reassigned",
"title": "\"const\" variables should not be reassigned",
"type": "BUG",
"status": "ready",
"remediation": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h2>Why is this an issue?</h2>
concise and expressive by directly extracting values or properties needed from arrays or objects. However, it is possible to define an empty pattern
that has no effect, where no variables are bound to the destructured values.</p>
<pre data-diff-id="1" data-diff-type="noncompliant">
var {a: {}} = myObj; // Noncompliant: this does not create any variable
let {a: {}} = myObj; // Noncompliant: this does not create any variable
function foo({p: []}) { // Noncompliant: this does not define any parameter
// ...
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Collection sizes and array length comparisons should make sense",
"title": "Collection size and array length comparisons should make sense",
"type": "BUG",
"status": "ready",
"remediation": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Collection and array contents should be used",
"title": "Collection contents should be used",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ <h2>Why is this an issue?</h2>
const reversed = a.toReversed();
const sorted = b.toSorted();
</pre>
<p>Alternatively, change a mutating method into a non-mutating alternative using the spread syntax (<code>…​</code>) or <code>slice()</code> to create
a copy first.</p>
<p>Alternatively, change a mutating method into a non-mutating alternative using the spread syntax (<code>…​</code>).</p>
<pre data-diff-id="1" data-diff-type="compliant">
const reversed = [...a].reverse();
const sorted = [...b].sort();
</pre>
<p>Or <code>slice()</code> to create a copy first.</p>
<pre data-diff-id="1" data-diff-type="compliant">
const reversed = a.slice().reverse();
const sorted = b.slice().sort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ <h2>Why is this an issue?</h2>
a "dead store".</p>
<p>This rule detects repeatedly adding an element at the same index or key in a collection or adding identical elements to a set.</p>
<pre>
fruits[1] = "banana";
fruits[1] = "apple"; // Noncompliant
fruits[1] = "banana";
fruits[1] = "apple"; // Noncompliant

myMap.set("key", 1);
myMap.set("key", 2); // Noncompliant
myMap.set("key", 1);
myMap.set("key", 2); // Noncompliant

mySet.add(1);
mySet.add(1); // Noncompliant
mySet.add(1);
mySet.add(1); // Noncompliant
</pre>
<p>This practice is redundant and will cause confusion for the reader. More importantly, it is often an error and not what the developer intended to
do.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ <h2>Why is this an issue?</h2>
break;
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch">MDN - <code>switch</code></a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h2>Why is this an issue?</h2>
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain">prototype chain</a>.</p>
<p>When used on an array, it will compare against the indexes of the array, not the values. This is likely not to be the expected behavior.</p>
<pre data-diff-id="1" data-diff-type="noncompliant">
function func1() {
function func() {
const arr = ["a", "b", "c"];

const expectedValue = "b";
Expand All @@ -22,7 +22,7 @@ <h2>Why is this an issue?</h2>

const expectedValue = "b";
if (arr.includes(expectedValue)) {
return expectedValue + " was found in the array";
return expectedValue + " found in the array";
} else {
return expectedValue + " not found";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Promise rejections should not be caught by \u0027try\u0027 block",
"title": "Promise rejections should not be caught by \u0027try\u0027 blocks",
"type": "BUG",
"status": "ready",
"remediation": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,16 @@ <h2>Why is this an issue?</h2>
easier to do so when the test is focused on a single behavior or functionality.</p>
<h2>Resources</h2>
<h3>Documentation</h3>
<p><a href="https://www.chaijs.com/api/bdd/#method_by">Chai.js - <code>.by</code></a> <a href="https://www.chaijs.com/api/bdd/#method_change">Chai.js
- <code>.change</code></a> <a href="https://www.chaijs.com/api/bdd/#method_decrease">Chai.js - <code>.decrease</code></a> <a
href="https://www.chaijs.com/api/bdd/#method_finite">Chai.js - <code>.finite</code></a> <a
href="https://www.chaijs.com/api/bdd/#method_include">Chai.js - <code>.include</code></a> <a
href="https://www.chaijs.com/api/bdd/#method_increase">Chai.js - <code>.increase</code></a> <a
href="https://www.chaijs.com/api/bdd/#method_members">Chai.js - <code>.members</code></a> <a
href="https://www.chaijs.com/api/bdd/#method_ownpropertydescriptor">Chai.js - <code>.ownPropertyDescriptor</code></a> <a
href="https://www.chaijs.com/api/bdd/#method_property">Chai.js - <code>.property</code></a> <a
href="https://www.chaijs.com/api/bdd/#method_throw">Chai.js - <code>.throw</code></a></p>
<ul>
<li> <a href="https://www.chaijs.com/api/bdd/#method_by">Chai.js - <code>.by</code></a> </li>
<li> <a href="https://www.chaijs.com/api/bdd/#method_change">Chai.js - <code>.change</code></a> </li>
<li> <a href="https://www.chaijs.com/api/bdd/#method_decrease">Chai.js - <code>.decrease</code></a> </li>
<li> <a href="https://www.chaijs.com/api/bdd/#method_finite">Chai.js - <code>.finite</code></a> </li>
<li> <a href="https://www.chaijs.com/api/bdd/#method_include">Chai.js - <code>.include</code></a> </li>
<li> <a href="https://www.chaijs.com/api/bdd/#method_increase">Chai.js - <code>.increase</code></a> </li>
<li> <a href="https://www.chaijs.com/api/bdd/#method_members">Chai.js - <code>.members</code></a> </li>
<li> <a href="https://www.chaijs.com/api/bdd/#method_ownpropertydescriptor">Chai.js - <code>.ownPropertyDescriptor</code></a> </li>
<li> <a href="https://www.chaijs.com/api/bdd/#method_property">Chai.js - <code>.property</code></a> </li>
<li> <a href="https://www.chaijs.com/api/bdd/#method_throw">Chai.js - <code>.throw</code></a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ <h2>Why is this an issue?</h2>
}
</pre>
<p>A redundant boolean cast affects code readability. Not only the condition becomes more verbose but it also misleads the reader who might question
the intent behind the extra cast.</p>
<p>The condition can be written without the Boolean cast.</p>
the intent behind the extra cast. The condition can be written without the Boolean cast.</p>
<pre>
if (foo) {
// ...
Expand All @@ -25,11 +24,11 @@ <h2>Why is this an issue?</h2>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#boolean_coercion">MDN Boolean coercion</a>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#boolean_coercion">MDN - Boolean coercion</a>
</li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion">MDN Type coercion</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy">MDN Truthy</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy">MDN Falsy</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion">MDN - Type coercion</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy">MDN - Truthy</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy">MDN - Falsy</a> </li>
</ul>
<h3>Articles &amp; blog posts</h3>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h2>Why is this an issue?</h2>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping">MDN Escaping in Regular expressions</a>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping">MDN - Escaping in Regular expressions</a>
</li>
<li> <a href="https://www.w3schools.com/js/js_strings.asp">W3 schools - JavaScript strings</a> </li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ <h3>How does this work?</h3>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">MDN Promise</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises">MDN Using promises</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">MDN Async function</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE">MDN IIFE</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">MDN - Promise</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises">MDN - Using promises</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">MDN - Async function</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE">MDN - IIFE</a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ <h4>Compliant solution</h4>
foo.toString();
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString">MDN Object.toString()</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString">MDN -
<code>Object.prototype.toString()</code></a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ <h4>Compliant solution</h4>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/classes.html#this-types">this Types</a> </li>
<li> <a href="https://en.wikipedia.org/wiki/Fluent_interface">Fluent interface</a> </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/classes.html#this-types">TypeScript - <code>this</code> Types</a> </li>
<li> <a href="https://en.wikipedia.org/wiki/Fluent_interface">Wikipedia - Fluent interface</a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ <h4>Compliant solution</h4>
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-">Non-null assertion
operator</a> </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-">TypeScript - Non-null
Assertion Operator (Postfix <code>!</code>)</a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h4>Compliant solution</h4>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/generics.html">TypeScript Generics</a> </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-constraints">TypeScript Generic Constraints</a> </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/generics.html">TypeScript - Generics</a> </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-constraints">TypeScript - Generic Constraints</a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h4>Compliant solution</h4>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types">Union Types</a> </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types">Intersection Types</a> </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types">TypeScript - Union Types</a> </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types">TypeScript - Intersection Types</a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ <h4>Compliant solution</h4>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> typescriptlang.org/docs/handbook/enums.html[TypeScript Enums] </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/enums.html">TypeScript - Enums</a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ <h4>Compliant solution</h4>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> typescriptlang.org/docs/handbook/enums.html[TypeScript Enums] </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/enums.html">TypeScript - Enums</a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ <h4>Compliant solution</h4>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining">MDN Optional chaining</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining">MDN - Optional chaining</a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ <h4>Compliant solution</h4>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://www.typescriptlang.org/docs/handbook/enums.html">TypeScript Enums</a> </li>
<li> <a href="https://www.typescriptlang.org/docs/handbook/enums.html">TypeScript - Enums</a> </li>
</ul>

Loading

0 comments on commit 29853cc

Please sign in to comment.