Skip to content

Commit

Permalink
Merge pull request #14 from yamadashy/fix/python-comment-remove
Browse files Browse the repository at this point in the history
Fix rtrimLines function and improve comment removal
  • Loading branch information
yamadashy authored Jul 27, 2024
2 parents 3a15b21 + 5248a9f commit 37efdfd
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 23 deletions.
5 changes: 4 additions & 1 deletion src/utils/fileManipulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface FileManipulator {
}

function rtrimLines(content: string): string {
return content.replace(/\s+$/gm, '');
return content.replace(/[ \t]+$/gm, '');
}

class StripCommentsManipulator implements FileManipulator {
Expand All @@ -31,6 +31,9 @@ class PythonManipulator implements FileManipulator {
result = result.replace(/'''[\s\S]*?'''/g, '');
result = result.replace(/"""[\s\S]*?"""/g, '');

// Then, remove inline comments
result = result.replace(/(?<!\\)#.*$/gm, '');

return rtrimLines(result);
}
}
Expand Down
127 changes: 105 additions & 22 deletions tests/utils/fileManipulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
int main() {
return 0;
}`,
}
`,
},
{
name: 'C# comment removal',
Expand All @@ -31,9 +35,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
public class Test {
public void Method() {}
}`,
}
`,
},
{
name: 'CSS comment removal',
Expand All @@ -45,9 +53,11 @@ describe('fileManipulator', () => {
}
`,
expected: `
body {
color: red;
}`,
}
`,
},
{
name: 'HTML comment removal',
Expand All @@ -67,9 +77,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
public class Test {
public void method() {}
}`,
}
`,
},
{
name: 'JavaScript comment removal',
Expand All @@ -83,9 +97,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
function test() {
return true;
}`,
}
`,
},
{
name: 'Less comment removal',
Expand All @@ -98,8 +116,12 @@ describe('fileManipulator', () => {
body { color: @variable; }
`,
expected: `
@variable: #888;
body { color: @variable; }`,
body { color: @variable; }
`,
},
{
name: 'PHP comment removal',
Expand All @@ -117,10 +139,15 @@ describe('fileManipulator', () => {
`,
expected: `
<?php
function test() {
return true;
}
?>`,
?>
`,
},
{
name: 'Python comment removal',
Expand All @@ -137,8 +164,14 @@ describe('fileManipulator', () => {
"""
`,
expected: `
def test():
return True`,
return True
`,
},
{
name: 'Ruby comment removal',
Expand All @@ -153,9 +186,14 @@ describe('fileManipulator', () => {
end
`,
expected: `
def test
true
end`,
end
`,
},
{
name: 'Sass comment removal',
Expand All @@ -169,9 +207,13 @@ describe('fileManipulator', () => {
color: $variable
`,
expected: `
$variable: #888
body
color: $variable`,
color: $variable
`,
},
{
name: 'SCSS comment removal',
Expand All @@ -184,8 +226,12 @@ describe('fileManipulator', () => {
body { color: $variable; }
`,
expected: `
$variable: #888;
body { color: $variable; }`,
body { color: $variable; }
`,
},
{
name: 'SQL comment removal',
Expand All @@ -195,7 +241,9 @@ describe('fileManipulator', () => {
SELECT * FROM table WHERE id = 1;
`,
expected: `
SELECT * FROM table WHERE id = 1;`,
SELECT * FROM table WHERE id = 1;
`,
},
{
name: 'Swift comment removal',
Expand All @@ -209,9 +257,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
func test() {
return true
}`,
}
`,
},
{
name: 'TypeScript comment removal',
Expand All @@ -225,9 +277,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
function test(): boolean {
return true;
}`,
}
`,
},
{
name: 'XML comment removal',
Expand All @@ -247,9 +303,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
void main() {
print('Hello');
}`,
}
`,
},
{
name: 'Go comment removal',
Expand All @@ -263,9 +323,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
func main() {
fmt.Println("Hello")
}`,
}
`,
},
{
name: 'Kotlin comment removal',
Expand All @@ -279,9 +343,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
fun main() {
println("Hello")
}`,
}
`,
},
{
name: 'Rust comment removal',
Expand All @@ -295,9 +363,13 @@ describe('fileManipulator', () => {
}
`,
expected: `
fn main() {
println!("Hello");
}`,
}
`,
},
{
name: 'Shell script comment removal',
Expand All @@ -307,7 +379,9 @@ describe('fileManipulator', () => {
echo "Hello"
`,
expected: `
echo "Hello"`,
echo "Hello"
`,
},
{
name: 'YAML comment removal',
Expand All @@ -318,7 +392,8 @@ describe('fileManipulator', () => {
`,
expected: `
key: value
another_key: another_value`,
another_key: another_value
`,
},
{
name: 'Vue file comment removal',
Expand All @@ -345,9 +420,11 @@ describe('fileManipulator', () => {
`,
expected: `
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
data() {
return {
Expand All @@ -357,8 +434,10 @@ describe('fileManipulator', () => {
}
</script>
<style>
.test { color: red; }
</style>`,
</style>
`,
},
{
name: 'Svelte file comment removal',
Expand All @@ -376,13 +455,17 @@ describe('fileManipulator', () => {
</style>
`,
expected: `
<div>{message}</div>
<script>
let message = 'Hello';
</script>
<style>
div { color: red; }
</style>`,
</style>
`,
},
];

Expand Down

0 comments on commit 37efdfd

Please sign in to comment.