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

feat: improve code fetcher #320

Merged
merged 1 commit into from
Dec 6, 2023
Merged
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
29 changes: 14 additions & 15 deletions public/components/package/pannels/warnings/code-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ require("highlightjs-line-numbers.js/dist/highlightjs-line-numbers.min.js");
const kLoadingMessage = "Loading ...";

function removeTags(str) {
if ((str === null) || (str === "")) {
if (str === null || str === "") {
return false;
}
// eslint-disable-next-line no-param-reassign
str = str.toString();

return str.replace(/<\/?[^>]+(>|$)/ig, "");
return str.toString().replace(/(<([^>]+)>)/ig, "");
}

export class CodeFetcher {
Expand Down Expand Up @@ -96,20 +94,21 @@ export class CodeFetcher {
if (withoutTags === false) {
return line;
}
const incriminedCodeSingleLine = code.split("\n").slice(location[0][0] - 1, location[0][0]);
const isMultiLine = location[0][0] < location[1][0];
const [[startLine]] = location;
const [[startLine], [endLine, endColumn]] = location;
const isMultiLine = startLine < endLine;
const lineIndex = startLine >= 10 ? 9 : startLine - 1;
const isRelevantLine = isMultiLine ?
lineIndex <= index && index <= location[1][0] - 1 :
// eslint-disable-next-line max-len
incriminedCodeSingleLine.includes(value) || (!isMultiLine && lineIndex === index && withoutTags.includes(value)) || (!value && lineIndex === index);
const startFrom = startLine >= 10 ? startLine - 9 : 1;

if (isRelevantLine) {
if (!isMultiLine && value && line.includes(value)) {
return line.replace(value, `<span class="relevant-line">${value}</span>`);
}
if (isMultiLine && lineIndex <= index && endLine >= startFrom + index) {
return `<span class="relevant-line">${line}</span>`;
}
else if (!isMultiLine && value && line.includes(value)) {
const indexStart = line.indexOf(value);

// eslint-disable-next-line max-len
return `${line.slice(0, indexStart)}<span class="relevant-line">${line.slice(indexStart, indexStart + endColumn)}</span>${line.slice(indexStart + endColumn)}`;
}
else if (!isMultiLine && startFrom + index === startLine) {
return `<span class="relevant-line">${line}</span>`;
}

Expand Down