class Yard2steep::Comments

Constants

COMMENT_RE
PARAM_RE
RETURN_RE
S_RE
TYPE_WITH_PAREN_RE

Public Class Methods

new(text) click to toggle source

@param [String] text

# File lib/yard2steep/comments.rb, line 39
def initialize(text)
  @comments_map = extract(text)
end

Public Instance Methods

parse_from(m_loc) click to toggle source

@param [Integer] m_loc represents location of method definition @return [Array(Hash { String => String }, String)]

# File lib/yard2steep/comments.rb, line 45
def parse_from(m_loc)
  Util.assert! { m_loc >= 0 }
  reset_context!

  l = m_loc - 1
  while l >= 0
    comment = @comments_map[l]
    break unless comment  # nil when no more comment exist

    parse_comment!(comment)
    l -= 1
  end

  [@p_types, @r_type]
end

Private Instance Methods

extract(text) click to toggle source

@param [String] text @return [Hash{ String => String }]

# File lib/yard2steep/comments.rb, line 71
def extract(text)
  # NOTE: `Ripper.lex` returns array of array such as
  # [
  #   [[1, 0], :on_comment, "# @param [Array] contents\n", EXPR_BEG],
  #   ...
  # ]
  r = {}
  Ripper.lex(text).each do |t|
    # Check token type
    type = t[1]
    next if type != :on_comment
    # Check comment body
    comment = t[2]
    next unless comment.match?(COMMENT_RE)

    line = t[0][0]
    r[line] = comment
  end

  r
end
normalize_type(type) click to toggle source

@param [String] type @return [String]

# File lib/yard2steep/comments.rb, line 127
def normalize_type(type)
  Type.translate(type)
end
parse_comment!(comment) click to toggle source

@param [String] comment @return [void]

# File lib/yard2steep/comments.rb, line 95
def parse_comment!(comment)
  return if try_param_comment(comment)
  return if try_return_comment(comment)
  raise "Must not reach here!"
end
reset_context!() click to toggle source

@return [void]

# File lib/yard2steep/comments.rb, line 64
def reset_context!
  @p_types = {}
  @r_type  = nil
end
try_param_comment(comment) click to toggle source

@param [String] comment @return [bool]

# File lib/yard2steep/comments.rb, line 103
def try_param_comment(comment)
  m = comment.match(PARAM_RE)
  return false unless m

  type = normalize_type(m[1])
  name = m[2]
  @p_types[name] = type

  true
end
try_return_comment(comment) click to toggle source

@param [String] comment @return [bool]

# File lib/yard2steep/comments.rb, line 116
def try_return_comment(comment)
  m = comment.match(RETURN_RE)
  return false unless m

  @r_type = normalize_type(m[1])

  true
end