Class: WhatDyaReturn::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/what_dya_return/extractor.rb

Instance Method Summary collapse

Instance Method Details

#extract(source_code) ⇒ Array<String>

Extracts the return value candidates from ‘source_code`

Examples:


WhatDyaReturn::Extractor.new.extract("  def foo\n    if bar\n      42\n    else\n      'baz'\n    end\n  end\n")
# => ['42', '"baz"']

Raises:

  • (WhatDyaReturn::SyntaxErrfor)

    if ‘source_code` cannot be parsed



28
29
30
31
32
33
34
35
36
37
# File 'lib/what_dya_return/extractor.rb', line 28

def extract(source_code)
  processed = AST::ProcessedSource.new(source_code, RUBY_VERSION.to_f)

  raise WhatDyaReturn::SyntaxError unless processed.valid_syntax?
  raise WhatDyaReturn::ArgumentError if processed.ast.type != :def

  Processor.new.process(processed.ast).map do |node|
    node.nil? ? 'nil' : Unparser.unparse(node)
  end.uniq
end