Skip to content

Commit 006cc8c

Browse files
committed
Undo sarif-processing change
Will move to a different PR.
1 parent ffe7fdc commit 006cc8c

4 files changed

Lines changed: 19 additions & 58 deletions

File tree

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

Lines changed: 7 additions & 13 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, physicalLocation.region);
57+
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!);
5858
const highlightedRegion = physicalLocation.region
59-
? getHighlightedRegion(physicalLocation.region)
59+
? getHighlightedRegion(physicalLocation.region!)
6060
: undefined;
6161

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

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 || '';
159+
function getCodeSnippet(region: sarif.Region): CodeSnippet {
160+
const text = region.snippet!.text!;
167161
const { startLine, endLine } = parseSarifRegion(region);
168162

169163
return {
170164
startLine,
171165
endLine,
172166
text
173-
};
167+
} as CodeSnippet;
174168
}
175169

176170
function getHighlightedRegion(region: sarif.Region): HighlightedRegion {
@@ -181,7 +175,7 @@ function getHighlightedRegion(region: sarif.Region): HighlightedRegion {
181175
startColumn,
182176
endLine,
183177

184-
// parseSarifRegion currently shifts the end column by 1 to account
178+
// parseSarifRegion currently shifts the end column by 1 to account
185179
// for the way vscode counts columns so we need to shift it back.
186180
endColumn: endColumn + 1
187181
};
@@ -201,7 +195,7 @@ function getCodeFlows(
201195
for (const threadFlowLocation of threadFlow.locations) {
202196
const physicalLocation = threadFlowLocation!.location!.physicalLocation!;
203197
const filePath = physicalLocation!.artifactLocation!.uri!;
204-
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion, physicalLocation.region);
198+
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!);
205199
const highlightedRegion = physicalLocation.region
206200
? getHighlightedRegion(physicalLocation.region)
207201
: 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 || 0;
194-
const endingLine = codeSnippet?.endLine || 0;
193+
const startingLine = codeSnippet.startLine;
194+
const endingLine = codeSnippet.endLine;
195195

196196
const titleFileUri = createRemoteFileRef(
197197
fileLink,

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

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

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', () => {
422+
it('should return errors for result locations with no context region', () => {
442423
const sarif = buildValidSarifLog();
443424
sarif.runs![0]!.results![0]!.locations![0]!.physicalLocation!.contextRegion = undefined;
444425

445426
const result = extractAnalysisAlerts(sarif, fakefileLinkPrefix);
446427

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-
455428
expect(result).to.be.ok;
456-
expectNoParsingError(result);
457-
expect(actualCodeSnippet).to.deep.equal(expectedCodeSnippet);
429+
expect(result.errors.length).to.equal(1);
430+
expectResultParsingError(result.errors[0]);
458431
});
459432

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

466439
expect(result).to.be.ok;
467440
expect(result.alerts.length).to.equal(1);
468-
expectNoParsingError(result);
469441
});
470442

471443
it('should return errors for result locations with no physical location', () => {
@@ -565,9 +537,9 @@ describe('SARIF processing', () => {
565537
expect(result).to.be.ok;
566538
expect(result.errors.length).to.equal(0);
567539
expect(result.alerts.length).to.equal(3);
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;
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;
571543
expect(result.alerts.every(a => a.severity === 'Warning')).to.be.true;
572544
});
573545

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

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

0 commit comments

Comments
 (0)