Skip to content

Commit 76354c4

Browse files
author
Dave Bartolomeo
committed
Fix PR feedback
1 parent 4e310ce commit 76354c4

File tree

2 files changed

+167
-157
lines changed

2 files changed

+167
-157
lines changed

extensions/ql-vscode/src/view/results/AlertTablePathNodeRow.tsx

Lines changed: 8 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import type {
2-
Location as SarifLogLocation,
3-
ReportingDescriptorReference,
4-
ThreadFlowLocation,
5-
Run,
6-
PhysicalLocation,
7-
ArtifactLocation,
8-
ToolComponent,
9-
} from "sarif";
1+
import type { ThreadFlowLocation, Run } from "sarif";
102
import type {
113
PathNode,
124
Result as ResultKeysResult,
@@ -18,150 +10,7 @@ import { selectableZebraStripe } from "./result-table-utils";
1810
import { useCallback, useMemo } from "react";
1911
import { VerticalRule } from "../common/VerticalRule";
2012
import type { UserSettings } from "../../common/interface-types";
21-
22-
/** The definition of a taxon for a data extension model row. */
23-
interface ModelTaxon {
24-
location: SarifLogLocation;
25-
}
26-
27-
/** Resolve an `ArtifactLocation` that might contain a relative reference instead of an absolute
28-
* URI.
29-
*/
30-
function resolveArtifactLocation(
31-
location: ArtifactLocation,
32-
baseUri: URL,
33-
): ArtifactLocation {
34-
if (location.uri === undefined) {
35-
// No URI at all. Just return the original location.
36-
return location;
37-
}
38-
return {
39-
...location,
40-
uri: new URL(location.uri, baseUri).toString(),
41-
};
42-
}
43-
44-
/** Get the URI of the pack's local root directory, if available. */
45-
function getLocalPackUri(extension: ToolComponent): URL | undefined {
46-
if (extension.locations === undefined) {
47-
return undefined;
48-
}
49-
50-
const localPackLocation = extension.locations.find(
51-
(loc) =>
52-
loc.properties !== undefined &&
53-
loc.properties.tags !== undefined &&
54-
loc.properties.tags.includes("CodeQL/LocalPackRoot"),
55-
);
56-
if (localPackLocation === undefined || localPackLocation.uri === undefined) {
57-
return undefined;
58-
}
59-
60-
return new URL(localPackLocation.uri);
61-
}
62-
63-
/** Resolve a `ReportingDescriptorReference` to the `ReportingDescriptor` for the taxon that it
64-
* refers to.
65-
*/
66-
function resolveTaxonDefinition(
67-
run: Run,
68-
taxonRef: ReportingDescriptorReference,
69-
): ModelTaxon | undefined {
70-
const extensions = run.tool.extensions;
71-
if (extensions === undefined) {
72-
return undefined;
73-
}
74-
75-
const extensionIndex = taxonRef.toolComponent?.index;
76-
if (
77-
extensionIndex === undefined ||
78-
extensionIndex < 0 ||
79-
extensionIndex >= extensions.length
80-
) {
81-
return undefined;
82-
}
83-
84-
const extension = extensions[extensionIndex];
85-
if (extension.taxa === undefined) {
86-
return undefined;
87-
}
88-
89-
const localPackUri = getLocalPackUri(extension);
90-
if (localPackUri === undefined) {
91-
return undefined;
92-
}
93-
94-
const taxonIndex = taxonRef.index;
95-
if (
96-
taxonIndex === undefined ||
97-
taxonIndex < 0 ||
98-
taxonIndex >= extension.taxa.length
99-
) {
100-
return undefined;
101-
}
102-
103-
const taxonDef = extension.taxa[taxonIndex];
104-
if (taxonDef.properties === undefined) {
105-
return undefined;
106-
}
107-
108-
const location: PhysicalLocation =
109-
taxonDef.properties["CodeQL/DataExtensionLocation"];
110-
if (location === undefined || location.artifactLocation === undefined) {
111-
return undefined;
112-
}
113-
114-
return {
115-
location: {
116-
physicalLocation: {
117-
...location,
118-
artifactLocation: resolveArtifactLocation(
119-
location.artifactLocation,
120-
localPackUri,
121-
),
122-
},
123-
},
124-
};
125-
}
126-
127-
/** Generate the React elements for each taxon. */
128-
function taxaLocations(
129-
taxa: ReportingDescriptorReference[] | undefined,
130-
run: Run | undefined,
131-
onClick: () => void,
132-
) {
133-
if (taxa === undefined || taxa.length === 0 || run === undefined) {
134-
return [];
135-
}
136-
137-
return taxa.flatMap((taxonRef, index) => {
138-
if (taxonRef.properties === undefined) {
139-
return [];
140-
}
141-
142-
const role = taxonRef.properties["CodeQL/DataflowRole"];
143-
if (typeof role !== "string") {
144-
return [];
145-
}
146-
147-
const taxonDef = resolveTaxonDefinition(run, taxonRef);
148-
if (taxonDef === undefined) {
149-
return [];
150-
}
151-
152-
return (
153-
<div key={index}>
154-
{`(${role}) `}
155-
<SarifLocation
156-
loc={taxonDef.location}
157-
databaseUri={undefined}
158-
sourceLocationPrefix=""
159-
onClick={onClick}
160-
/>
161-
</div>
162-
);
163-
});
164-
}
13+
import { TaxaLocations } from "./locations/TaxaLocations";
16514

16615
interface Props {
16716
step: ThreadFlowLocation;
@@ -252,11 +101,13 @@ export function AlertTablePathNodeRow(props: Props) {
252101
>
253102
{userSettings.shouldShowProvenance ? (
254103
<div className="vscode-codeql__taxa-cell-div">
255-
{taxaLocations(step.taxa, run, handleSarifLocationClicked)}
104+
<TaxaLocations
105+
taxa={step.taxa}
106+
run={run}
107+
onClick={handleSarifLocationClicked}
108+
/>
256109
</div>
257-
) : (
258-
[]
259-
)}
110+
) : null}
260111
</td>
261112
<td
262113
{...selectableZebraStripe(
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import type {
2+
Location as SarifLogLocation,
3+
ArtifactLocation,
4+
PhysicalLocation,
5+
ReportingDescriptorReference,
6+
Run,
7+
ToolComponent,
8+
} from "sarif";
9+
import { SarifLocation } from "./SarifLocation";
10+
11+
/** The definition of a taxon for a data extension model row. */
12+
interface ModelTaxon {
13+
location: SarifLogLocation;
14+
}
15+
16+
/** Resolve an `ArtifactLocation` that might contain a relative reference instead of an absolute
17+
* URI.
18+
*/
19+
function resolveArtifactLocation(
20+
location: ArtifactLocation,
21+
baseUri: URL,
22+
): ArtifactLocation {
23+
if (location.uri === undefined) {
24+
// No URI at all. Just return the original location.
25+
return location;
26+
}
27+
return {
28+
...location,
29+
uri: new URL(location.uri, baseUri).toString(),
30+
};
31+
}
32+
33+
/** Get the URI of the pack's local root directory, if available. */
34+
function getLocalPackUri(extension: ToolComponent): URL | undefined {
35+
if (extension.locations === undefined) {
36+
return undefined;
37+
}
38+
39+
const localPackLocation = extension.locations.find(
40+
(loc) =>
41+
loc.properties !== undefined &&
42+
loc.properties.tags !== undefined &&
43+
loc.properties.tags.includes("CodeQL/LocalPackRoot"),
44+
);
45+
if (localPackLocation === undefined || localPackLocation.uri === undefined) {
46+
return undefined;
47+
}
48+
49+
return new URL(localPackLocation.uri);
50+
}
51+
52+
/** Resolve a `ReportingDescriptorReference` to the `ReportingDescriptor` for the taxon that it
53+
* refers to.
54+
*/
55+
function resolveTaxonDefinition(
56+
run: Run,
57+
taxonRef: ReportingDescriptorReference,
58+
): ModelTaxon | undefined {
59+
const extensions = run.tool.extensions;
60+
if (extensions === undefined) {
61+
return undefined;
62+
}
63+
64+
const extensionIndex = taxonRef.toolComponent?.index;
65+
if (
66+
extensionIndex === undefined ||
67+
extensionIndex < 0 ||
68+
extensionIndex >= extensions.length
69+
) {
70+
return undefined;
71+
}
72+
73+
const extension = extensions[extensionIndex];
74+
if (extension.taxa === undefined) {
75+
return undefined;
76+
}
77+
78+
const localPackUri = getLocalPackUri(extension);
79+
if (localPackUri === undefined) {
80+
return undefined;
81+
}
82+
83+
const taxonIndex = taxonRef.index;
84+
if (
85+
taxonIndex === undefined ||
86+
taxonIndex < 0 ||
87+
taxonIndex >= extension.taxa.length
88+
) {
89+
return undefined;
90+
}
91+
92+
const taxonDef = extension.taxa[taxonIndex];
93+
if (taxonDef.properties === undefined) {
94+
return undefined;
95+
}
96+
97+
const location: PhysicalLocation =
98+
taxonDef.properties["CodeQL/DataExtensionLocation"];
99+
if (location === undefined || location.artifactLocation === undefined) {
100+
return undefined;
101+
}
102+
103+
return {
104+
location: {
105+
physicalLocation: {
106+
...location,
107+
artifactLocation: resolveArtifactLocation(
108+
location.artifactLocation,
109+
localPackUri,
110+
),
111+
},
112+
},
113+
};
114+
}
115+
116+
interface Props {
117+
taxa: ReportingDescriptorReference[] | undefined;
118+
run: Run | undefined;
119+
onClick: () => void;
120+
}
121+
122+
/** Generate the React elements for each taxon. */
123+
export function TaxaLocations({
124+
taxa,
125+
run,
126+
onClick,
127+
}: Props): React.JSX.Element[] {
128+
if (taxa === undefined || taxa.length === 0 || run === undefined) {
129+
return [];
130+
}
131+
132+
return taxa.flatMap((taxonRef, index) => {
133+
if (taxonRef.properties === undefined) {
134+
return [];
135+
}
136+
137+
const role = taxonRef.properties["CodeQL/DataflowRole"];
138+
if (typeof role !== "string") {
139+
return [];
140+
}
141+
142+
const taxonDef = resolveTaxonDefinition(run, taxonRef);
143+
if (taxonDef === undefined) {
144+
return [];
145+
}
146+
147+
return (
148+
<div key={index}>
149+
{`(${role}) `}
150+
<SarifLocation
151+
loc={taxonDef.location}
152+
databaseUri={undefined}
153+
sourceLocationPrefix=""
154+
onClick={onClick}
155+
/>
156+
</div>
157+
);
158+
});
159+
}

0 commit comments

Comments
 (0)