From 6c6943df47dbea5aefbc39f8470b0cea82d98c37 Mon Sep 17 00:00:00 2001 From: Mayur Kawale <122032765+Mefisto04@users.noreply.github.com> Date: Sun, 13 Oct 2024 12:43:48 +0530 Subject: [PATCH] Update fileManipulator.ts --- src/core/file/fileManipulator.ts | 215 ++++++++++--------------------- 1 file changed, 69 insertions(+), 146 deletions(-) diff --git a/src/core/file/fileManipulator.ts b/src/core/file/fileManipulator.ts index dcc76997d..ce38ce338 100644 --- a/src/core/file/fileManipulator.ts +++ b/src/core/file/fileManipulator.ts @@ -6,7 +6,11 @@ interface FileManipulator { removeEmptyLines(content: string): string; } -const rtrimLines = (content: string): string => content.replace(/[ \t]+$/gm, ''); +const rtrimLines = (content: string): string => + content + .split('\n') + .map((line) => line.trimEnd()) + .join('\n'); class BaseManipulator implements FileManipulator { removeComments(content: string): string { @@ -41,115 +45,28 @@ class StripCommentsManipulator extends BaseManipulator { class PythonManipulator extends BaseManipulator { removeDocStrings(content: string): string { if (!content) return ''; - const lines = content.split('\n'); - - let result = ''; - - let buffer = ''; - let quoteType: '' | "'" | '"' = ''; - let tripleQuotes = 0; - - const doubleQuoteRegex = /^\s*(? { - let ans = -1; - let start = 0; - let end = pairs.length - 1; - while (start <= end) { - const mid = Math.floor((start + end) / 2); - const [pairStart, pairEnd] = pairs[mid]; - if (hashIndex > pairStart && hashIndex < pairEnd) { - ans = mid; - break; - } - if (hashIndex < pairStart) { - end = mid - 1; - } else { - start = mid + 1; - } - } - return ans !== -1; - }; - let result = ''; - const pairs: [number, number][] = []; - let prevQuote = 0; - while (prevQuote < content.length) { - const openingQuote: number = content.slice(prevQuote + 1).search(/(? { + const hashIndex = line.indexOf('#'); + if (hashIndex !== -1 && !this.isInsideQuotes(line, hashIndex)) { + return line.slice(0, hashIndex).trimEnd(); } - result += `${content.slice(prevHash, nextNewLine)}\n`; - } - prevHash = nextNewLine + 1; - } - return result; + return line; + }) + .join('\n'); + return rtrimLines(result); + } + + private isInsideQuotes(line: string, index: number): boolean { + const singleQuotePairs = (line.match(/(? manipulator.removeComments(acc), content); + return this.manipulators.reduce( + (acc, manipulator) => manipulator.removeComments(acc), + content + ); } } -const manipulators: Record = { - '.c': new StripCommentsManipulator('c'), - '.cs': new StripCommentsManipulator('csharp'), - '.css': new StripCommentsManipulator('css'), - '.dart': new StripCommentsManipulator('c'), - '.go': new StripCommentsManipulator('c'), - '.html': new StripCommentsManipulator('html'), - '.java': new StripCommentsManipulator('java'), - '.js': new StripCommentsManipulator('javascript'), - '.jsx': new StripCommentsManipulator('javascript'), - '.kt': new StripCommentsManipulator('c'), - '.less': new StripCommentsManipulator('less'), - '.php': new StripCommentsManipulator('php'), - '.rb': new StripCommentsManipulator('ruby'), - '.rs': new StripCommentsManipulator('c'), - '.sass': new StripCommentsManipulator('sass'), - '.scss': new StripCommentsManipulator('sass'), - '.sh': new StripCommentsManipulator('perl'), - '.sql': new StripCommentsManipulator('sql'), - '.swift': new StripCommentsManipulator('swift'), - '.ts': new StripCommentsManipulator('javascript'), - '.tsx': new StripCommentsManipulator('javascript'), - '.xml': new StripCommentsManipulator('xml'), - '.yaml': new StripCommentsManipulator('perl'), - '.yml': new StripCommentsManipulator('perl'), - - '.py': new PythonManipulator(), - - '.vue': new CompositeManipulator( - new StripCommentsManipulator('html'), - new StripCommentsManipulator('css'), - new StripCommentsManipulator('javascript'), - ), - '.svelte': new CompositeManipulator( - new StripCommentsManipulator('html'), - new StripCommentsManipulator('css'), - new StripCommentsManipulator('javascript'), - ), +const manipulators: Record FileManipulator> = { + '.c': () => new StripCommentsManipulator('c'), + '.cs': () => new StripCommentsManipulator('csharp'), + '.css': () => new StripCommentsManipulator('css'), + '.dart': () => new StripCommentsManipulator('c'), + '.go': () => new StripCommentsManipulator('c'), + '.html': () => new StripCommentsManipulator('html'), + '.java': () => new StripCommentsManipulator('java'), + '.js': () => new StripCommentsManipulator('javascript'), + '.jsx': () => new StripCommentsManipulator('javascript'), + '.kt': () => new StripCommentsManipulator('c'), + '.less': () => new StripCommentsManipulator('less'), + '.php': () => new StripCommentsManipulator('php'), + '.rb': () => new StripCommentsManipulator('ruby'), + '.rs': () => new StripCommentsManipulator('c'), + '.sass': () => new StripCommentsManipulator('sass'), + '.scss': () => new StripCommentsManipulator('sass'), + '.sh': () => new StripCommentsManipulator('perl'), + '.sql': () => new StripCommentsManipulator('sql'), + '.swift': () => new StripCommentsManipulator('swift'), + '.ts': () => new StripCommentsManipulator('javascript'), + '.tsx': () => new StripCommentsManipulator('javascript'), + '.xml': () => new StripCommentsManipulator('xml'), + '.yaml': () => new StripCommentsManipulator('perl'), + '.yml': () => new StripCommentsManipulator('perl'), + + '.py': () => new PythonManipulator(), + + '.vue': () => + new CompositeManipulator( + new StripCommentsManipulator('html'), + new StripCommentsManipulator('css'), + new StripCommentsManipulator('javascript') + ), + '.svelte': () => + new CompositeManipulator( + new StripCommentsManipulator('html'), + new StripCommentsManipulator('css'), + new StripCommentsManipulator('javascript') + ), }; export const getFileManipulator = (filePath: string): FileManipulator | null => { const ext = path.extname(filePath); - return manipulators[ext] || null; + const manipulatorFactory = manipulators[ext]; + return manipulatorFactory ? manipulatorFactory() : null; };