Allow for undefined codeSnippets

This reverts commit 006cc8c52a.
This commit is contained in:
Andrew Eisenberg 2022-03-29 13:10:28 -07:00
Родитель 006cc8c52a
Коммит 61e674e9f6
4 изменённых файлов: 58 добавлений и 19 удалений

Просмотреть файл

@ -54,9 +54,9 @@ function extractResultAlerts(
for (const location of result.locations ?? []) { for (const location of result.locations ?? []) {
const physicalLocation = location.physicalLocation!; const physicalLocation = location.physicalLocation!;
const filePath = physicalLocation.artifactLocation!.uri!; const filePath = physicalLocation.artifactLocation!.uri!;
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!); const codeSnippet = getCodeSnippet(physicalLocation.contextRegion, physicalLocation.region);
const highlightedRegion = physicalLocation.region const highlightedRegion = physicalLocation.region
? getHighlightedRegion(physicalLocation.region!) ? getHighlightedRegion(physicalLocation.region)
: undefined; : undefined;
const analysisAlert: AnalysisAlert = { const analysisAlert: AnalysisAlert = {
@ -156,15 +156,21 @@ export function tryGetRule(
return undefined; return undefined;
} }
function getCodeSnippet(region: sarif.Region): CodeSnippet { function getCodeSnippet(region?: sarif.Region, alternateRegion?: sarif.Region): CodeSnippet | undefined {
const text = region.snippet!.text!; region = region ?? alternateRegion;
if (!region) {
return undefined;
}
const text = region.snippet?.text || '';
const { startLine, endLine } = parseSarifRegion(region); const { startLine, endLine } = parseSarifRegion(region);
return { return {
startLine, startLine,
endLine, endLine,
text text
} as CodeSnippet; };
} }
function getHighlightedRegion(region: sarif.Region): HighlightedRegion { function getHighlightedRegion(region: sarif.Region): HighlightedRegion {
@ -175,7 +181,7 @@ function getHighlightedRegion(region: sarif.Region): HighlightedRegion {
startColumn, startColumn,
endLine, endLine,
// parseSarifRegion currently shifts the end column by 1 to account // parseSarifRegion currently shifts the end column by 1 to account
// for the way vscode counts columns so we need to shift it back. // for the way vscode counts columns so we need to shift it back.
endColumn: endColumn + 1 endColumn: endColumn + 1
}; };
@ -195,7 +201,7 @@ function getCodeFlows(
for (const threadFlowLocation of threadFlow.locations) { for (const threadFlowLocation of threadFlow.locations) {
const physicalLocation = threadFlowLocation!.location!.physicalLocation!; const physicalLocation = threadFlowLocation!.location!.physicalLocation!;
const filePath = physicalLocation!.artifactLocation!.uri!; const filePath = physicalLocation!.artifactLocation!.uri!;
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!); const codeSnippet = getCodeSnippet(physicalLocation.contextRegion, physicalLocation.region);
const highlightedRegion = physicalLocation.region const highlightedRegion = physicalLocation.region
? getHighlightedRegion(physicalLocation.region) ? getHighlightedRegion(physicalLocation.region)
: undefined; : undefined;

Просмотреть файл

@ -21,7 +21,7 @@ export interface AnalysisAlert {
shortDescription: string; shortDescription: string;
severity: ResultSeverity; severity: ResultSeverity;
fileLink: FileLink; fileLink: FileLink;
codeSnippet: CodeSnippet; codeSnippet?: CodeSnippet;
highlightedRegion?: HighlightedRegion; highlightedRegion?: HighlightedRegion;
codeFlows: CodeFlow[]; codeFlows: CodeFlow[];
} }

Просмотреть файл

@ -181,17 +181,17 @@ const FileCodeSnippet = ({
messageChildren, messageChildren,
}: { }: {
fileLink: FileLink, fileLink: FileLink,
codeSnippet: CodeSnippet, codeSnippet?: CodeSnippet,
highlightedRegion?: HighlightedRegion, highlightedRegion?: HighlightedRegion,
severity?: ResultSeverity, severity?: ResultSeverity,
message?: AnalysisMessage, message?: AnalysisMessage,
messageChildren?: React.ReactNode, messageChildren?: React.ReactNode,
}) => { }) => {
const code = codeSnippet.text.split('\n'); const code = codeSnippet?.text.split('\n') || [];
const startingLine = codeSnippet.startLine; const startingLine = codeSnippet?.startLine || 0;
const endingLine = codeSnippet.endLine; const endingLine = codeSnippet?.endLine || 0;
const titleFileUri = createRemoteFileRef( const titleFileUri = createRemoteFileRef(
fileLink, fileLink,

Просмотреть файл

@ -419,15 +419,42 @@ describe('SARIF processing', () => {
expectResultParsingError(result.errors[0]); expectResultParsingError(result.errors[0]);
}); });
it('should return errors for result locations with no context region', () => { it('should not return errors for result locations with no snippet', () => {
const sarif = buildValidSarifLog();
sarif.runs![0]!.results![0]!.locations![0]!.physicalLocation!.contextRegion!.snippet = undefined;
const result = extractAnalysisAlerts(sarif, fakefileLinkPrefix);
const expectedCodeSnippet = {
startLine: result.alerts[0].codeSnippet!.startLine,
endLine: result.alerts[0].codeSnippet!.endLine,
text: ''
};
const actualCodeSnippet = result.alerts[0].codeSnippet;
expect(result).to.be.ok;
expectNoParsingError(result);
expect(actualCodeSnippet).to.deep.equal(expectedCodeSnippet);
});
it('should not return errors for result locations with no contextRegion', () => {
const sarif = buildValidSarifLog(); const sarif = buildValidSarifLog();
sarif.runs![0]!.results![0]!.locations![0]!.physicalLocation!.contextRegion = undefined; sarif.runs![0]!.results![0]!.locations![0]!.physicalLocation!.contextRegion = undefined;
const result = extractAnalysisAlerts(sarif, fakefileLinkPrefix); const result = extractAnalysisAlerts(sarif, fakefileLinkPrefix);
const expectedCodeSnippet = {
startLine: result.alerts[0].highlightedRegion!.startLine,
endLine: result.alerts[0].highlightedRegion!.endLine,
text: ''
};
const actualCodeSnippet = result.alerts[0].codeSnippet;
expect(result).to.be.ok; expect(result).to.be.ok;
expect(result.errors.length).to.equal(1); expectNoParsingError(result);
expectResultParsingError(result.errors[0]); expect(actualCodeSnippet).to.deep.equal(expectedCodeSnippet);
}); });
it('should not return errors for result locations with no region', () => { it('should not return errors for result locations with no region', () => {
@ -438,6 +465,7 @@ describe('SARIF processing', () => {
expect(result).to.be.ok; expect(result).to.be.ok;
expect(result.alerts.length).to.equal(1); expect(result.alerts.length).to.equal(1);
expectNoParsingError(result);
}); });
it('should return errors for result locations with no physical location', () => { it('should return errors for result locations with no physical location', () => {
@ -537,9 +565,9 @@ describe('SARIF processing', () => {
expect(result).to.be.ok; expect(result).to.be.ok;
expect(result.errors.length).to.equal(0); expect(result.errors.length).to.equal(0);
expect(result.alerts.length).to.equal(3); expect(result.alerts.length).to.equal(3);
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet.text === 'foo')).to.be.ok; expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet!.text === 'foo')).to.be.ok;
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet.text === 'bar')).to.be.ok; expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet!.text === 'bar')).to.be.ok;
expect(result.alerts.find(a => getMessageText(a.message) === 'msg2' && a.codeSnippet.text === 'baz')).to.be.ok; expect(result.alerts.find(a => getMessageText(a.message) === 'msg2' && a.codeSnippet!.text === 'baz')).to.be.ok;
expect(result.alerts.every(a => a.severity === 'Warning')).to.be.true; expect(result.alerts.every(a => a.severity === 'Warning')).to.be.true;
}); });
@ -595,9 +623,14 @@ describe('SARIF processing', () => {
expect(msg.startsWith('Error when processing SARIF result')).to.be.true; expect(msg.startsWith('Error when processing SARIF result')).to.be.true;
} }
function expectNoParsingError(result: { errors: string[] | undefined }) {
const array = result.errors || [];
expect(array.length, array.join()).to.equal(0);
}
function buildValidSarifLog(): sarif.Log { function buildValidSarifLog(): sarif.Log {
return { return {
version: '0.0.1' as sarif.Log.version, version: '2.1.0',
runs: [ runs: [
{ {
results: [ results: [