Skip to content

Commit

Permalink
Add empty-comment checker extension
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
orSolocate committed Oct 7, 2020
1 parent 98d2c06 commit d66cd83
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions doc/whatsnew/2.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Summary -- Release highlights
New checkers
============

* Add `empty-comment` check for empty comments.

Other Changes
=============
Expand Down
56 changes: 56 additions & 0 deletions pylint/extensions/empty_comment.py
Original file line number Diff line number Diff line change
@@ -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))
9 changes: 9 additions & 0 deletions tests/extensions/data/empty_comment.py
Original file line number Diff line number Diff line change
@@ -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"#
#######
34 changes: 34 additions & 0 deletions tests/extensions/test_empty_comment.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d66cd83

Please sign in to comment.