Sha256: 6d2a414f12a43390880e28894de668f6270ba4a28fbf3cb34e9a5fc885dbea07

Contents?: true

Size: 900 Bytes

Versions: 1

Compression:

Stored size: 900 Bytes

Contents

# frozen_string_literal: true

require 'unparser'
require_relative 'processor'

module WhatDyaReturn
  class Extractor
    #
    # Extracts the return value candidates from `source_code`
    #
    # @example
    #
    #   WhatDyaReturn::Extractor.new.extract(<<-CODE)
    #     def foo
    #       if bar
    #         42
    #       else
    #         'baz'
    #       end
    #     end
    #   CODE
    #   # => ['42', '"baz"']
    #
    # @param [String] source_code
    # @return [Array<String>]
    # @raise [WhatDyaReturn::SyntaxErrfor] if `source_code` cannot be parsed
    #
    def extract(source_code)
      processed = AST::ProcessedSource.new(source_code, RUBY_VERSION.to_f)
      raise WhatDyaReturn::SyntaxError unless processed.valid_syntax?

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
what_dya_return-0.2.0 lib/what_dya_return/extractor.rb