|
1 |
| -import type { Result } from "sarif"; |
| 1 | +import type { Location, Result } from "sarif"; |
| 2 | + |
| 3 | +function toCanonicalLocation(location: Location): Location { |
| 4 | + if (location.physicalLocation?.artifactLocation?.index !== undefined) { |
| 5 | + const canonicalLocation = { |
| 6 | + ...location, |
| 7 | + }; |
| 8 | + |
| 9 | + canonicalLocation.physicalLocation = { |
| 10 | + ...canonicalLocation.physicalLocation, |
| 11 | + }; |
| 12 | + |
| 13 | + canonicalLocation.physicalLocation.artifactLocation = { |
| 14 | + ...canonicalLocation.physicalLocation.artifactLocation, |
| 15 | + }; |
| 16 | + |
| 17 | + // The index is dependent on the result of the SARIF file and usually doesn't really tell |
| 18 | + // us anything useful, so we remove it from the comparison. |
| 19 | + delete canonicalLocation.physicalLocation.artifactLocation.index; |
| 20 | + |
| 21 | + return canonicalLocation; |
| 22 | + } |
| 23 | + |
| 24 | + // Don't create a new object if we don't need to |
| 25 | + return location; |
| 26 | +} |
2 | 27 |
|
3 | 28 | function toCanonicalResult(result: Result): Result {
|
4 | 29 | const canonicalResult = {
|
5 | 30 | ...result,
|
6 | 31 | };
|
7 | 32 |
|
8 | 33 | if (canonicalResult.locations) {
|
9 |
| - canonicalResult.locations = canonicalResult.locations.map((location) => { |
10 |
| - if (location.physicalLocation?.artifactLocation?.index !== undefined) { |
11 |
| - const canonicalLocation = { |
12 |
| - ...location, |
13 |
| - }; |
| 34 | + canonicalResult.locations = |
| 35 | + canonicalResult.locations.map(toCanonicalLocation); |
| 36 | + } |
14 | 37 |
|
15 |
| - canonicalLocation.physicalLocation = { |
16 |
| - ...canonicalLocation.physicalLocation, |
17 |
| - }; |
| 38 | + if (canonicalResult.relatedLocations) { |
| 39 | + canonicalResult.relatedLocations = |
| 40 | + canonicalResult.relatedLocations.map(toCanonicalLocation); |
| 41 | + } |
18 | 42 |
|
19 |
| - canonicalLocation.physicalLocation.artifactLocation = { |
20 |
| - ...canonicalLocation.physicalLocation.artifactLocation, |
21 |
| - }; |
| 43 | + if (canonicalResult.codeFlows) { |
| 44 | + canonicalResult.codeFlows = canonicalResult.codeFlows.map((codeFlow) => { |
| 45 | + if (codeFlow.threadFlows) { |
| 46 | + return { |
| 47 | + ...codeFlow, |
| 48 | + threadFlows: codeFlow.threadFlows.map((threadFlow) => { |
| 49 | + if (threadFlow.locations) { |
| 50 | + return { |
| 51 | + ...threadFlow, |
| 52 | + locations: threadFlow.locations.map((threadFlowLocation) => { |
| 53 | + if (threadFlowLocation.location) { |
| 54 | + return { |
| 55 | + ...threadFlowLocation, |
| 56 | + location: toCanonicalLocation( |
| 57 | + threadFlowLocation.location, |
| 58 | + ), |
| 59 | + }; |
| 60 | + } |
22 | 61 |
|
23 |
| - // The index is dependent on the result of the SARIF file and usually doesn't really tell |
24 |
| - // us anything useful, so we remove it from the comparison. |
25 |
| - delete canonicalLocation.physicalLocation.artifactLocation.index; |
| 62 | + return threadFlowLocation; |
| 63 | + }), |
| 64 | + }; |
| 65 | + } |
26 | 66 |
|
27 |
| - return canonicalLocation; |
| 67 | + return threadFlow; |
| 68 | + }), |
| 69 | + }; |
28 | 70 | }
|
29 | 71 |
|
30 |
| - // Don't create a new object if we don't need to |
31 |
| - return location; |
| 72 | + return codeFlow; |
32 | 73 | });
|
33 | 74 | }
|
34 | 75 |
|
|
0 commit comments