Track function natspec comments

dev-function-comments
Simone 1 year ago
parent 456a414c84
commit 96b57fab69
  1. 15
      slither/core/declarations/function.py
  2. 17
      slither/solc_parsing/declarations/function.py

@ -227,6 +227,7 @@ class Function(SourceMapping, metaclass=ABCMeta): # pylint: disable=too-many-pu
# To be improved with a parsing of the documentation
self.has_documentation: bool = False
self._comments: Optional[str] = None
###################################################################################
###################################################################################
@ -363,6 +364,20 @@ class Function(SourceMapping, metaclass=ABCMeta): # pylint: disable=too-many-pu
def id(self, new_id: str):
self._id = new_id
@property
def comments(self) -> Optional[str]:
"""
Return the comments associated with the function.
Returns:
the comment as a string
"""
return self._comments
@comments.setter
def comments(self, comments: str):
self._comments = comments
@property
@abstractmethod
def file_scope(self) -> "FileScope":

@ -94,6 +94,23 @@ class FunctionSolc(CallerContextExpression):
if "documentation" in function_data:
function.has_documentation = True
# Old solc versions store the comment in attributes["documentation"]
# More recent ones store it in attributes["documentation"]["text"]
if (
"documentation" in function_data
and function_data["documentation"] is not None
and (
"text" in function_data["documentation"]
or isinstance(function_data["documentation"], str)
)
):
text = (
function_data["documentation"]
if isinstance(function_data["documentation"], str)
else function_data["documentation"]["text"]
)
self._function.comments = text
@property
def underlying_function(self) -> Function:
return self._function

Loading…
Cancel
Save