-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored RemoveEmptyStringsConcatenation
Added separated tests
- Loading branch information
1 parent
d394942
commit c60b9b1
Showing
4 changed files
with
172 additions
and
41 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/codemodder/codemods/transformations/remove_empty_string_concatenation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import libcst as cst | ||
from libcst import CSTTransformer | ||
|
||
|
||
class RemoveEmptyStringConcatenation(CSTTransformer): | ||
""" | ||
Removes concatenation with empty strings (e.g. "hello " + "") or "hello" "" | ||
""" | ||
|
||
def leave_BinaryOperation( | ||
self, original_node: cst.BinaryOperation, updated_node: cst.BinaryOperation | ||
) -> cst.BaseExpression: | ||
return self.handle_node(updated_node) | ||
|
||
def leave_ConcatenatedString( | ||
self, | ||
original_node: cst.ConcatenatedString, | ||
updated_node: cst.ConcatenatedString, | ||
) -> cst.BaseExpression: | ||
return self.handle_node(updated_node) | ||
|
||
def handle_node( | ||
self, updated_node: cst.BinaryOperation | cst.ConcatenatedString | ||
) -> cst.BaseExpression: | ||
match updated_node.left: | ||
# TODO f-string cases | ||
case cst.SimpleString() if updated_node.left.raw_value == "": | ||
match updated_node.right: | ||
case cst.SimpleString() if updated_node.right.raw_value == "": | ||
return cst.SimpleString(value='""') | ||
case _: | ||
return updated_node.right | ||
match updated_node.right: | ||
case cst.SimpleString() if updated_node.right.raw_value == "": | ||
match updated_node.left: | ||
case cst.SimpleString() if updated_node.left.raw_value == "": | ||
return cst.SimpleString(value='""') | ||
case _: | ||
return updated_node.left | ||
return updated_node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
tests/transformations/test_remove_empty_string_concatenation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import libcst as cst | ||
from libcst.codemod import Codemod, CodemodTest | ||
|
||
from codemodder.codemods.transformations.remove_empty_string_concatenation import ( | ||
RemoveEmptyStringConcatenation, | ||
) | ||
|
||
|
||
class RemoveEmptyStringConcatenationCodemod(Codemod): | ||
def transform_module_impl(self, tree: cst.Module) -> cst.Module: | ||
return tree.visit(RemoveEmptyStringConcatenation()) | ||
|
||
|
||
class TestRemoveEmptyStringConcatenation(CodemodTest): | ||
TRANSFORM = RemoveEmptyStringConcatenationCodemod | ||
|
||
def test_left(self): | ||
before = """ | ||
"" + "world" | ||
""" | ||
|
||
after = """ | ||
"world" | ||
""" | ||
|
||
self.assertCodemod(before, after) | ||
|
||
def test_right(self): | ||
before = """ | ||
"hello" + "" | ||
""" | ||
|
||
after = """ | ||
"hello" | ||
""" | ||
|
||
self.assertCodemod(before, after) | ||
|
||
def test_both(self): | ||
before = """ | ||
"" + "" | ||
""" | ||
|
||
after = """ | ||
"" | ||
""" | ||
|
||
self.assertCodemod(before, after) | ||
|
||
def test_concatenated_string_right(self): | ||
before = """ | ||
"hello" "" | ||
""" | ||
|
||
after = """ | ||
"hello" | ||
""" | ||
|
||
self.assertCodemod(before, after) | ||
|
||
def test_concatenated_string_left(self): | ||
before = """ | ||
"world" | ||
""" | ||
|
||
after = """ | ||
"world" | ||
""" | ||
|
||
self.assertCodemod(before, after) | ||
|
||
def test_multiple_mixed(self): | ||
before = ( | ||
""" | ||
"" + '' """ | ||
""" + r'''''' | ||
""" | ||
) | ||
|
||
after = '""' | ||
|
||
self.assertCodemod(before, after) |