Skip to content

Commit 2f79087

Browse files
authored
Merge pull request #1253 from github/aeisenberg/codeSnippet-handling
2 parents 0efd029 + 43f314b commit 2f79087

4 files changed

Lines changed: 69 additions & 20 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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,33 @@ const FileCodeSnippet = ({
180180
messageChildren,
181181
}: {
182182
fileLink: FileLink,
183-
codeSnippet: CodeSnippet,
183+
codeSnippet?: CodeSnippet,
184184
highlightedRegion?: HighlightedRegion,
185185
severity?: ResultSeverity,
186186
message?: AnalysisMessage,
187187
messageChildren?: React.ReactNode,
188188
}) => {
189189

190-
const code = codeSnippet.text.split('\n');
191-
192-
const startingLine = codeSnippet.startLine;
193-
const endingLine = codeSnippet.endLine;
190+
const startingLine = codeSnippet?.startLine || 0;
191+
const endingLine = codeSnippet?.endLine || 0;
194192

195193
const titleFileUri = createRemoteFileRef(
196194
fileLink,
197195
startingLine,
198196
endingLine);
199197

198+
if (!codeSnippet) {
199+
return (
200+
<Container>
201+
<TitleContainer>
202+
<Link href={titleFileUri}>{fileLink.filePath}</Link>
203+
</TitleContainer>
204+
</Container>
205+
);
206+
}
207+
208+
const code = codeSnippet.text.split('\n');
209+
200210
return (
201211
<Container>
202212
<TitleContainer>

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 use highlightedRegion 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[] }) {
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)