Skip to content

Commit dccce31

Browse files
committed
DocStyleBear: Add MalformedComment support
DocStyleBear will use new error handling mechanism `MalformedComment`. And yield a `RESULT` with a message containing error details. Related to coala/coala#4548
1 parent d25f244 commit dccce31

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

bears/documentation/DocumentationStyleBear.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from coalib.bearlib.languages.documentation.DocumentationComment import (
2-
DocumentationComment)
2+
DocumentationComment, MalformedComment)
33
from coalib.bearlib.languages.documentation.DocstyleDefinition import (
44
DocstyleDefinition)
55
from coalib.bearlib.languages.documentation.DocBaseClass import (
@@ -124,22 +124,29 @@ def run(self, filename, file, language: str,
124124
"""
125125

126126
for doc_comment in self.extract(file, language, docstyle):
127-
parsed = doc_comment.parse()
128-
129-
(new_metadata, warning_desc) = self.process_documentation(
130-
parsed, allow_missing_func_desc, indent_size,
131-
expand_one_liners)
132-
133-
new_comment = DocumentationComment.from_metadata(
134-
new_metadata, doc_comment.docstyle_definition,
135-
doc_comment.marker, doc_comment.indent, doc_comment.position)
136-
137-
if new_comment != doc_comment:
138-
# Something changed, let's apply a result.
139-
diff = self.generate_diff(file, doc_comment, new_comment)
140-
141-
yield Result(
127+
if isinstance(doc_comment, MalformedComment):
128+
yield Result.from_values(
142129
origin=self,
143-
message=warning_desc,
144-
affected_code=(diff.range(filename),),
145-
diffs={filename: diff})
130+
message=doc_comment.message,
131+
file=filename,
132+
line=doc_comment.line + 1)
133+
else:
134+
parsed = doc_comment.parse()
135+
136+
(new_metadata, warning_desc) = self.process_documentation(
137+
parsed, allow_missing_func_desc, indent_size,
138+
expand_one_liners)
139+
140+
new_comment = DocumentationComment.from_metadata(
141+
new_metadata, doc_comment.docstyle_definition,
142+
doc_comment.marker, doc_comment.indent,
143+
doc_comment.position)
144+
145+
if new_comment != doc_comment:
146+
# Something changed, let's apply a result.
147+
diff = self.generate_diff(file, doc_comment, new_comment)
148+
yield Result(
149+
origin=self,
150+
message=warning_desc,
151+
affected_code=(diff.range(filename),),
152+
diffs={filename: diff})

tests/documentation/DocumentationStyleBearTest.py

+36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from queue import Queue
2+
from textwrap import dedent
23
import os.path
34
import unittest
45

@@ -47,6 +48,24 @@ def test_function(self):
4748
return test_function
4849

4950

51+
def test_MalformedComment(test_data, message, optional_setting=None):
52+
def test_MalformedComment_function(self):
53+
arguments = {'language': 'python', 'docstyle': 'default'}
54+
if optional_setting:
55+
arguments.update(optional_setting)
56+
section = Section('test-section')
57+
for key, value in arguments.items():
58+
section[key] = value
59+
with execute_bear(
60+
DocumentationStyleBear(section, Queue()),
61+
'dummy_file',
62+
test_data,
63+
**arguments) as results:
64+
self.assertEqual(results[0].message, message)
65+
66+
return test_MalformedComment_function
67+
68+
5069
class DocumentationStyleBearTest(unittest.TestCase):
5170
test_bad1 = test('bad_file.py.test', 'bad_file.py.test.correct')
5271
test_bad2 = test('bad_file2.py.test', 'bad_file2.py.test.correct',
@@ -59,3 +78,20 @@ class DocumentationStyleBearTest(unittest.TestCase):
5978
test_good2 = test('good_file2.py.test', 'good_file2.py.test')
6079
test_good3 = test('good_file3.py.test', 'good_file3.py.test',
6180
{'allow_missing_func_desc': 'True'})
81+
82+
test_malformed_comment_python = test_MalformedComment(
83+
['"""\n',
84+
'This will yield MalformedComment'],
85+
dedent("""\
86+
Please check the docstring for faulty markers. A starting
87+
marker has been found, but no instance of DocComment is
88+
returned."""))
89+
90+
test_malformed_comment_java = test_MalformedComment(
91+
['\n',
92+
'/** This will yield MalformedComment\n'],
93+
dedent("""\
94+
Please check the docstring for faulty markers. A starting
95+
marker has been found, but no instance of DocComment is
96+
returned."""),
97+
{'language': 'java'})

0 commit comments

Comments
 (0)