Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 792914a

Browse files
vilchik-elenaraspopov.iv
andauthored
improve 'no-identical-functions': add option for min function size (#325)
* Add min lines configuration for no-identical-functions rule * Improve options schema * Bring back classic options schema Co-authored-by: raspopov.iv <[email protected]>
1 parent 033e87e commit 792914a

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

docs/rules/no-identical-functions.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@ function calculateCode() {
2727
doOtherThing();
2828
return code;
2929
}
30-
30+
3131
function getName() {
3232
return calculateCode();
3333
}
3434
```
3535

36-
## Exceptions
36+
## Configuration
37+
38+
This rule has a numeric option (defaulted to 3) to specify the minimum number of lines to trigger an issue. Lines between curly braces are taken into consideration.
3739

38-
Functions with fewer than 3 lines are ignored.
40+
```json
41+
{
42+
"no-identical-functions": "error",
43+
"no-identical-functions": ["error", 5]
44+
}
45+
```

src/rules/no-identical-functions.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { areEquivalent } from '../utils/equivalence';
2424
import { getMainFunctionTokenLocation, report, issueLocation } from '../utils/locations';
2525
import docsUrl from '../utils/docs-url';
2626

27+
const DEFAULT_MIN_LINES = 3;
28+
2729
type FunctionNode =
2830
| TSESTree.FunctionDeclaration
2931
| TSESTree.FunctionExpression
@@ -32,7 +34,9 @@ type FunctionNode =
3234
const message =
3335
'Update this function so that its implementation is not identical to the one on line {{line}}.';
3436

35-
const rule: TSESLint.RuleModule<string, string[]> = {
37+
type Options = (number | 'sonar-runtime')[];
38+
39+
const rule: TSESLint.RuleModule<string, Options> = {
3640
meta: {
3741
messages: {
3842
identicalFunctions: message,
@@ -45,13 +49,16 @@ const rule: TSESLint.RuleModule<string, string[]> = {
4549
url: docsUrl(__filename),
4650
},
4751
schema: [
52+
{ type: 'integer', minimum: 3 },
4853
{
4954
enum: ['sonar-runtime'],
5055
},
5156
],
5257
},
53-
create(context: TSESLint.RuleContext<string, string[]>) {
58+
create(context) {
5459
const functions: Array<{ function: FunctionNode; parent: TSESTree.Node | undefined }> = [];
60+
const minLines: number =
61+
typeof context.options[0] === 'number' ? context.options[0] : DEFAULT_MIN_LINES;
5562

5663
return {
5764
FunctionDeclaration(node: TSESTree.Node) {
@@ -136,7 +143,7 @@ const rule: TSESLint.RuleModule<string, string[]> = {
136143
const firstLine = tokens[0].loc.start.line;
137144
const lastLine = tokens[tokens.length - 1].loc.end.line;
138145

139-
return lastLine - firstLine > 1;
146+
return lastLine - firstLine + 1 >= minLines;
140147
}
141148

142149
return false;

tests/rules/no-identical-functions.test.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ ruleTester.run('no-identical-functions', rule, {
7070
)
7171
`,
7272
},
73+
{
74+
code: `
75+
const x = {
76+
foo() {
77+
console.log("Hello");
78+
console.log("World");
79+
return 42;
80+
},
81+
82+
bar() {
83+
console.log("Hello");
84+
console.log("World");
85+
return 42;
86+
},
87+
};`,
88+
options: [4],
89+
},
7390
],
7491
invalid: [
7592
{
@@ -300,7 +317,7 @@ ruleTester.run('no-identical-functions', rule, {
300317
return 42;
301318
},
302319
};`,
303-
options: ['sonar-runtime'],
320+
options: [3, 'sonar-runtime'],
304321
errors: [
305322
encodedMessage(3, 10, [
306323
{ line: 3, column: 8, endLine: 3, endColumn: 11, message: 'Original implementation' },
@@ -314,17 +331,19 @@ ruleTester.run('no-identical-functions', rule, {
314331
// ^^^^>
315332
return [
316333
1,
334+
2,
317335
];
318336
}
319337
function bar1() {
320338
// ^^^^
321339
return [
322340
1,
341+
2,
323342
];
324343
}`,
325-
options: ['sonar-runtime'],
344+
options: [4, 'sonar-runtime'],
326345
errors: [
327-
encodedMessage(2, 8, [
346+
encodedMessage(2, 9, [
328347
{ line: 2, column: 15, endLine: 2, endColumn: 19, message: 'Original implementation' },
329348
]),
330349
],

0 commit comments

Comments
 (0)