Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown results: Highlight snippets with "<strong>" #1302

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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[];
Expand Down Expand Up @@ -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);
Expand All @@ -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(
`<pre><code class="${language}">`,
...codeSnippet.split('\n'),
...codeLines,
'</code></pre>',
);
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}<strong>${partiallyHighlightedLine.highlightedSection}</strong>${partiallyHighlightedLine.plainSection2}`;
}

function buildMarkdownAlertMessage(interpretedResult: AnalysisAlert): string {
let alertMessage = '';
for (const token of interpretedResult.message.tokens) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<pre><code class="javascript">
function cleanupTemp() {
let cmd = "rm -rf " + path.join(__dirname, "temp");
cp.execSync(cmd); // BAD
cp.execSync(<strong>cmd</strong>); // BAD
}

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

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

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

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


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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<pre><code class="javascript">
if (isWindows()) {
//set for the current session and beyond
child_process.execSync(`setx path "${meteorPath}/;%path%`);
child_process.execSync(<strong>`setx path "${meteorPath}/;%path%`</strong>);
return;
}

Expand Down