Class: ActionviewPrecompiler::RipperASTParser::Node

Inherits:
Array
  • Object
show all
Defined in:
lib/actionview_precompiler/ast_parser/ripper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, arr, opts = {}) ⇒ Node

Returns a new instance of Node.



10
11
12
13
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 10

def initialize(type, arr, opts = {})
  @type = type
  super(arr)
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 8

def type
  @type
end

Instance Method Details

#argument_nodesObject



34
35
36
37
38
39
40
41
42
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 34

def argument_nodes
  raise unless fcall?
  return [] if self[1].nil?
  if self[1].last == false || self[1].last.type == :vcall
    self[1][0...-1]
  else
    self[1][0..-1]
  end
end

#call?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 56

def call?
  type == :call
end

#call_method_nameObject



64
65
66
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 64

def call_method_name
  self.last.first
end

#childrenObject



15
16
17
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 15

def children
  to_a
end

#fcall?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 24

def fcall?
  type == :command || type == :fcall
end

#fcall_named?(name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 28

def fcall_named?(name)
  fcall? &&
    self[0].type == :@ident &&
    self[0][0] == name
end

#hash?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 75

def hash?
  type == :bare_assoc_hash || type == :hash
end

#hash_from_body(body) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 91

def hash_from_body(body)
  body.map do |hash_node|
    return nil if hash_node.type != :assoc_new

    [hash_node[0], hash_node[1]]
  end.to_h
end

#inspectObject



19
20
21
22
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 19

def inspect
  typeinfo = type && type != :list ? ':' + type.to_s + ', ' : ''
  's(' + typeinfo + map(&:inspect).join(", ") + ')'
end

#string?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 44

def string?
  type == :string_literal
end

#symbol?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 99

def symbol?
  type == :@label || type == :symbol_literal
end

#to_hashObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 79

def to_hash
  if type == :bare_assoc_hash
    hash_from_body(self[0])
  elsif type == :hash && self[0] == nil
    {}
  elsif type == :hash && self[0].type == :assoclist_from_args
    hash_from_body(self[0][0])
  else
    raise "not a hash? #{inspect}"
  end
end

#to_stringObject



68
69
70
71
72
73
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 68

def to_string
  raise unless string?
  raise "fixme" unless self[0].type == :string_content
  raise "fixme" unless self[0][0].type == :@tstring_content
  self[0][0][0]
end

#to_symbolObject



103
104
105
106
107
108
109
110
111
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 103

def to_symbol
  if type == :@label && self[0] =~ /\A(.+):\z/
    $1.to_sym
  elsif type == :symbol_literal && self[0].type == :symbol && self[0][0].type == :@ident
    self[0][0][0].to_sym
  else
    raise "not a symbol?: #{self.inspect}"
  end
end

#variable_nameObject



60
61
62
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 60

def variable_name
  self[0][0]
end

#variable_reference?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 48

def variable_reference?
  type == :var_ref
end

#vcall?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/actionview_precompiler/ast_parser/ripper.rb', line 52

def vcall?
  type == :vcall
end