def _contains_commented_out_code(self) -> bool:
        """
        Check if the current physical line contains commented out code.

        This test relies on eradicate function to remove commented out code
        from a physical line.

        Physical lines might appear like commented code although they are part
        of a multi-line docstring (e.g. a `# noqa: DAR201` comment to suppress
        flake8 warning about missing returns in the docstring).
        To prevent this false-positive, the tokens of the physical line are
        checked for a comment. The eradicate function is only invokes,
        when the tokens indicate a comment in the physical line.

        """
        comment_in_line = any(token_type == tokenize.COMMENT
                              for token_type, _, _, _, _ in self._tokens)

        if comment_in_line:
            filtered_source = ''.join(
                filter_commented_out_code(
                    self._physical_line,
                    self._options,
                ))
            return self._physical_line != filtered_source
        return False
Beispiel #2
0
    def test_filter_commented_out_code(self):
        self.assertEqual(
            """\
# This is a comment.

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
""",
            ''.join(eradicate.filter_commented_out_code(
                """\
# print(5)
# This is a comment.
# x = 1

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
# try:
#     x = 1
# finally:
#     x = 0
""")))
Beispiel #3
0
    def test_filter_commented_out_code_should_avoid_escaped_newlines(self):
        line = """\
if False: \\
# print(3)
    print(2)
    print(3)
"""
        self.assertEqual(line,
                         ''.join(eradicate.filter_commented_out_code(line)))
Beispiel #4
0
    def test_filter_commented_out_code_should_avoid_escaped_newlines(self):
        line = """\
if False: \\
# print(3)
    print(2)
    print(3)
"""
        self.assertEqual(
            line,
            ''.join(eradicate.filter_commented_out_code(line)))
    def run(self, filename, file):
        """
        Detects commented out source code in Python.
        """
        corrected = tuple(eradicate.filter_commented_out_code(''.join(file)))

        for diff in Diff.from_string_arrays(file, corrected).split_diff():
            yield Result(self,
                         "This file contains commented out source code.",
                         affected_code=(diff.range(filename),),
                         diffs={filename: diff})
Beispiel #6
0
    def test_filter_commented_out_code_without_aggressive(self):
        code = """\
# iterate through choices inside of parenthesis (separated by '|'):

# if the Optional takes a value, format is:
#    -s ARGS, --long ARGS
"""
        self.assertEqual(
            code,
            ''.join(eradicate.filter_commented_out_code(code,
                                                        aggressive=False)))
class PyCommentedCodeBear(CorrectionBasedBear):
    GET_REPLACEMENT = staticmethod(lambda file: (list(
        eradicate.filter_commented_out_code(''.join(file))), []))
    RESULT_MESSAGE = "This file contains commented out source code."

    def run(self, filename, file):
        """
        Detects commented out source code in Python.
        """
        for result in self.retrieve_results(filename, file):
            yield result
Beispiel #8
0
    def run(self, filename, file):
        """
        Detects commented out source code in Python.
        """
        corrected = tuple(eradicate.filter_commented_out_code(''.join(file)))

        for diff in Diff.from_string_arrays(file, corrected).split_diff():
            yield Result(self,
                         'This file contains commented out source code.',
                         affected_code=(diff.range(filename),),
                         diffs={filename: diff})
Beispiel #9
0
    def test_filter_commented_out_code_with_annotation(self):
        self.assertEqual(
            '\n\n\n', ''.join(
                eradicate.filter_commented_out_code("""\
# class CommentedClass(object):
#     def __init__(self, prop: int) -> None:
#         self.property = prop

#     def __str__(self) -> str:
#         return self.__class__.__name__

#    def set_prop(self, prop: int):
#        self.prop = prop

#    def get_prop(self):
#        return self.prop
""")))
Beispiel #10
0
    def test_filter_commented_out_code_with_larger_example(self):
        self.assertEqual(
            """\
# This is a comment.

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
""", ''.join(
                eradicate.filter_commented_out_code("""\
# print(5)
# This is a comment.
# x = 1

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
""")))
Beispiel #11
0
    def test_filter_commented_out_code_with_larger_example(self):
        self.assertEqual(
            """\
# This is a comment.

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
""",
            ''.join(eradicate.filter_commented_out_code(
                """\
# print(5)
# This is a comment.
# x = 1

y = 1  # x = 3

# The below is another comment.
# 3 / 2 + 21
""")))
Beispiel #12
0
 def lint(self, filename, file):
     output = list(eradicate.filter_commented_out_code(''.join(file)))
     return self.process_output(output, filename, file)