module SyntaxTree::Reflection::Type

This module represents the type of the values being passed to attributes of nodes. It is used as part of the documentation of the attributes.

Constants

CONSTANTS

Public Class Methods

parse(comment) click to toggle source
# File lib/syntax_tree/reflection.rb, line 66
def parse(comment)
  comment = comment.gsub("\n", " ")

  unless comment.start_with?("[")
    raise "Comment does not start with a bracket: #{comment.inspect}"
  end

  count = 1
  found =
    comment.chars[1..]
      .find
      .with_index(1) do |char, index|
        count += { "[" => 1, "]" => -1 }.fetch(char, 0)
        break index if count == 0
      end

  # If we weren't able to find the end of the balanced brackets, then
  # the comment is malformed.
  if found.nil?
    raise "Comment does not have balanced brackets: #{comment.inspect}"
  end

  parse_type(comment[1...found].strip)
end