From 83a5718eb67b7c391117996bdbfb707dc7655fc0 Mon Sep 17 00:00:00 2001 From: PierreDemailly <39910767+PierreDemailly@users.noreply.github.com> Date: Wed, 6 Dec 2023 13:50:21 +0100 Subject: [PATCH] feat: improve code fetcher (#320) --- .../package/pannels/warnings/code-fetcher.js | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/public/components/package/pannels/warnings/code-fetcher.js b/public/components/package/pannels/warnings/code-fetcher.js index 258d494b..c3b4ffa3 100644 --- a/public/components/package/pannels/warnings/code-fetcher.js +++ b/public/components/package/pannels/warnings/code-fetcher.js @@ -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 { @@ -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, `${value}`); - } + if (isMultiLine && lineIndex <= index && endLine >= startFrom + index) { + return `${line}`; + } + else if (!isMultiLine && value && line.includes(value)) { + const indexStart = line.indexOf(value); + // eslint-disable-next-line max-len + return `${line.slice(0, indexStart)}${line.slice(indexStart, indexStart + endColumn)}${line.slice(indexStart + endColumn)}`; + } + else if (!isMultiLine && startFrom + index === startLine) { return `${line}`; }