From bc11c8ef61a21e26632d604be5292310fdbb11ae Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Thu, 15 Feb 2024 09:29:17 -0300 Subject: [PATCH] document new codemod str concat in seq literals --- .../test_str_concat_in_seq_literals.py | 37 +++++++++++++++++++ src/codemodder/scripts/generate_docs.py | 4 ++ src/core_codemods/__init__.py | 2 + ..._python_str-concat-in-sequence-literals.md | 14 +++++++ .../str_concat_in_seq_literal.py | 14 ++----- .../str_concat_in_sequence_literals.py | 7 ++++ 6 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 integration_tests/test_str_concat_in_seq_literals.py create mode 100644 src/core_codemods/docs/pixee_python_str-concat-in-sequence-literals.md create mode 100644 tests/samples/str_concat_in_sequence_literals.py diff --git a/integration_tests/test_str_concat_in_seq_literals.py b/integration_tests/test_str_concat_in_seq_literals.py new file mode 100644 index 00000000..bc2f2233 --- /dev/null +++ b/integration_tests/test_str_concat_in_seq_literals.py @@ -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 diff --git a/src/codemodder/scripts/generate_docs.py b/src/codemodder/scripts/generate_docs.py index 20e3e9c8..e2256edd 100644 --- a/src/codemodder/scripts/generate_docs.py +++ b/src/codemodder/scripts/generate_docs.py @@ -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 | { diff --git a/src/core_codemods/__init__.py b/src/core_codemods/__init__.py index 2da21231..09f56835 100644 --- a/src/core_codemods/__init__.py +++ b/src/core_codemods/__init__.py @@ -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", @@ -116,6 +117,7 @@ RemoveAssertionInPytestRaises, FixAssertTuple, LazyLogging, + StrConcatInSeqLiteral, ], ) diff --git a/src/core_codemods/docs/pixee_python_str-concat-in-sequence-literals.md b/src/core_codemods/docs/pixee_python_str-concat-in-sequence-literals.md new file mode 100644 index 00000000..ddc6cf9b --- /dev/null +++ b/src/core_codemods/docs/pixee_python_str-concat-in-sequence-literals.md @@ -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", +] +``` diff --git a/src/core_codemods/str_concat_in_seq_literal.py b/src/core_codemods/str_concat_in_seq_literal.py index 36fa117b..36b859fc 100644 --- a/src/core_codemods/str_concat_in_seq_literal.py +++ b/src/core_codemods/str_concat_in_seq_literal.py @@ -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) diff --git a/tests/samples/str_concat_in_sequence_literals.py b/tests/samples/str_concat_in_sequence_literals.py new file mode 100644 index 00000000..d53a6236 --- /dev/null +++ b/tests/samples/str_concat_in_sequence_literals.py @@ -0,0 +1,7 @@ +bad = [ + "ab" + "cd", + "ef", + "gh" + "ij", +]