diff --git a/extensions/ql-vscode/src/remote-queries/remote-queries-markdown-generation.ts b/extensions/ql-vscode/src/remote-queries/remote-queries-markdown-generation.ts index 4f432c7cfec..0edd951101f 100644 --- a/extensions/ql-vscode/src/remote-queries/remote-queries-markdown-generation.ts +++ b/extensions/ql-vscode/src/remote-queries/remote-queries-markdown-generation.ts @@ -1,6 +1,7 @@ import { createRemoteFileRef } from '../pure/location-link-utils'; +import { parseHighlightedLine, shouldHighlightLine } from '../pure/sarif-utils'; import { RemoteQuery } from './remote-query'; -import { AnalysisAlert, AnalysisResults, FileLink } from './shared/analysis-result'; +import { AnalysisAlert, AnalysisResults, CodeSnippet, FileLink, HighlightedRegion } from './shared/analysis-result'; // Each array item is a line of the markdown file. export type MarkdownFile = string[]; @@ -81,10 +82,11 @@ function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert, interpretedResult.highlightedRegion?.endLine )); lines.push(''); - const codeSnippet = interpretedResult.codeSnippet?.text; + const codeSnippet = interpretedResult.codeSnippet; + const highlightedRegion = interpretedResult.highlightedRegion; if (codeSnippet) { lines.push( - ...generateMarkdownForCodeSnippet(codeSnippet, language), + ...generateMarkdownForCodeSnippet(codeSnippet, language, highlightedRegion), ); } const alertMessage = buildMarkdownAlertMessage(interpretedResult); @@ -99,17 +101,43 @@ function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert, return lines; } -function generateMarkdownForCodeSnippet(codeSnippet: string, language: string): MarkdownFile { +function generateMarkdownForCodeSnippet( + codeSnippet: CodeSnippet, + language: string, + highlightedRegion?: HighlightedRegion +): MarkdownFile { const lines: MarkdownFile = []; + const snippetStartLine = codeSnippet.startLine || 0; + const codeLines = codeSnippet.text + .split('\n') + .map((line, index) => + highlightCodeLines(line, index + snippetStartLine, highlightedRegion) + ); lines.push( `
`,
-    ...codeSnippet.split('\n'),
+    ...codeLines,
     '
', ); lines.push(''); return lines; } +function highlightCodeLines( + line: string, + lineNumber: number, + highlightedRegion?: HighlightedRegion +): string { + if (!highlightedRegion || !shouldHighlightLine(lineNumber, highlightedRegion)) { + return line; + } + const partiallyHighlightedLine = parseHighlightedLine( + line, + lineNumber, + highlightedRegion + ); + return `${partiallyHighlightedLine.plainSection1}${partiallyHighlightedLine.highlightedSection}${partiallyHighlightedLine.plainSection2}`; +} + function buildMarkdownAlertMessage(interpretedResult: AnalysisAlert): string { let alertMessage = ''; for (const token of interpretedResult.message.tokens) { diff --git a/extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo1.md b/extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo1.md index 651582ef7dd..10ced141122 100644 --- a/extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo1.md +++ b/extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo1.md @@ -5,7 +5,7 @@

 function cleanupTemp() {
   let cmd = "rm -rf " + path.join(__dirname, "temp");
-  cp.execSync(cmd); // BAD
+  cp.execSync(cmd); // BAD
 }
 
 
@@ -19,7 +19,7 @@ function cleanupTemp() {

 (function() {
 	cp.execFileSync('rm',  ['-rf', path.join(__dirname, "temp")]); // GOOD
-	cp.execSync('rm -rf ' + path.join(__dirname, "temp")); // BAD
+	cp.execSync('rm -rf ' + path.join(__dirname, "temp")); // BAD
 
 	execa.shell('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
 
@@ -34,7 +34,7 @@ function cleanupTemp() {
 

 	cp.execSync('rm -rf ' + path.join(__dirname, "temp")); // BAD
 
-	execa.shell('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
+	execa.shell('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
 	execa.shellSync('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
 
 
@@ -49,7 +49,7 @@ function cleanupTemp() {
 

 
 	execa.shell('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
-	execa.shellSync('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
+	execa.shellSync('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
 
 	const safe = "\"" + path.join(__dirname, "temp") + "\"";
 
diff --git a/extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo2.md b/extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo2.md
index b1882308893..dd419d6485e 100644
--- a/extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo2.md
+++ b/extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo2.md
@@ -5,7 +5,7 @@
 

   if (isWindows()) {
     //set for the current session and beyond
-    child_process.execSync(`setx path "${meteorPath}/;%path%`);
+    child_process.execSync(`setx path "${meteorPath}/;%path%`);
     return;
   }