yardcheck icon indicating copy to clipboard operation
yardcheck copied to clipboard

Also check if a parameter is a `Module`

Open backus opened this issue 9 years ago • 0 comments

Ideally there would be a way to distinguish between these two method calls

module Bar
  def self.example1(value)
  end

  def self.example2(value)
  end
end

Bar.example1(String.new)
Bar.example2(String)

but unfortunately it seems like the best you can do is either

  1. Write a pretty unhelpful doc like so

    # @param value [Class]
    def self.example2(value)
    end
    

    this is correct but preferably it would say that it ONLY takes the String class if that is the contract

  2. Write an incorrect but helpful doc like so

    # @param value [String]
    def self.example2(value)
    end
    

    this means an instance of String but a lot of people write this anyways because it is usually clear with context

  3. Write an invalid but helpful and syntactically different doc like so

    # @param value [Class:String]
    def self.example2(value)
    end
    

    YARD doesn't support this Class:Blah syntax but it does differentiate from "an instance of String" and it is also helpful.

I'm not really interested in supporting a superset of YARD type annotations (unless yardcheck really took off I guess and Loren had more or less officially moved on from ruby / YARD). I also think it is dumb to tell people to make their documentation useless by writing @param value [Class] and maybe writing a note after the docs. The point of YARD is to have good metadata alongside docs.

I think the best solution is basically supporting the ambiguous syntax until a better alternative is clear. Therefore, both example1 and example2 would be documented the same way (@param value [String]) and the check would look like

observed_value.is_a?(String) || (observed_value.is_a?(Module) && observed_value < String)

backus avatar Mar 20 '17 01:03 backus