Skip to content

Commit c0ffb7e

Browse files
committed
Lift parseSarifLocation out of class
1 parent 3e8c53b commit c0ffb7e

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed

extensions/ql-vscode/src/view/alert-table.tsx

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -133,67 +133,6 @@ export class PathTable extends React.Component<PathTableProps, PathTableState> {
133133
return <span title={locationHint}>{msg}</span>;
134134
}
135135

136-
function parseSarifLocation(loc: Sarif.Location): ParsedSarifLocation {
137-
const physicalLocation = loc.physicalLocation;
138-
if (physicalLocation === undefined)
139-
return { t: 'NoLocation', hint: 'no physical location' };
140-
if (physicalLocation.artifactLocation === undefined)
141-
return { t: 'NoLocation', hint: 'no artifact location' };
142-
if (physicalLocation.artifactLocation.uri === undefined)
143-
return { t: 'NoLocation', hint: 'artifact location has no uri' };
144-
145-
// This is not necessarily really an absolute uri; it could either be a
146-
// file uri or a relative uri.
147-
const uri = physicalLocation.artifactLocation.uri;
148-
149-
const fileUriRegex = /^file:/;
150-
const effectiveLocation = uri.match(fileUriRegex) ?
151-
decodeURIComponent(uri.replace(fileUriRegex, '')) :
152-
getPathRelativeToSourceLocationPrefix(sourceLocationPrefix, uri);
153-
const userVisibleFile = uri.match(fileUriRegex) ?
154-
decodeURIComponent(uri.replace(fileUriRegex, '')) :
155-
uri;
156-
157-
if (physicalLocation.region === undefined) {
158-
// If the region property is absent, the physicalLocation object refers to the entire file.
159-
// Source: https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/sarif-v2.1.0-cs01.html#_Toc16012638.
160-
// TODO: Do we get here if we provide a non-filesystem URL?
161-
return {
162-
t: LocationStyle.WholeFile,
163-
file: effectiveLocation,
164-
userVisibleFile,
165-
};
166-
} else {
167-
const region = physicalLocation.region;
168-
// We assume that the SARIF we're given always has startLine
169-
// This is not mandated by the SARIF spec, but should be true of
170-
// SARIF output by our own tools.
171-
const lineStart = region.startLine!;
172-
173-
// These defaults are from SARIF 2.1.0 spec, section 3.30.2, "Text Regions"
174-
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/sarif-v2.1.0-cs01.html#_Ref493492556
175-
const lineEnd = region.endLine === undefined ? lineStart : region.endLine;
176-
const colStart = region.startColumn === undefined ? 1 : region.startColumn;
177-
178-
// We also assume that our tools will always supply `endColumn` field, which is
179-
// fortunate, since the SARIF spec says that it defaults to the end of the line, whose
180-
// length we don't know at this point in the code.
181-
//
182-
// It is off by one with respect to the way vscode counts columns in selections.
183-
const colEnd = region.endColumn! - 1;
184-
185-
return {
186-
t: LocationStyle.FivePart,
187-
file: effectiveLocation,
188-
userVisibleFile,
189-
lineStart,
190-
colStart,
191-
lineEnd,
192-
colEnd,
193-
};
194-
}
195-
}
196-
197136
const updateSelectionCallback = (pathNodeKey: Keys.PathNode | undefined) => {
198137
return () => {
199138
this.setState(previousState => ({
@@ -204,7 +143,7 @@ export class PathTable extends React.Component<PathTableProps, PathTableState> {
204143
};
205144

206145
function renderSarifLocationWithText(text: string | undefined, loc: Sarif.Location, pathNodeKey: Keys.PathNode | undefined): JSX.Element | undefined {
207-
const parsedLoc = parseSarifLocation(loc);
146+
const parsedLoc = parseSarifLocation(loc, sourceLocationPrefix);
208147
switch (parsedLoc.t) {
209148
case 'NoLocation':
210149
return renderNonLocation(text, parsedLoc.hint);
@@ -220,7 +159,7 @@ export class PathTable extends React.Component<PathTableProps, PathTableState> {
220159
* human-readable form of the location itself.
221160
*/
222161
function renderSarifLocation(loc: Sarif.Location, pathNodeKey: Keys.PathNode | undefined): JSX.Element | undefined {
223-
const parsedLoc = parseSarifLocation(loc);
162+
const parsedLoc = parseSarifLocation(loc, sourceLocationPrefix);
224163
let shortLocation, longLocation: string;
225164
switch (parsedLoc.t) {
226165
case 'NoLocation':
@@ -352,3 +291,64 @@ export class PathTable extends React.Component<PathTableProps, PathTableState> {
352291
</table>;
353292
}
354293
}
294+
295+
function parseSarifLocation(loc: Sarif.Location, sourceLocationPrefix: string): ParsedSarifLocation {
296+
const physicalLocation = loc.physicalLocation;
297+
if (physicalLocation === undefined)
298+
return { t: 'NoLocation', hint: 'no physical location' };
299+
if (physicalLocation.artifactLocation === undefined)
300+
return { t: 'NoLocation', hint: 'no artifact location' };
301+
if (physicalLocation.artifactLocation.uri === undefined)
302+
return { t: 'NoLocation', hint: 'artifact location has no uri' };
303+
304+
// This is not necessarily really an absolute uri; it could either be a
305+
// file uri or a relative uri.
306+
const uri = physicalLocation.artifactLocation.uri;
307+
308+
const fileUriRegex = /^file:/;
309+
const effectiveLocation = uri.match(fileUriRegex) ?
310+
decodeURIComponent(uri.replace(fileUriRegex, '')) :
311+
getPathRelativeToSourceLocationPrefix(sourceLocationPrefix, uri);
312+
const userVisibleFile = uri.match(fileUriRegex) ?
313+
decodeURIComponent(uri.replace(fileUriRegex, '')) :
314+
uri;
315+
316+
if (physicalLocation.region === undefined) {
317+
// If the region property is absent, the physicalLocation object refers to the entire file.
318+
// Source: https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/sarif-v2.1.0-cs01.html#_Toc16012638.
319+
// TODO: Do we get here if we provide a non-filesystem URL?
320+
return {
321+
t: LocationStyle.WholeFile,
322+
file: effectiveLocation,
323+
userVisibleFile,
324+
};
325+
} else {
326+
const region = physicalLocation.region;
327+
// We assume that the SARIF we're given always has startLine
328+
// This is not mandated by the SARIF spec, but should be true of
329+
// SARIF output by our own tools.
330+
const lineStart = region.startLine!;
331+
332+
// These defaults are from SARIF 2.1.0 spec, section 3.30.2, "Text Regions"
333+
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/sarif-v2.1.0-cs01.html#_Ref493492556
334+
const lineEnd = region.endLine === undefined ? lineStart : region.endLine;
335+
const colStart = region.startColumn === undefined ? 1 : region.startColumn;
336+
337+
// We also assume that our tools will always supply `endColumn` field, which is
338+
// fortunate, since the SARIF spec says that it defaults to the end of the line, whose
339+
// length we don't know at this point in the code.
340+
//
341+
// It is off by one with respect to the way vscode counts columns in selections.
342+
const colEnd = region.endColumn! - 1;
343+
344+
return {
345+
t: LocationStyle.FivePart,
346+
file: effectiveLocation,
347+
userVisibleFile,
348+
lineStart,
349+
colStart,
350+
lineEnd,
351+
colEnd,
352+
};
353+
}
354+
}

0 commit comments

Comments
 (0)