sfcgeorge / yard-contracts

YARD Plugin for Automatic Param Docs from Contracts.
MIT License
26 stars 3 forks source link

Returning a hash has a weird rendering #6

Closed NobbZ closed 9 years ago

NobbZ commented 9 years ago

Hi!

I have the following (simplified) function and the generated documentation looks weird:

Contract Token => {success: Bool, message: String}
def unlock token
  {success: true, message: "Yeah!"}
end

The generated documentation shows up like following:

Returns:

  • ({:success=>Bool, :message=>String}) - +:message=>String+

Also this function shows up twice! Once as a class method returning the Hash but taking no arguments and once as instance method returning Object and taking the argument token in the summaries.

Even more strange is, that in the Class Method Details the parameter token is explained but not in the method head, the instance method is left undocumented but has the link to the source.

edit The promised screenshot:

Screenshot that shows the described problem

sfcgeorge commented 9 years ago

Hi, thanks for opening this issue. Wow that is weird in many ways. I've tested class and instance methods, they usually show up separately without duplication. Instance is the default so I have no idea why it would think it's a class method.

Showing up twice makes perfect sense to me though. As the Contract call comes before the method, yard-contracts has to stub out the method documentation with params, then YARD gets to the actual method and documents the rest. If yard-contracts has got it right, then YARD will merge it's method doc with the one from yard-contracts and all is well, no duplication. But as yard-contracts has mistakenly identified it as a class method it shows up once there, then YARD gets it right so it shows up again as instance.

The signature being wrong is explained because YARD fills that bit in. So the questions are, why does yard-contracts think it's a class method and why do hash return values get messed up. I'll look into it.

NobbZ commented 9 years ago

In another file with another function that returns a hash I can't reproduce it.

Even though removing everything yard generates did not change anything, but deleting the function, run yard and then reinsert the function seems to have solved this problem. Which occured also with non-hash return types in my experiments I did last night.

I wanted to gather some data about what and why, and all I can tell now is: "sorry can't reproduce anymore"...

NobbZ commented 9 years ago

It is getting even more strange...

After refactoring that function again the problem reappeared.

I was able to figure out that it has nothing to do with the return type but with the content of the function!

Documentation for the following is generated fine:

class User
  def foo
    self.username
  end
end

While the following has the "two method" problem as descroibed above:

class User
  include Contracts

  Contract None => String
  def foo
    self.username
  end
end

So as soon I have an occurence of self.anything in the function the problem occurs. Even if I could circumvent the usage of self by renaming the parameter, I prefer to always use self for calling methods of the current object.

It does not matter if I define the method username with a contract or without or leaving it undefined/implicitely created by ActiveRecord.

NobbZ commented 9 years ago

I will close this one and open a new issue, because the title of this issue is wrong and misleading.