Skip to content

Commit

Permalink
Merge pull request #59 from yamadashy/fix/remove-comment-python-f-string
Browse files Browse the repository at this point in the history
Fix Python comment removal to preserve f-strings
  • Loading branch information
yamadashy authored Aug 24, 2024
2 parents d2fef62 + edba6ed commit 1e04161
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ class StripCommentsManipulator extends BaseManipulator {

class PythonManipulator extends BaseManipulator {
removeComments(content: string): string {
// First, use strip-comments to remove standard comments
let result = strip(content, { language: 'python', preserveNewlines: true });

// Then, remove triple-quote comments
result = result.replace(/'''[\s\S]*?'''/g, '');
result = result.replace(/"""[\s\S]*?"""/g, '');

// Then, remove inline comments
result = result.replace(/(?<!\\)#.*$/gm, '');
// Remove single-line comments
const result = content.replace(/(?<!\\)#.*$/gm, '');

return rtrimLines(result);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/file/fileProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pMap from 'p-map';
import { RepopackConfigMerged } from '../../config/configTypes.js';
import { getProcessConcurrency } from '../../shared/processConcurrency.js';
import { getFileManipulator } from './fileManipulater.js';
import { getFileManipulator } from './fileManipulator.js';
import { ProcessedFile, RawFile } from './fileTypes.js';

export const processFiles = async (rawFiles: RawFile[], config: RepopackConfigMerged): Promise<ProcessedFile[]> => {
Expand Down
16 changes: 9 additions & 7 deletions tests/core/file/fileManipulator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test, describe } from 'vitest';
import { getFileManipulator } from '../../../src/core/file/fileManipulater.js';
import { getFileManipulator } from '../../../src/core/file/fileManipulator.js';

describe('fileManipulator', () => {
const testCases = [
Expand Down Expand Up @@ -156,21 +156,23 @@ describe('fileManipulator', () => {
# Single line comment
def test():
'''
Multi-line comment
docstring
'''
return True
"""
Another multi-line comment
Another docstring
"""
`,
expected: `
def test():
'''
docstring
'''
return True
"""
Another docstring
"""
`,
},
{
Expand Down
4 changes: 2 additions & 2 deletions tests/core/file/fileProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect, describe, it, vi } from 'vitest';
import { processFiles, processContent } from '../../../src/core/file/fileProcessor.js';
import { getFileManipulator } from '../../../src/core/file/fileManipulater.js';
import { getFileManipulator } from '../../../src/core/file/fileManipulator.js';
import { RawFile } from '../../../src/core/file/fileTypes.js';
import { createMockConfig } from '../../testing/testUtils.js';

vi.mock('../../../src/core/file/fileManipulater');
vi.mock('../../../src/core/file/fileManipulator');

describe('fileProcessor', () => {
describe('processFiles', () => {
Expand Down

0 comments on commit 1e04161

Please sign in to comment.