Skip to content

Commit e1a10fc

Browse files
committed
Markdown results: Highlight snippets with "<strong>"
1 parent a74dfea commit e1a10fc

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createRemoteFileRef } from '../pure/location-link-utils';
2+
import { parseHighlightedLine, shouldHighlightLine } from '../pure/sarif-utils';
23
import { RemoteQuery } from './remote-query';
3-
import { AnalysisAlert, AnalysisResults, FileLink } from './shared/analysis-result';
4+
import { AnalysisAlert, AnalysisResults, CodeSnippet, FileLink, HighlightedRegion } from './shared/analysis-result';
45

56
// Each array item is a line of the markdown file.
67
export type MarkdownFile = string[];
@@ -81,10 +82,11 @@ function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert,
8182
interpretedResult.highlightedRegion?.endLine
8283
));
8384
lines.push('');
84-
const codeSnippet = interpretedResult.codeSnippet?.text;
85+
const codeSnippet = interpretedResult.codeSnippet;
86+
const highlightedRegion = interpretedResult.highlightedRegion;
8587
if (codeSnippet) {
8688
lines.push(
87-
...generateMarkdownForCodeSnippet(codeSnippet, language),
89+
...generateMarkdownForCodeSnippet(codeSnippet, language, highlightedRegion),
8890
);
8991
}
9092
const alertMessage = buildMarkdownAlertMessage(interpretedResult);
@@ -99,17 +101,43 @@ function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert,
99101
return lines;
100102
}
101103

102-
function generateMarkdownForCodeSnippet(codeSnippet: string, language: string): MarkdownFile {
104+
function generateMarkdownForCodeSnippet(
105+
codeSnippet: CodeSnippet,
106+
language: string,
107+
highlightedRegion?: HighlightedRegion
108+
): MarkdownFile {
103109
const lines: MarkdownFile = [];
110+
const snippetStartLine = codeSnippet.startLine || 0;
111+
const codeLines = codeSnippet.text
112+
.split('\n')
113+
.map((line, index) =>
114+
highlightCodeLines(line, index + snippetStartLine, highlightedRegion)
115+
);
104116
lines.push(
105117
`<pre><code class="${language}">`,
106-
...codeSnippet.split('\n'),
118+
...codeLines,
107119
'</code></pre>',
108120
);
109121
lines.push('');
110122
return lines;
111123
}
112124

125+
function highlightCodeLines(
126+
line: string,
127+
lineNumber: number,
128+
highlightedRegion?: HighlightedRegion
129+
): string {
130+
if (!highlightedRegion || !shouldHighlightLine(lineNumber, highlightedRegion)) {
131+
return line;
132+
}
133+
const partiallyHighlightedLine = parseHighlightedLine(
134+
line,
135+
lineNumber,
136+
highlightedRegion
137+
);
138+
return `${partiallyHighlightedLine.plainSection1}<strong>${partiallyHighlightedLine.highlightedSection}</strong>${partiallyHighlightedLine.plainSection2}`;
139+
}
140+
113141
function buildMarkdownAlertMessage(interpretedResult: AnalysisAlert): string {
114142
let alertMessage = '';
115143
for (const token of interpretedResult.message.tokens) {

extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo1.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<pre><code class="javascript">
66
function cleanupTemp() {
77
let cmd = "rm -rf " + path.join(__dirname, "temp");
8-
cp.execSync(cmd); // BAD
8+
cp.execSync(<strong>cmd</strong>); // BAD
99
}
1010

1111
</code></pre>
@@ -19,7 +19,7 @@ function cleanupTemp() {
1919
<pre><code class="javascript">
2020
(function() {
2121
cp.execFileSync('rm', ['-rf', path.join(__dirname, "temp")]); // GOOD
22-
cp.execSync('rm -rf ' + path.join(__dirname, "temp")); // BAD
22+
cp.execSync(<strong>'rm -rf ' + path.join(__dirname, "temp")</strong>); // BAD
2323

2424
execa.shell('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
2525

@@ -34,7 +34,7 @@ function cleanupTemp() {
3434
<pre><code class="javascript">
3535
cp.execSync('rm -rf ' + path.join(__dirname, "temp")); // BAD
3636

37-
execa.shell('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
37+
execa.shell(<strong>'rm -rf ' + path.join(__dirname, "temp")</strong>); // NOT OK
3838
execa.shellSync('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
3939

4040

@@ -49,7 +49,7 @@ function cleanupTemp() {
4949
<pre><code class="javascript">
5050

5151
execa.shell('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
52-
execa.shellSync('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
52+
execa.shellSync(<strong>'rm -rf ' + path.join(__dirname, "temp")</strong>); // NOT OK
5353

5454
const safe = "\"" + path.join(__dirname, "temp") + "\"";
5555

extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<pre><code class="javascript">
66
if (isWindows()) {
77
//set for the current session and beyond
8-
child_process.execSync(`setx path "${meteorPath}/;%path%`);
8+
child_process.execSync(<strong>`setx path "${meteorPath}/;%path%`</strong>);
99
return;
1010
}
1111

0 commit comments

Comments
 (0)