Skip to content

Commit 61e674e

Browse files
committed
Allow for undefined codeSnippets
This reverts commit 006cc8c.
1 parent 006cc8c commit 61e674e

4 files changed

Lines changed: 58 additions & 19 deletions

File tree

extensions/ql-vscode/src/remote-queries/sarif-processing.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ function extractResultAlerts(
5454
for (const location of result.locations ?? []) {
5555
const physicalLocation = location.physicalLocation!;
5656
const filePath = physicalLocation.artifactLocation!.uri!;
57-
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!);
57+
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion, physicalLocation.region);
5858
const highlightedRegion = physicalLocation.region
59-
? getHighlightedRegion(physicalLocation.region!)
59+
? getHighlightedRegion(physicalLocation.region)
6060
: undefined;
6161

6262
const analysisAlert: AnalysisAlert = {
@@ -156,15 +156,21 @@ export function tryGetRule(
156156
return undefined;
157157
}
158158

159-
function getCodeSnippet(region: sarif.Region): CodeSnippet {
160-
const text = region.snippet!.text!;
159+
function getCodeSnippet(region?: sarif.Region, alternateRegion?: sarif.Region): CodeSnippet | undefined {
160+
region = region ?? alternateRegion;
161+
162+
if (!region) {
163+
return undefined;
164+
}
165+
166+
const text = region.snippet?.text || '';
161167
const { startLine, endLine } = parseSarifRegion(region);
162168

163169
return {
164170
startLine,
165171
endLine,
166172
text
167-
} as CodeSnippet;
173+
};
168174
}
169175

170176
function getHighlightedRegion(region: sarif.Region): HighlightedRegion {
@@ -175,7 +181,7 @@ function getHighlightedRegion(region: sarif.Region): HighlightedRegion {
175181
startColumn,
176182
endLine,
177183

178-
// parseSarifRegion currently shifts the end column by 1 to account
184+
// parseSarifRegion currently shifts the end column by 1 to account
179185
// for the way vscode counts columns so we need to shift it back.
180186
endColumn: endColumn + 1
181187
};
@@ -195,7 +201,7 @@ function getCodeFlows(
195201
for (const threadFlowLocation of threadFlow.locations) {
196202
const physicalLocation = threadFlowLocation!.location!.physicalLocation!;
197203
const filePath = physicalLocation!.artifactLocation!.uri!;
198-
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!);
204+
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion, physicalLocation.region);
199205
const highlightedRegion = physicalLocation.region
200206
? getHighlightedRegion(physicalLocation.region)
201207
: undefined;

extensions/ql-vscode/src/remote-queries/shared/analysis-result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface AnalysisAlert {
2121
shortDescription: string;
2222
severity: ResultSeverity;
2323
fileLink: FileLink;
24-
codeSnippet: CodeSnippet;
24+
codeSnippet?: CodeSnippet;
2525
highlightedRegion?: HighlightedRegion;
2626
codeFlows: CodeFlow[];
2727
}

extensions/ql-vscode/src/remote-queries/view/FileCodeSnippet.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,17 @@ const FileCodeSnippet = ({
181181
messageChildren,
182182
}: {
183183
fileLink: FileLink,
184-
codeSnippet: CodeSnippet,
184+
codeSnippet?: CodeSnippet,
185185
highlightedRegion?: HighlightedRegion,
186186
severity?: ResultSeverity,
187187
message?: AnalysisMessage,
188188
messageChildren?: React.ReactNode,
189189
}) => {
190190

191-
const code = codeSnippet.text.split('\n');
191+
const code = codeSnippet?.text.split('\n') || [];
192192

193-
const startingLine = codeSnippet.startLine;
194-
const endingLine = codeSnippet.endLine;
193+
const startingLine = codeSnippet?.startLine || 0;
194+
const endingLine = codeSnippet?.endLine || 0;
195195

196196
const titleFileUri = createRemoteFileRef(
197197
fileLink,

extensions/ql-vscode/test/pure-tests/sarif-processing.test.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,42 @@ describe('SARIF processing', () => {
419419
expectResultParsingError(result.errors[0]);
420420
});
421421

422-
it('should return errors for result locations with no context region', () => {
422+
it('should not return errors for result locations with no snippet', () => {
423+
const sarif = buildValidSarifLog();
424+
sarif.runs![0]!.results![0]!.locations![0]!.physicalLocation!.contextRegion!.snippet = undefined;
425+
426+
const result = extractAnalysisAlerts(sarif, fakefileLinkPrefix);
427+
428+
const expectedCodeSnippet = {
429+
startLine: result.alerts[0].codeSnippet!.startLine,
430+
endLine: result.alerts[0].codeSnippet!.endLine,
431+
text: ''
432+
};
433+
434+
const actualCodeSnippet = result.alerts[0].codeSnippet;
435+
436+
expect(result).to.be.ok;
437+
expectNoParsingError(result);
438+
expect(actualCodeSnippet).to.deep.equal(expectedCodeSnippet);
439+
});
440+
441+
it('should not return errors for result locations with no contextRegion', () => {
423442
const sarif = buildValidSarifLog();
424443
sarif.runs![0]!.results![0]!.locations![0]!.physicalLocation!.contextRegion = undefined;
425444

426445
const result = extractAnalysisAlerts(sarif, fakefileLinkPrefix);
427446

447+
const expectedCodeSnippet = {
448+
startLine: result.alerts[0].highlightedRegion!.startLine,
449+
endLine: result.alerts[0].highlightedRegion!.endLine,
450+
text: ''
451+
};
452+
453+
const actualCodeSnippet = result.alerts[0].codeSnippet;
454+
428455
expect(result).to.be.ok;
429-
expect(result.errors.length).to.equal(1);
430-
expectResultParsingError(result.errors[0]);
456+
expectNoParsingError(result);
457+
expect(actualCodeSnippet).to.deep.equal(expectedCodeSnippet);
431458
});
432459

433460
it('should not return errors for result locations with no region', () => {
@@ -438,6 +465,7 @@ describe('SARIF processing', () => {
438465

439466
expect(result).to.be.ok;
440467
expect(result.alerts.length).to.equal(1);
468+
expectNoParsingError(result);
441469
});
442470

443471
it('should return errors for result locations with no physical location', () => {
@@ -537,9 +565,9 @@ describe('SARIF processing', () => {
537565
expect(result).to.be.ok;
538566
expect(result.errors.length).to.equal(0);
539567
expect(result.alerts.length).to.equal(3);
540-
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet.text === 'foo')).to.be.ok;
541-
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet.text === 'bar')).to.be.ok;
542-
expect(result.alerts.find(a => getMessageText(a.message) === 'msg2' && a.codeSnippet.text === 'baz')).to.be.ok;
568+
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet!.text === 'foo')).to.be.ok;
569+
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet!.text === 'bar')).to.be.ok;
570+
expect(result.alerts.find(a => getMessageText(a.message) === 'msg2' && a.codeSnippet!.text === 'baz')).to.be.ok;
543571
expect(result.alerts.every(a => a.severity === 'Warning')).to.be.true;
544572
});
545573

@@ -595,9 +623,14 @@ describe('SARIF processing', () => {
595623
expect(msg.startsWith('Error when processing SARIF result')).to.be.true;
596624
}
597625

626+
function expectNoParsingError(result: { errors: string[] | undefined }) {
627+
const array = result.errors || [];
628+
expect(array.length, array.join()).to.equal(0);
629+
}
630+
598631
function buildValidSarifLog(): sarif.Log {
599632
return {
600-
version: '0.0.1' as sarif.Log.version,
633+
version: '2.1.0',
601634
runs: [
602635
{
603636
results: [

0 commit comments

Comments
 (0)