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

Commit 1b2a721

Browse files
no-identical-conditions: provide secondary issue locations (#103)
1 parent 2e7830f commit 1b2a721

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/rules/no-identical-conditions.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@ import { Rule } from "eslint";
2323
import * as estree from "estree";
2424
import { isIfStatement } from "../utils/nodes";
2525
import { areEquivalent } from "../utils/equivalence";
26+
import { report, issueLocation } from "../utils/locations";
2627

2728
const rule: Rule.RuleModule = {
29+
meta: {
30+
schema: [
31+
{
32+
// internal parameter
33+
enum: ["sonar-runtime"],
34+
},
35+
],
36+
},
2837
create(context: Rule.RuleContext) {
2938
return {
3039
IfStatement(node: estree.Node) {
@@ -35,8 +44,15 @@ const rule: Rule.RuleModule = {
3544
if (isIfStatement(statement)) {
3645
if (areEquivalent(condition, statement.test, context.getSourceCode())) {
3746
const line = ifStmt.loc && ifStmt.loc.start.line;
38-
if (line !== undefined) {
39-
context.report({ message: `This branch duplicates the one on line ${line}`, node: statement.test });
47+
if (line && condition.loc) {
48+
report(
49+
context,
50+
{
51+
message: `This branch duplicates the one on line ${line}`,
52+
node: statement.test,
53+
},
54+
[issueLocation(condition.loc, condition.loc, "Original")],
55+
);
4056
}
4157
}
4258
statement = statement.alternate;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ ruleTester.run("no-identical-conditions", rule, {
3939
`,
4040
errors: [{ message: "This branch duplicates the one on line 2", line: 3, column: 18, endColumn: 19 }],
4141
},
42+
{
43+
code: `
44+
if (a) {}
45+
// ^>
46+
else if (a) {}
47+
// ^
48+
`,
49+
options: ["sonar-runtime"],
50+
errors: [
51+
JSON.stringify({
52+
secondaryLocations: [
53+
{
54+
line: 2,
55+
column: 12,
56+
endLine: 2,
57+
endColumn: 13,
58+
message: "Original",
59+
},
60+
],
61+
message: "This branch duplicates the one on line 2",
62+
}),
63+
],
64+
},
4265
{
4366
code: `
4467
if (b) {}

0 commit comments

Comments
 (0)