7
7
8
8
import astroid
9
9
10
- from pylint .interfaces import IAstroidChecker
11
10
from pylint .checkers import BaseChecker
12
11
13
12
14
13
class CommandMissingDocumentation (BaseChecker ):
15
14
"""Checker to ensure command docstrings include all information for docs."""
16
15
17
- __implements__ = IAstroidChecker
18
-
19
16
name = "command-docstring"
20
17
21
18
name_ambiguous = "ambiguous-register"
@@ -68,9 +65,10 @@ def visit_functiondef(self, node):
68
65
return
69
66
argnames = {arg .name .replace ("_" , "-" ) for arg in node .args .args }
70
67
regular_argnames = argnames - {"self" , "count" }
71
- self ._check_args_section (node , regular_argnames )
72
- self ._check_count_section (node , argnames )
73
- self ._check_syntax_section (node , regular_argnames )
68
+ docstr = node .doc_node .value
69
+ self ._check_args_section (node , docstr , regular_argnames )
70
+ self ._check_count_section (node , docstr , argnames )
71
+ self ._check_syntax_section (node , docstr , regular_argnames )
74
72
75
73
@staticmethod
76
74
def sections (docstr ):
@@ -85,25 +83,25 @@ def sections(docstr):
85
83
content += line
86
84
return sections
87
85
88
- def _check_syntax_section (self , node , argnames ):
86
+ def _check_syntax_section (self , node , docstr , argnames ):
89
87
"""Check if a syntax section is available for commands with arguments."""
90
88
if not argnames :
91
89
return
92
- for section in self .sections (node . doc ):
90
+ for section in self .sections (docstr ):
93
91
if re .match (r"\*\*syntax:\*\* ``.*``" , section .strip ()):
94
92
return
95
93
self .add_message (self .name_syntax , node = node , args = (node .name ,))
96
94
97
- def _check_count_section (self , node , argnames ):
95
+ def _check_count_section (self , node , docstr , argnames ):
98
96
"""Check if a count section is available for commands that support count."""
99
97
if "count" not in argnames :
100
98
return
101
- if "**count:**" not in node . doc :
99
+ if "**count:**" not in docstr :
102
100
self .add_message (self .name_count , node = node , args = (node .name ,))
103
101
104
- def _check_args_section (self , node , argnames ):
102
+ def _check_args_section (self , node , docstr , argnames ):
105
103
"""Check if all command arguments are documented."""
106
- docstring_argnames = self ._get_args_from_docstring (node )
104
+ docstring_argnames = self ._get_args_from_docstring (node , docstr )
107
105
difference = argnames - docstring_argnames
108
106
for argname in difference :
109
107
self .add_message (
@@ -145,7 +143,7 @@ def _is_command(self, node) -> bool:
145
143
return True
146
144
return False
147
145
148
- def _get_args_from_docstring (self , node ) -> Set [str ]:
146
+ def _get_args_from_docstring (self , node , docstr ) -> Set [str ]:
149
147
"""Retrieve documented arguments from command docstring.
150
148
151
149
If an argument is not correctly formatted in the documentation section, the
@@ -154,11 +152,10 @@ def _get_args_from_docstring(self, node) -> Set[str]:
154
152
Returns:
155
153
Set of all documented argument names.
156
154
"""
157
- docstr = node .doc
158
155
if docstr is None :
159
156
self .add_message (self .name_missing , node = node , args = (node .name ,))
160
157
return set ()
161
- lines = [line .strip () for line in node . doc .split ("\n " )]
158
+ lines = [line .strip () for line in docstr .split ("\n " )]
162
159
163
160
def _get_args (identifier , pattern ):
164
161
try :
0 commit comments