Undo sarif-processing change

Will move to a different PR.
This commit is contained in:
Andrew Eisenberg
2022-03-29 13:06:44 -07:00
parent ffe7fdcb46
commit 006cc8c52a
4 changed files with 19 additions and 58 deletions

View File

@@ -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, physicalLocation.region); const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!);
const highlightedRegion = physicalLocation.region const highlightedRegion = physicalLocation.region
? getHighlightedRegion(physicalLocation.region) ? getHighlightedRegion(physicalLocation.region!)
: undefined; : undefined;
const analysisAlert: AnalysisAlert = { const analysisAlert: AnalysisAlert = {
@@ -156,21 +156,15 @@ export function tryGetRule(
return undefined; return undefined;
} }
function getCodeSnippet(region?: sarif.Region, alternateRegion?: sarif.Region): CodeSnippet | undefined { function getCodeSnippet(region: sarif.Region): CodeSnippet {
region = region ?? alternateRegion; const text = region.snippet!.text!;
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 {
@@ -181,7 +175,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
}; };
@@ -201,7 +195,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, physicalLocation.region); const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!);
const highlightedRegion = physicalLocation.region const highlightedRegion = physicalLocation.region
? getHighlightedRegion(physicalLocation.region) ? getHighlightedRegion(physicalLocation.region)
: undefined; : undefined;

View File

@@ -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[];
} }

View File

@@ -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 || 0; const startingLine = codeSnippet.startLine;
const endingLine = codeSnippet?.endLine || 0; const endingLine = codeSnippet.endLine;
const titleFileUri = createRemoteFileRef( const titleFileUri = createRemoteFileRef(
fileLink, fileLink,

View File

@@ -419,42 +419,15 @@ describe('SARIF processing', () => {
expectResultParsingError(result.errors[0]); expectResultParsingError(result.errors[0]);
}); });
it('should not return errors for result locations with no snippet', () => { it('should return errors for result locations with no context region', () => {
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;
expectNoParsingError(result); expect(result.errors.length).to.equal(1);
expect(actualCodeSnippet).to.deep.equal(expectedCodeSnippet); expectResultParsingError(result.errors[0]);
}); });
it('should not return errors for result locations with no region', () => { it('should not return errors for result locations with no region', () => {
@@ -465,7 +438,6 @@ 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', () => {
@@ -565,9 +537,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;
}); });
@@ -623,14 +595,9 @@ 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: '2.1.0', version: '0.0.1' as sarif.Log.version,
runs: [ runs: [
{ {
results: [ results: [