|
| 1 | +import path from 'path'; |
| 2 | +import strip from 'strip-comments'; |
| 3 | + |
| 4 | +interface FileManipulator { |
| 5 | + removeComments(content: string): string; |
| 6 | +} |
| 7 | + |
| 8 | +function rtrimLines(content: string): string { |
| 9 | + return content.replace(/\s+$/gm, ''); |
| 10 | +} |
| 11 | + |
| 12 | +class StripCommentsManipulator implements FileManipulator { |
| 13 | + private language: string; |
| 14 | + |
| 15 | + constructor(language: string) { |
| 16 | + this.language = language; |
| 17 | + } |
| 18 | + |
| 19 | + removeComments(content: string): string { |
| 20 | + let result = strip(content, { language: this.language, preserveNewlines: true }); |
| 21 | + return rtrimLines(result); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class PythonManipulator implements FileManipulator { |
| 26 | + removeComments(content: string): string { |
| 27 | + // First, use strip-comments to remove standard comments |
| 28 | + let result = strip(content, { language: 'python', preserveNewlines: true }); |
| 29 | + |
| 30 | + // Then, remove triple-quote comments |
| 31 | + result = result.replace(/'''[\s\S]*?'''/g, ''); |
| 32 | + result = result.replace(/"""[\s\S]*?"""/g, ''); |
| 33 | + |
| 34 | + return rtrimLines(result); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class CompositeManipulator implements FileManipulator { |
| 39 | + private manipulators: FileManipulator[]; |
| 40 | + |
| 41 | + constructor(...manipulators: FileManipulator[]) { |
| 42 | + this.manipulators = manipulators; |
| 43 | + } |
| 44 | + |
| 45 | + removeComments(content: string): string { |
| 46 | + return this.manipulators.reduce((acc, manipulator) => manipulator.removeComments(acc), content); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const manipulators: Record<string, FileManipulator> = { |
| 51 | + '.c': new StripCommentsManipulator('c'), |
| 52 | + '.cs': new StripCommentsManipulator('csharp'), |
| 53 | + '.css': new StripCommentsManipulator('css'), |
| 54 | + '.dart': new StripCommentsManipulator('c'), |
| 55 | + '.go': new StripCommentsManipulator('c'), |
| 56 | + '.html': new StripCommentsManipulator('html'), |
| 57 | + '.java': new StripCommentsManipulator('java'), |
| 58 | + '.js': new StripCommentsManipulator('javascript'), |
| 59 | + '.jsx': new StripCommentsManipulator('javascript'), |
| 60 | + '.kt': new StripCommentsManipulator('c'), |
| 61 | + '.less': new StripCommentsManipulator('less'), |
| 62 | + '.php': new StripCommentsManipulator('php'), |
| 63 | + '.rb': new StripCommentsManipulator('ruby'), |
| 64 | + '.rs': new StripCommentsManipulator('c'), |
| 65 | + '.sass': new StripCommentsManipulator('sass'), |
| 66 | + '.scss': new StripCommentsManipulator('sass'), |
| 67 | + '.sh': new StripCommentsManipulator('perl'), |
| 68 | + '.sql': new StripCommentsManipulator('sql'), |
| 69 | + '.swift': new StripCommentsManipulator('swift'), |
| 70 | + '.ts': new StripCommentsManipulator('javascript'), |
| 71 | + '.tsx': new StripCommentsManipulator('javascript'), |
| 72 | + '.xml': new StripCommentsManipulator('xml'), |
| 73 | + '.yaml': new StripCommentsManipulator('perl'), |
| 74 | + '.yml': new StripCommentsManipulator('perl'), |
| 75 | + |
| 76 | + '.py': new PythonManipulator(), |
| 77 | + |
| 78 | + '.vue': new CompositeManipulator( |
| 79 | + new StripCommentsManipulator('html'), |
| 80 | + new StripCommentsManipulator('css'), |
| 81 | + new StripCommentsManipulator('javascript'), |
| 82 | + ), |
| 83 | + '.svelte': new CompositeManipulator( |
| 84 | + new StripCommentsManipulator('html'), |
| 85 | + new StripCommentsManipulator('css'), |
| 86 | + new StripCommentsManipulator('javascript'), |
| 87 | + ), |
| 88 | +}; |
| 89 | + |
| 90 | +export function getFileManipulator(filePath: string): FileManipulator | null { |
| 91 | + const ext = path.extname(filePath); |
| 92 | + return manipulators[ext] || null; |
| 93 | +} |
0 commit comments