Module: RbsActivesupport::AST

Included in:
DeclarationBuilder, Generator
Defined in:
lib/rbs_activesupport/ast.rb,
sig/rbs_activesupport/ast.rbs

Instance Method Summary collapse

Instance Method Details

#eval_args_with_options(node) ⇒ [ Array[Symbol], Hash[Symbol, untyped] ]

Parameters:

Returns:



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rbs_activesupport/ast.rb', line 13

def eval_args_with_options(node) #: [Array[Symbol], Hash[Symbol, untyped]]
  # @type var methods: Array[Symbol]
  # @type var options: Hash[Symbol, untyped]
  *args, _ = eval_node(node)
  if args.last.is_a?(Hash)
    options = args.pop
    [args, options]
  else
    [args, {}]
  end
end

#eval_include_args(node) ⇒ Array[RBS::Namespace]

Parameters:

Returns:



6
7
8
9
10
# File 'lib/rbs_activesupport/ast.rb', line 6

def eval_include_args(node) #: Array[RBS::Namespace]
  # @type var args: Array[RBS::Namespace]
  *args, _ = eval_node(node)
  args
end

#eval_node(node) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity

Parameters:

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rbs_activesupport/ast.rb', line 25

def eval_node(node) # rubocop:disable Metrics/PerceivedComplexity
  case node
  when nil
    nil
  when Symbol, Hash, RBS::Namespace # Only for debug use
    node
  when Array
    node.map { |e| eval_node(e) }
  when RubyVM::AbstractSyntaxTree::Node
    case node.type
    when :LIT, :STR, :SYM, :INTEGER, :FLOAT
      node.children.first
    when :HASH
      children = node.children.first&.children
      if children
        items = children.compact.map { |i| eval_node(i) }
        Hash[*items]
      else
        {}
      end
    when :ZLIST
      []
    when :LIST
      node.children[...-1]&.map { |e| eval_node(e) }
    when :TRUE
      true
    when :FALSE
      false
    when :NIL
      nil
    when :CONST
      RBS::Namespace.new(path: node.children, absolute: false)
    when :COLON2
      eval_node(node.children.first) + RBS::Namespace.new(path: [node.children.last], absolute: false)
    when :COLON3
      RBS::Namespace.new(path: node.children, absolute: true)
    when :CALL, :ITER
      node
    else
      p node # for debug
      raise
    end
  else
    p node # for debug
    raise
  end
end