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

Commit df14ad9

Browse files
no-duplicated-branches: provide secondary issue locations (#112)
1 parent 7ef8e71 commit df14ad9

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

src/rules/no-duplicated-branches.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@ import * as estree from "estree";
2424
import { getParent, isIfStatement, isBlockStatement } from "../utils/nodes";
2525
import { areEquivalent } from "../utils/equivalence";
2626
import { collectIfBranches, takeWithoutBreak, collectSwitchBranches } from "../utils/conditions";
27+
import { report, issueLocation } from "../utils/locations";
2728

2829
const MESSAGE = "This {{type}}'s code block is the same as the block for the {{type}} on line {{line}}.";
2930

3031
const rule: Rule.RuleModule = {
32+
meta: {
33+
schema: [
34+
{
35+
// internal parameter
36+
enum: ["sonar-runtime"],
37+
},
38+
],
39+
},
3140
create(context: Rule.RuleContext) {
3241
return {
3342
IfStatement(node: estree.Node) {
@@ -126,14 +135,10 @@ const rule: Rule.RuleModule = {
126135
}
127136

128137
function reportIssue(node: estree.Node, equivalentNode: estree.Node, type: string) {
129-
context.report({
130-
message: MESSAGE,
131-
data: {
132-
type,
133-
line: String(equivalentNode.loc!.start.line),
134-
},
135-
node,
136-
});
138+
const equivalentNodeLoc = equivalentNode.loc as estree.SourceLocation;
139+
report(context, { message: MESSAGE, data: { type, line: String(equivalentNode.loc!.start.line) }, node }, [
140+
issueLocation(equivalentNodeLoc, equivalentNodeLoc, "Original"),
141+
]);
137142
}
138143
},
139144
};

tests/rules/no-duplicated-branches.test.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,24 @@ ruleTester.run("no-duplicated-branches if", rule, {
122122
first();
123123
second();
124124
}`,
125-
errors: [{ line: 5 }],
125+
options: ["sonar-runtime"],
126+
errors: [
127+
{
128+
line: 5,
129+
message: JSON.stringify({
130+
secondaryLocations: [
131+
{
132+
line: 2,
133+
column: 13,
134+
endLine: 5,
135+
endColumn: 7,
136+
message: "Original",
137+
},
138+
],
139+
message: "This branch's code block is the same as the block for the branch on line 2.",
140+
}),
141+
},
142+
],
126143
},
127144
{
128145
code: `
@@ -249,7 +266,24 @@ ruleTester.run("no-duplicated-branches switch", rule, {
249266
second();
250267
break;
251268
}`,
252-
errors: [{ line: 7 }],
269+
options: ["sonar-runtime"],
270+
errors: [
271+
{
272+
line: 7,
273+
message: JSON.stringify({
274+
secondaryLocations: [
275+
{
276+
line: 3,
277+
column: 8,
278+
endLine: 6,
279+
endColumn: 16,
280+
message: "Original",
281+
},
282+
],
283+
message: `This case's code block is the same as the block for the case on line 3.`,
284+
}),
285+
},
286+
],
253287
},
254288
{
255289
code: `

0 commit comments

Comments
 (0)