Skip to content

Commit 3b16d31

Browse files
committed
Delete unused fixInvalidNotifications function
1 parent 40aec38 commit 3b16d31

File tree

2 files changed

+2
-148
lines changed

2 files changed

+2
-148
lines changed

src/sarif/index.test.ts

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@ import * as fs from "fs";
22

33
import test from "ava";
44

5-
import {
6-
getRecordingLogger,
7-
LoggedMessage,
8-
setupTests,
9-
} from "../testing-utils";
5+
import { setupTests } from "../testing-utils";
106

11-
import {
12-
fixInvalidNotifications,
13-
getToolNames,
14-
SarifLocation,
15-
type SarifFile,
16-
} from ".";
7+
import { getToolNames, type SarifFile } from ".";
178

189
setupTests(test);
1910

@@ -25,64 +16,3 @@ test("getToolNames", (t) => {
2516
const toolNames = getToolNames(JSON.parse(input) as SarifFile);
2617
t.deepEqual(toolNames, ["CodeQL command-line toolchain", "ESLint"]);
2718
});
28-
29-
function createMockSarifWithNotification(
30-
locations: SarifLocation[],
31-
): SarifFile {
32-
return {
33-
runs: [
34-
{
35-
tool: {
36-
driver: {
37-
name: "CodeQL",
38-
},
39-
},
40-
invocations: [
41-
{
42-
toolExecutionNotifications: [
43-
{
44-
locations,
45-
},
46-
],
47-
},
48-
],
49-
},
50-
],
51-
};
52-
}
53-
54-
const stubLocation: SarifLocation = {
55-
physicalLocation: {
56-
artifactLocation: {
57-
uri: "file1",
58-
},
59-
},
60-
};
61-
62-
test("fixInvalidNotifications leaves notifications with unique locations alone", (t) => {
63-
const messages: LoggedMessage[] = [];
64-
const result = fixInvalidNotifications(
65-
createMockSarifWithNotification([stubLocation]),
66-
getRecordingLogger(messages),
67-
);
68-
t.deepEqual(result, createMockSarifWithNotification([stubLocation]));
69-
t.is(messages.length, 1);
70-
t.deepEqual(messages[0], {
71-
type: "debug",
72-
message: "No duplicate locations found in SARIF notification objects.",
73-
});
74-
});
75-
76-
test("fixInvalidNotifications removes duplicate locations", (t) => {
77-
const messages: LoggedMessage[] = [];
78-
const result = fixInvalidNotifications(
79-
createMockSarifWithNotification([stubLocation, stubLocation]),
80-
getRecordingLogger(messages),
81-
);
82-
t.deepEqual(result, createMockSarifWithNotification([stubLocation]));
83-
t.is(messages.length, 1);
84-
t.deepEqual(messages[0], {
85-
type: "info",
86-
message: "Removed 1 duplicate locations from SARIF notification objects.",
87-
});
88-
});

src/sarif/index.ts

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -107,82 +107,6 @@ export function getToolNames(sarif: SarifFile): string[] {
107107
return Object.keys(toolNames);
108108
}
109109

110-
export function removeDuplicateLocations(
111-
locations: SarifLocation[],
112-
): SarifLocation[] {
113-
const newJsonLocations = new Set<string>();
114-
return locations.filter((location) => {
115-
const jsonLocation = JSON.stringify(location);
116-
if (!newJsonLocations.has(jsonLocation)) {
117-
newJsonLocations.add(jsonLocation);
118-
return true;
119-
}
120-
return false;
121-
});
122-
}
123-
124-
export function fixInvalidNotifications(
125-
sarif: SarifFile,
126-
logger: Logger,
127-
): SarifFile {
128-
if (!Array.isArray(sarif.runs)) {
129-
return sarif;
130-
}
131-
132-
// Ensure that the array of locations for each SARIF notification contains unique locations.
133-
// This is a workaround for a bug in the CodeQL CLI that causes duplicate locations to be
134-
// emitted in some cases.
135-
let numDuplicateLocationsRemoved = 0;
136-
137-
const newSarif = {
138-
...sarif,
139-
runs: sarif.runs.map((run) => {
140-
if (
141-
run.tool?.driver?.name !== "CodeQL" ||
142-
!Array.isArray(run.invocations)
143-
) {
144-
return run;
145-
}
146-
return {
147-
...run,
148-
invocations: run.invocations.map((invocation) => {
149-
if (!Array.isArray(invocation.toolExecutionNotifications)) {
150-
return invocation;
151-
}
152-
return {
153-
...invocation,
154-
toolExecutionNotifications:
155-
invocation.toolExecutionNotifications.map((notification) => {
156-
if (!Array.isArray(notification.locations)) {
157-
return notification;
158-
}
159-
const newLocations = removeDuplicateLocations(
160-
notification.locations,
161-
);
162-
numDuplicateLocationsRemoved +=
163-
notification.locations.length - newLocations.length;
164-
return {
165-
...notification,
166-
locations: newLocations,
167-
};
168-
}),
169-
};
170-
}),
171-
};
172-
}),
173-
};
174-
175-
if (numDuplicateLocationsRemoved > 0) {
176-
logger.info(
177-
`Removed ${numDuplicateLocationsRemoved} duplicate locations from SARIF notification ` +
178-
"objects.",
179-
);
180-
} else {
181-
logger.debug("No duplicate locations found in SARIF notification objects.");
182-
}
183-
return newSarif;
184-
}
185-
186110
export function readSarifFile(sarifFilePath: string): SarifFile {
187111
return JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as SarifFile;
188112
}

0 commit comments

Comments
 (0)