Skip to content

Commit

Permalink
document new codemod str concat in seq literals
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Feb 15, 2024
1 parent 1964726 commit bc11c8e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 10 deletions.
37 changes: 37 additions & 0 deletions integration_tests/test_str_concat_in_seq_literals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from core_codemods.str_concat_in_seq_literal import StrConcatInSeqLiteral
from integration_tests.base_test import (
BaseIntegrationTest,
original_and_expected_from_code_path,
)


class TestStrConcatInSeqLiteral(BaseIntegrationTest):
codemod = StrConcatInSeqLiteral
code_path = "tests/samples/str_concat_in_sequence_literals.py"
original_code, expected_new_code = original_and_expected_from_code_path(
code_path,
[
(1, """ "ab",\n"""),
(4, """ "gh",\n"""),
],
)

# fmt: off
expected_diff =(
"""--- \n"""
"""+++ \n"""
"""@@ -1,7 +1,7 @@\n"""
""" bad = [\n"""
"""- "ab"\n"""
"""+ "ab",\n"""
""" "cd",\n"""
""" "ef",\n"""
"""- "gh"\n"""
"""+ "gh",\n"""
""" "ij",\n"""
""" ]\n""")
# fmt: on

expected_line_change = "1"
change_description = StrConcatInSeqLiteral.change_description
num_changes = 2
4 changes: 4 additions & 0 deletions src/codemodder/scripts/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ class DocMetadata:
importance="Medium",
guidance_explained="We believe this change is safe and will not cause any issues.",
),
"str-concat-in-sequence-literals": DocMetadata(
importance="Medium",
guidance_explained="While string concatenation inside a sequence iterable is likely a mistake, there are instances when you may choose to use them..",
),
}

METADATA = CORE_METADATA | {
Expand Down
2 changes: 2 additions & 0 deletions src/core_codemods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from .sonar.sonar_flask_json_response_type import SonarFlaskJsonResponseType
from .sonar.sonar_django_json_response_type import SonarDjangoJsonResponseType
from .lazy_logging import LazyLogging
from .str_concat_in_seq_literal import StrConcatInSeqLiteral

registry = CodemodCollection(
origin="pixee",
Expand Down Expand Up @@ -116,6 +117,7 @@
RemoveAssertionInPytestRaises,
FixAssertTuple,
LazyLogging,
StrConcatInSeqLiteral,
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
This codemod fixes cases of implicit string concatenation inside lists, sets, or tuples. This is most likely a mistake: you probably meant include a comma in between the concatenated strings.

Our changes look something like this:
```diff
bad = [
- "ab"
+ "ab",
"cd",
"ef",
- "gh"
+ "gh",
"ij",
]
```
14 changes: 4 additions & 10 deletions src/core_codemods/str_concat_in_seq_literal.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import libcst as cst
from core_codemods.api import Metadata, ReviewGuidance, SimpleCodemod, Reference
from core_codemods.api import Metadata, ReviewGuidance, SimpleCodemod
from codemodder.codemods.utils_mixin import NameResolutionMixin, AncestorPatternsMixin


class StrConcatInSeqLiteral(SimpleCodemod, NameResolutionMixin, AncestorPatternsMixin):
metadata = Metadata(
name="str-concat-in-sequence-literals",
summary="TODOReplace Comparisons to Empty Sequence with Implicit Boolean Comparison",
summary="Convert Implicit String Concat Inside Sequence into Individual Elements",
review_guidance=ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW,
references=[
Reference(
url="TODOhttps://docs.python.org/3/library/stdtypes.html#truth-value-testing"
),
],
)
change_description = (
"todoReplace comparisons to empty sequence with implicit boolean comparison."
references=[],
)
change_description = "Convert implicit string concat into individual elements."

def leave_List(self, original_node: cst.List, updated_node: cst.List) -> cst.List:
return self.process_node_elements(original_node, updated_node)
Expand Down
7 changes: 7 additions & 0 deletions tests/samples/str_concat_in_sequence_literals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bad = [
"ab"
"cd",
"ef",
"gh"
"ij",
]

0 comments on commit bc11c8e

Please sign in to comment.