Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting tag information from files #1076

Closed
lal-s opened this issue Feb 27, 2017 · 5 comments
Closed

Extracting tag information from files #1076

lal-s opened this issue Feb 27, 2017 · 5 comments
Labels

Comments

@lal-s
Copy link

lal-s commented Feb 27, 2017

I am trying to parse through each file and get the @tag value.

Example

class HelloWorld
# This is an example
# @return greeting
def say_hello(name)
    return "Hello World"
end
end

If I want to retrieve to retrieve @return from yard documentation. What would be the best way to do it.

Thanks :)

@nvoynov
Copy link

nvoynov commented Mar 10, 2017

Thanks for you question, and I'm also interested in something like this.

I'm going to place links to functional requirements in source code comments by custom tag, and then do some traceability automation, by extracting source code items related to requirements ... just all source code items where exists @requirement tag.

Something like this

# @requirement fr.foo
class Foo 
 # @requirement fr.foo.bar
 def bar
 end
end

Can I get something next?

[{file: 'foo.rb', class: 'Foo'}, {file: 'foo.rb', meth: 'Foo#bar'}]

@lsegal
Copy link
Owner

lsegal commented Apr 24, 2017

Sorry for the delay on getting to this.

YARD has an extensive programmatic API that lets you do a lot of code auditing. Check out the Architecture Overview for some high level docs that drill down into what you may need. Generally you'll want to look at the Registry, CodeObjects, and tags.

The short example is:

require 'yard'

# Equivalent to running `yard -n` before this script
YARD::CLI::Yardoc.run('-n')

# Load the registry that was written by above command
YARD::Registry.load

# Get all the method objects
meths = YARD::Registry.all(:method)
p meths

# Get all the objects with only 1 @return tag
objs = YARD::Registry.all.select do |obj|
  obj.has_tag?(:return) && obj.tags(:return).size == 1
end

# Get all objects defined in foo.rb
objs = YARD::Registry.all.select do |obj|
  obj.files.include? 'foo.rb'
end

Hope that helps!

@ericazz17
Copy link

@isegal I am wondering how do you print the return types in the code above?

@lsegal
Copy link
Owner

lsegal commented Jan 14, 2020

@EricaZhang17 you can see the tags documentation linked above or see Docstring#tags but basically the obj.tags(:return) object you get in the above example will return a Tag object that has a types property. See YARD::Tags::Tag for more information there.

@ericazz17
Copy link

ericazz17 commented Jan 14, 2020

I am wondering if it is possible to render the tag information so that it for each method, there can be a list of information like ClassName, MethodName, "(Param1Type, Param2Type, ...) -> ReturnType" ?

Right now using p obj.tags(:param) or p obj.tags(:return), I am able to get a list of objects [#<YARD::Tags::Tag:0x007fea390943b0 @tag_name="param", @text="an Object to compare this {Country} with.", @name="c", @types=["Object"], @object=#<yardoc method TZInfo::Country#eql?>>] but I can't seem to combine the two type information together. Thank you!@isegal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants