From d66cd83fae25ea17c4cc7f4f3a5f59d624bafe3a Mon Sep 17 00:00:00 2001 From: Or Date: Fri, 2 Oct 2020 18:19:31 +0300 Subject: [PATCH] Add empty-comment checker extension PR Review: refactor checker messages, test cases added: empty commit case, comments row case. unittest - pathlib replaces os.path. python3.5 fix for pathlib library use refactor `comment_ending` to `empty_comment` empty-comment test fixed (after Rebase) --- ChangeLog | 3 ++ doc/whatsnew/2.7.rst | 1 + pylint/extensions/empty_comment.py | 56 ++++++++++++++++++++++++++ tests/extensions/data/empty_comment.py | 9 +++++ tests/extensions/test_empty_comment.py | 34 ++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 pylint/extensions/empty_comment.py create mode 100644 tests/extensions/data/empty_comment.py create mode 100644 tests/extensions/test_empty_comment.py diff --git a/ChangeLog b/ChangeLog index 10aee60a9e..ac718ef00c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,8 +2,11 @@ Pylint's ChangeLog ------------------ +* Add check for empty comments + * Fix minor documentation issue in contribute.rst + What's New in Pylint 2.6.1? =========================== Release date: 2020-09-08 diff --git a/doc/whatsnew/2.7.rst b/doc/whatsnew/2.7.rst index 902b4cccb1..e53eb3fb75 100644 --- a/doc/whatsnew/2.7.rst +++ b/doc/whatsnew/2.7.rst @@ -13,6 +13,7 @@ Summary -- Release highlights New checkers ============ +* Add `empty-comment` check for empty comments. Other Changes ============= diff --git a/pylint/extensions/empty_comment.py b/pylint/extensions/empty_comment.py new file mode 100644 index 0000000000..2ab7da9a14 --- /dev/null +++ b/pylint/extensions/empty_comment.py @@ -0,0 +1,56 @@ +from pylint.checkers import BaseChecker +from pylint.interfaces import IRawChecker + + +def is_line_commented(line): + """ Checks if a `# symbol that is not part of a string was found in line""" + + comment_idx = line.find(b"#") + if comment_idx == -1: + return False + if comment_part_of_string(line, comment_idx): + return is_line_commented(line[:comment_idx] + line[comment_idx + 1 :]) + return True + + +def comment_part_of_string(line, comment_idx): + """ checks if the symbol at comment_idx is part of a string """ + + if ( + line[:comment_idx].count(b"'") % 2 == 1 + and line[comment_idx:].count(b"'") % 2 == 1 + ) or ( + line[:comment_idx].count(b'"') % 2 == 1 + and line[comment_idx:].count(b'"') % 2 == 1 + ): + return True + return False + + +class CommentChecker(BaseChecker): + __implements__ = IRawChecker + + name = "refactoring" + msgs = { + "R2044": ( + "Line with empty comment", + "empty-comment", + ( + "Used when a # symbol appears on a line not followed by an actual comment" + ), + ) + } + options = () + priority = -1 # low priority + + def process_module(self, node): + with node.stream() as stream: + for (line_num, line) in enumerate(stream): + line = line.rstrip() + if line.endswith(b"#"): + if not is_line_commented(line[:-1]): + self.add_message("empty-comment", line=line_num) + + +def register(linter): + linter.register_checker(CommentChecker(linter)) diff --git a/tests/extensions/data/empty_comment.py b/tests/extensions/data/empty_comment.py new file mode 100644 index 0000000000..8a18df2ebd --- /dev/null +++ b/tests/extensions/data/empty_comment.py @@ -0,0 +1,9 @@ +"""empty-comment test-case""" +A = 5 # +# +A = '#' + '1' +print(A) # +print("A=", A) # should not be an error# +A = "#pe\0ace#love#" # +A = "peace#love" # \0 peace'#'''' love#peace'''-'#love'-"peace#love"# +####### diff --git a/tests/extensions/test_empty_comment.py b/tests/extensions/test_empty_comment.py new file mode 100644 index 0000000000..87ddc1c8ec --- /dev/null +++ b/tests/extensions/test_empty_comment.py @@ -0,0 +1,34 @@ +from pathlib import Path + +import pytest + +import pylint.extensions.empty_comment as empty_comment + + +@pytest.fixture(scope="module") +def checker(): + return empty_comment.CommentChecker + + +@pytest.fixture(scope="module") +def enable(): + return ["empty-comment"] + + +@pytest.fixture(scope="module") +def disable(): + return ["all"] + + +def test_comment_base_case(linter): + comment_test = str(Path(__file__).parent.joinpath("data", "empty_comment.py")) + linter.check([comment_test]) + msgs = linter.reporter.messages + assert len(msgs) == 4 + for msg in msgs: + assert msg.symbol == "empty-comment" + assert msg.msg == "Line with empty comment" + assert msgs[0].line == 1 + assert msgs[1].line == 2 + assert msgs[2].line == 4 + assert msgs[3].line == 6