Skip to content

Commit 7d3b015

Browse files
committed
Generate markdown for raw result tables
1 parent 7d0d11f commit 7d3b015

3 files changed

Lines changed: 82 additions & 2 deletions

File tree

extensions/ql-vscode/src/remote-queries/remote-queries-markdown-generation.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { CellValue } from '../pure/bqrs-cli-types';
2+
import { tryGetRemoteLocation } from '../pure/bqrs-utils';
13
import { createRemoteFileRef } from '../pure/location-link-utils';
24
import { parseHighlightedLine, shouldHighlightLine } from '../pure/sarif-utils';
5+
import { convertNonPrintableChars } from '../text-utils';
36
import { RemoteQuery } from './remote-query';
4-
import { AnalysisAlert, AnalysisResults, CodeSnippet, FileLink, getAnalysisResultCount, HighlightedRegion } from './shared/analysis-result';
7+
import { AnalysisAlert, AnalysisRawResults, AnalysisResults, CodeSnippet, FileLink, getAnalysisResultCount, HighlightedRegion } from './shared/analysis-result';
58

69
// Each array item is a line of the markdown file.
710
export type MarkdownFile = string[];
@@ -34,7 +37,8 @@ export function generateMarkdown(query: RemoteQuery, analysesResults: AnalysisRe
3437
lines.push(...individualResult);
3538
}
3639
if (analysisResult.rawResults) {
37-
// TODO: Generate markdown table for raw results
40+
const rawResultTable = generateMarkdownForRawResults(analysisResult.rawResults);
41+
lines.push(...rawResultTable);
3842
}
3943
files.push(lines);
4044
}
@@ -198,6 +202,52 @@ function generateMarkdownForPathResults(
198202
return buildExpandableMarkdownSection('Show paths', pathLines);
199203
}
200204

205+
function generateMarkdownForRawResults(
206+
analysisRawResults: AnalysisRawResults
207+
): MarkdownFile {
208+
const tableRows: MarkdownFile = [];
209+
const columnCount = analysisRawResults.schema.columns.length;
210+
// Table headers are the column names if they exist, and empty otherwise
211+
const headers = analysisRawResults.schema.columns.map(
212+
(column) => column.name || ''
213+
);
214+
const tableHeader = `| ${headers.join(' | ')} |`;
215+
216+
tableRows.push(tableHeader);
217+
tableRows.push('|' + ' --- |'.repeat(columnCount));
218+
219+
for (const row of analysisRawResults.resultSet.rows) {
220+
const cells = row.map((cell) =>
221+
generateMarkdownForRawTableCell(cell, analysisRawResults.fileLinkPrefix)
222+
);
223+
tableRows.push(`| ${cells.join(' | ')} |`);
224+
}
225+
return tableRows;
226+
}
227+
228+
function generateMarkdownForRawTableCell(
229+
value: CellValue,
230+
fileLinkPrefix: string
231+
) {
232+
let cellValue: string;
233+
switch (typeof value) {
234+
case 'string':
235+
case 'number':
236+
case 'boolean':
237+
cellValue = `\`${convertNonPrintableChars(value.toString())}\``;
238+
break;
239+
case 'object':
240+
{
241+
const url = tryGetRemoteLocation(value.url, fileLinkPrefix);
242+
cellValue = `[\`${convertNonPrintableChars(value.label)}\`](${url})`;
243+
}
244+
break;
245+
}
246+
// `|` characters break the table, so we need to escape them
247+
return cellValue.replace('|', '\\|');
248+
}
249+
250+
201251
/**
202252
* Creates a markdown link to a remote file.
203253
* If the "link text" is not provided, we use the file path.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
### github/codeql
2+
3+
| c | |
4+
| --- | --- |
5+
| [`functio ... ght);\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/src/Expressions/examples/CompareIdenticalValues.js#L8-L13) | `6` |
6+
| [`functio ... i-1);\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/src/LanguageFeatures/examples/ArgumentsCallerCallee.js#L1-L5) | `5` |
7+
| [`functio ... i-1);\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/src/LanguageFeatures/examples/ArgumentsCallerCalleeGood.js#L1-L5) | `5` |
8+
| [`functio ... n -1;\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/src/Statements/examples/UselessComparisonTest.js#L1-L12) | `12` |
9+
| [`functio ... false\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/constants.js#L1-L8) | `8` |
10+
| [`functio ... \n }\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/loop.js#L1-L12) | `12` |
11+
| [`functio ... e\n }\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/loop.js#L14-L22) | `9` |
12+
| [`functio ... K\n }\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/loop.js#L24-L40) | `17` |
13+
| [`functio ... e\n }\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/plus.js#L1-L17) | `17` |
14+
| [`functio ... alse \n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/plus.js#L19-L28) | `10` |
15+
| [`functio ... true\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/plus.js#L30-L33) | `4` |
16+
| [`functio ... K\n }\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/tst.js#L1-L15) | `15` |
17+
| [`functio ... e\n }\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/tst.js#L17-L31) | `15` |
18+
| [`functio ... false\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/tst.js#L33-L41) | `9` |
19+
| [`functio ... e\n }\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/library-tests/RangeAnalysis/tst.js#L43-L52) | `10` |
20+
| [`functio ... ght);\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/query-tests/Expressions/CompareIdenticalValues/tst.js#L8-L13) | `6` |
21+
| [`functio ... i-1);\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/query-tests/LanguageFeatures/ArgumentsCallerCallee/tst.js#L1-L5) | `5` |
22+
| [`functio ... }\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/query-tests/Security/CWE-834/LoopBoundInjectionExitBad.js#L17-L29) | `13` |
23+
| [`functio ... true\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/query-tests/Statements/UselessComparisonTest/constant.js#L1-L4) | `4` |
24+
| [`functio ... n -1;\n}`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/query-tests/Statements/UselessComparisonTest/example.js#L1-L12) | `12` |
25+
| [`functio ... turn; }`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/query-tests/Statements/UselessComparisonTest/tst.js#L8-L8) | `1` |
26+
| [`functio ... i+1); }`](https://github.com/github/codeql/blob/cbdd4927cee593b715d8469240ce1d31edaaef9b/javascript/ql/test/query-tests/Statements/UselessComparisonTest/tst.js#L9-L9) | `1` |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
### meteor/meteor
2+
3+
| c | |
4+
| --- | --- |
5+
| [`functio ... rn H\|0}`](https://github.com/meteor/meteor/blob/53f3c4442d3542d3d2a012a854472a0d1bef9d12/packages/logic-solver/minisat.js#L7-L7) | `1` |
6+
| [`functio ... ext;\n\t}`](https://github.com/meteor/meteor/blob/53f3c4442d3542d3d2a012a854472a0d1bef9d12/packages/sha/sha256.js#L94-L124) | `31` |

0 commit comments

Comments
 (0)