Skip to content

Commit 9b099a8

Browse files
authored
ESLINTJS-18 Use ESLint built-in rule no-unreachable-loop instead of our own no-one-iteration-loop (#5576)
1 parent d7b6e6f commit 9b099a8

File tree

8 files changed

+66
-144
lines changed

8 files changed

+66
-144
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"angular.js:src/jqLite.js": [
3+
199,
4+
317
5+
]
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"es5-shim:es5-shim.js": [
3+
80
4+
]
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jquery:src/core.js": [
3+
228
4+
]
5+
}

packages/jsts/src/rules/S1751/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
* You should have received a copy of the Sonar Source-Available License
1515
* along with this program; if not, see https://sonarsource.com/license/ssal/
1616
*/
17-
export { rule } from './rule.js';
17+
import { getESLintCoreRule } from '../external/core.js';
18+
export const rule = getESLintCoreRule('no-unreachable-loop');

packages/jsts/src/rules/S1751/meta.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
* You should have received a copy of the Sonar Source-Available License
1515
* along with this program; if not, see https://sonarsource.com/license/ssal/
1616
*/
17-
export const implementation = 'original';
18-
export const eslintId = 'no-one-iteration-loop';
17+
export const implementation = 'external';
18+
export const eslintId = 'no-unreachable-loop';
19+
export const externalPlugin = 'eslint';

packages/jsts/src/rules/S1751/rule.ts

Lines changed: 0 additions & 106 deletions
This file was deleted.

packages/jsts/src/rules/S1751/unit.test.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* You should have received a copy of the Sonar Source-Available License
1515
* along with this program; if not, see https://sonarsource.com/license/ssal/
1616
*/
17-
import { rule } from './rule.js';
17+
import { rule } from './index.js';
1818
import { DefaultParserRuleTester } from '../../../tests/tools/testers/rule-tester.js';
1919
import { describe, it } from 'node:test';
2020

2121
describe('S1751', () => {
2222
it('S1751', () => {
2323
const ruleTester = new DefaultParserRuleTester();
2424

25-
ruleTester.run('no-one-iteration-loop', rule, {
25+
ruleTester.run('no-unreachable-loop', rule, {
2626
valid: [
2727
valid(`
2828
while (cond) {
@@ -86,19 +86,6 @@ describe('S1751', () => {
8686
continue;
8787
}`),
8888

89-
valid(`
90-
function foo() {
91-
for(p of arr) {
92-
return p; // Compliant: used to return the first element of an array
93-
}
94-
}`),
95-
96-
valid(`
97-
for (p in obj) {
98-
bar();
99-
break; // Compliant: often used to check whether an object is "empty"
100-
}`),
101-
10289
valid(`
10390
while(foo()) {
10491
if (bar()) {
@@ -285,7 +272,7 @@ describe('S1751', () => {
285272
code,
286273
errors: [
287274
{
288-
messageId: 'refactorLoop',
275+
messageId: 'invalid',
289276
},
290277
],
291278
};
Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,60 @@
11
<h2>Why is this an issue?</h2>
22
<p>A selector parameter is a <code>boolean</code> parameter that’s used to determine which of two paths to take through a method. Specifying such a
3-
parameter may seem innocuous, particularly if it’s well named.</p>
3+
parameter may seem innocuous, particularly if it’s well-named.</p>
44
<p>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
5-
take extra time to look the method up.</p>
6-
<p>This rule finds methods with a <code>boolean</code> that’s used to determine which path to take through the method.</p>
5+
take extra time to look the method up. Additionally, selector parameters create maintenance challenges: adding a third option requires changing the
6+
boolean to an enum or similar type, breaking existing code.</p>
7+
<p>This rule finds methods with a <code>boolean</code> that is used to determine which path to take through the method.</p>
78
<h3>Noncompliant code example</h3>
89
<pre>
9-
function tempt(name: string, ofAge: boolean) {
10-
if (ofAge) {
11-
offerLiquor(name);
10+
function feed(name: string, isHuman: boolean) {
11+
if (isHuman) {
12+
// implementation for human
1213
} else {
13-
offerCandy(name);
14+
// implementation for animal
1415
}
1516
}
1617

17-
// ...
18-
function corrupt() {
19-
tempt("Joe", false); // does this mean not to temp Joe?
20-
}
18+
// Intent is not clear at call site
19+
feed('Max', false); // does this mean not to feed Max?
2120
</pre>
2221
<h3>Compliant solution</h3>
23-
<p>Instead, separate methods should be written.</p>
22+
<p>Instead, split the method into separate, clearly named methods:</p>
2423
<pre>
25-
function temptAdult(name: string) {
26-
offerLiquor(name);
24+
function feedHuman(name: string) {
25+
offerSushi(name);
2726
}
2827

29-
function temptChild(name: string) {
30-
offerCandy(name);
28+
function feedAnimal(name: string) {
29+
offerCarrot(name);
3130
}
3231

33-
// ...
34-
function corrupt() {
35-
age &lt; legalAge ? temptChild("Joe") : temptAdult("Joe");
32+
// Clear intent at call site
33+
feedHuman("Joe");
34+
feedAnimal("Max");
35+
</pre>
36+
<p>Alternatively, use descriptive object parameters or types:</p>
37+
<pre>
38+
type EntityType = 'human' | 'animal';
39+
40+
function feed(name: string, entityType: EntityType) {
41+
if (entityType === 'human') {
42+
// implementation for human
43+
} else {
44+
// implementation for animal
45+
}
3646
}
47+
48+
feed('Max', 'animal');
49+
</pre>
50+
<pre>
51+
function feed(name: string, { human = true } ) {
52+
if (human) {
53+
// implementation for human
54+
} else {
55+
// implementation for animal
56+
}
57+
}
58+
59+
feed('Max', { human: false });
3760
</pre>

0 commit comments

Comments
 (0)