Refactor to have multiple return params

This commit is contained in:
cd10012 2019-10-12 21:11:56 -04:00 committed by chriseth
parent 30ea41c36d
commit b3ae601e88
2 changed files with 75 additions and 8 deletions

View File

@ -99,16 +99,54 @@ Json::Value Natspec::devDocumentation(ContractDefinition const& _contractDef)
if (auto fun = dynamic_cast<FunctionDefinition const*>(&it.second->declaration()))
{
Json::Value method(devDocumentation(fun->annotation().docTags));
if (!method.empty())
// add the function, only if we have any documentation to add
if (!method.empty())
{
Json::Value ret(Json::objectValue);
// for constructors, the "return" node will never exist. invalid tags
// will already generate an error within dev::solidity::DocStringAnalyzer.
auto returnParams = fun->returnParameters();
auto returnDoc = fun->annotation().docTags.equal_range("return");
if (!returnParams.empty())
{
// if there is no name then append subsequent return notices like previous behavior
string value;
unsigned int n = 0;
for (auto i = returnDoc.first; i != returnDoc.second; i++)
{
string paramName = returnParams.at(n)->name();
if (!paramName.empty())
{
ret[paramName] = Json::Value(i->second.content);
n++;
}
else
{
value += i->second.content;
method["return"] = Json::Value(value);
}
}
}
if (!ret.empty())
method["return"] = ret;
methods[it.second->externalSignature()] = method;
}
}
}
doc["methods"] = methods;
return doc;
}
string Natspec::extractDoc(multimap<string, DocTag> const& _tags, string const& _name)
{
string value;
@ -129,12 +167,6 @@ Json::Value Natspec::devDocumentation(std::multimap<std::string, DocTag> const&
if (!author.empty())
json["author"] = author;
// for constructors, the "return" node will never exist. invalid tags
// will already generate an error within dev::solidity::DocStringAnalyzer.
auto ret = extractDoc(_tags, "return");
if (!ret.empty())
json["return"] = ret;
Json::Value params(Json::objectValue);
auto paramRange = _tags.equal_range("param");
for (auto i = paramRange.first; i != paramRange.second; ++i)

View File

@ -416,6 +416,41 @@ BOOST_AUTO_TEST_CASE(dev_return_desc_after_nl)
}
BOOST_AUTO_TEST_CASE(dev_return_desc_multiple)
{
char const* sourceCode = R"(
contract test {
/// @dev Multiplies a number by 7 and adds second parameter
/// @param a Documentation for the first parameter starts here.
/// Since it's a really complicated parameter we need 2 lines
/// @param second Documentation for the second parameter
/// @return The result of the multiplication
/// @return And cookies with nutella
function mul(uint a, uint second) public returns (uint d, uint f) {
uint mul = a * 7;
return (mul, second);
}
}
)";
char const* natspec = "{"
"\"methods\":{"
" \"mul(uint256,uint256)\":{ \n"
" \"details\": \"Multiplies a number by 7 and adds second parameter\",\n"
" \"params\": {\n"
" \"a\": \"Documentation for the first parameter starts here. Since it's a really complicated parameter we need 2 lines\",\n"
" \"second\": \"Documentation for the second parameter\"\n"
" },\n"
" \"return\": {\n"
" \"d\": \"The result of the multiplication\",\n"
" \"f\": \"And cookies with nutella\"\n"
" }\n"
" }\n"
"}}";
checkNatspec(sourceCode, "test", natspec, false);
}
BOOST_AUTO_TEST_CASE(dev_multiline_return)
{
char const* sourceCode = R"(