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

Commit 750c66c

Browse files
no-identical-functions: provide secondary issue locations (#101)
1 parent afef3fc commit 750c66c

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

src/rules/no-identical-functions.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@
2222
import { Rule } from "eslint";
2323
import * as estree from "estree";
2424
import { areEquivalent } from "../utils/equivalence";
25-
import { getMainFunctionTokenLocation } from "../utils/locations";
25+
import { getMainFunctionTokenLocation, report, issueLocation } from "../utils/locations";
2626
import { getParent } from "../utils/nodes";
2727

28-
const MESSAGE = "Update this function so that its implementation is not identical to the one on line {{line}}.";
28+
const message = (line: string) =>
29+
`Update this function so that its implementation is not identical to the one on line ${line}.`;
2930

3031
const rule: Rule.RuleModule = {
32+
meta: {
33+
schema: [
34+
{
35+
enum: ["sonar-runtime"],
36+
},
37+
],
38+
},
3139
create(context: Rule.RuleContext) {
3240
const functions: Array<{ function: estree.Function; parent: estree.Node | undefined }> = [];
3341

@@ -67,11 +75,18 @@ const rule: Rule.RuleModule = {
6775
originalFunction.loc
6876
) {
6977
const loc = getMainFunctionTokenLocation(duplicatingFunction, functions[i].parent, context);
70-
context.report({
71-
message: MESSAGE,
72-
data: { line: String(originalFunction.loc.start.line) },
73-
loc,
74-
});
78+
const originalFunctionLoc = getMainFunctionTokenLocation(originalFunction, functions[j].parent, context);
79+
const secondaryLocations = [
80+
issueLocation(originalFunctionLoc, originalFunctionLoc, "Original implementation"),
81+
];
82+
report(
83+
context,
84+
{
85+
message: message(String(originalFunction.loc.start.line)),
86+
loc,
87+
},
88+
secondaryLocations,
89+
);
7590
break;
7691
}
7792
}

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
2020
import { RuleTester } from "eslint";
21+
import { IssueLocation } from "../../src/utils/locations";
2122

2223
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
2324
import rule = require("../../src/rules/no-identical-functions");
@@ -225,6 +226,48 @@ ruleTester.run("no-identical-functions", rule, {
225226
};`,
226227
errors: [{ line: 9, column: 9, endColumn: 12 }],
227228
},
229+
{
230+
code: `
231+
const x = {
232+
foo() {
233+
//^^^>
234+
console.log("Hello");
235+
console.log("World");
236+
return 42;
237+
},
238+
239+
bar() {
240+
//^^^
241+
console.log("Hello");
242+
console.log("World");
243+
return 42;
244+
},
245+
};`,
246+
options: ["sonar-runtime"],
247+
errors: [
248+
encodedMessage(3, 10, [{ line: 3, column: 8, endLine: 3, endColumn: 11, message: "Original implementation" }]),
249+
],
250+
},
251+
{
252+
// few nodes, but many lines
253+
code: `
254+
function foo1() {
255+
// ^^^^>
256+
return [
257+
1,
258+
];
259+
}
260+
function bar1() {
261+
// ^^^^
262+
return [
263+
1,
264+
];
265+
}`,
266+
options: ["sonar-runtime"],
267+
errors: [
268+
encodedMessage(2, 8, [{ line: 2, column: 15, endLine: 2, endColumn: 19, message: "Original implementation" }]),
269+
],
270+
},
228271
],
229272
});
230273

@@ -235,3 +278,18 @@ function message(originalLine: number, duplicationLine: number): RuleTester.Test
235278
endLine: duplicationLine,
236279
};
237280
}
281+
282+
function encodedMessage(
283+
originalLine: number,
284+
duplicationLine: number,
285+
secondaries: IssueLocation[],
286+
): RuleTester.TestCaseError {
287+
return {
288+
message: JSON.stringify({
289+
secondaryLocations: secondaries,
290+
message: `Update this function so that its implementation is not identical to the one on line ${originalLine}.`,
291+
}),
292+
line: duplicationLine,
293+
endLine: duplicationLine,
294+
};
295+
}

0 commit comments

Comments
 (0)