Skip to content

Commit

Permalink
🐛 Accept incidents without line number (konveyor#158)
Browse files Browse the repository at this point in the history
Use first line as fallback.

---------

Signed-off-by: Radoslaw Szwajkowski <[email protected]>
  • Loading branch information
rszwajko authored Dec 13, 2024
1 parent 39cdb3b commit d77c244
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
78 changes: 78 additions & 0 deletions vscode/src/issueView/__tests__/transformations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Incident, RuleSet } from "@editor-extensions/shared";
import { produce } from "immer";
import { FOO } from "../../analysis/__tests__/data";
import { allIncidents } from "../transformation";
import expect from "expect";

describe("analysis data transformations", () => {
it("uses first line if line number is missing or incorrect in the incident", () => {
const responseWithoutLineNumber = [
produce(FOO, (draft: RuleSet) => {
draft.violations!["foo-01"].incidents = [
{
uri: "file:///src/Foo.java",
message: "new message",
},
{
uri: "file:///src/Foo.java",
message: "new message",
lineNumber: -1,
},
{
uri: "file:///src/Foo.java",
message: "new message",
lineNumber: 0.3,
},
{
uri: "file:///src/Foo.java",
message: "new message",
lineNumber: "foo",
} as unknown as Incident,
];
}),
];

const result = allIncidents(responseWithoutLineNumber);
expect(result).toHaveLength(4);
result.forEach((res) => expect(res.lineNumber).toBe(1));
});
it("filters out incidents without message", () => {
const response = [
produce(FOO, (draft: RuleSet) => {
draft.violations!["foo-01"].incidents = [
{
uri: "file:///src/Foo.java",
//no message
} as Incident,
{
uri: "file:///src/Foo.java",
message: "",
},
];
}),
];

expect(allIncidents(response)).toHaveLength(1);
});
it("filters out incidents with incorrect URI", () => {
const response = [
produce(FOO, (draft: RuleSet) => {
draft.violations!["foo-01"].incidents = [
{
uri: "/src/Foo.java",
message: "",
},
{
uri: "",
message: "",
},
{
message: "",
} as Incident,
];
}),
];

expect(allIncidents(response)).toHaveLength(0);
});
});
13 changes: 8 additions & 5 deletions vscode/src/issueView/transformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export const allIncidents = (ruleSets: Immutable<RuleSet[]>): Incident[] =>
// allow empty messages (they will be grouped together)
typeof it.message === "string" &&
typeof it.uri === "string" &&
Number.isInteger(it.lineNumber) &&
// expect non-empty path in format file:///some/file.ext
it.uri &&
// expect 1-based numbering (vscode.Position is zero-based)
it.lineNumber! > 0,
);
it.uri.startsWith("file://"),
)
.map((it) => ({
...it,
// line numbers are optional - use first line as fallback
// expect 1-based numbering (vscode.Position is zero-based)
lineNumber: Number.isInteger(it.lineNumber) && it.lineNumber! > 0 ? it.lineNumber : 1,
}));

0 comments on commit d77c244

Please sign in to comment.