Class: ActionviewPrecompiler::PrismASTParser::RenderNode

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

Overview

This class represents a node in the tree that is returned by the parser that corresponds to an argument to a render call, or a child of one of those nodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ RenderNode

Returns a new instance of RenderNode.



28
29
30
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 28

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



26
27
28
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 26

def node
  @node
end

Instance Method Details

#call?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 32

def call?
  node.is_a?(Prism::CallNode)
end

#call_method_nameObject



62
63
64
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 62

def call_method_name
  node.message
end

#hash?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 36

def hash?
  node.is_a?(Prism::HashNode) || node.is_a?(Prism::KeywordHashNode)
end

#string?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 40

def string?
  node.is_a?(Prism::StringNode)
end

#symbol?Boolean

Returns:

  • (Boolean)


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

def symbol?
  node.is_a?(Prism::SymbolNode)
end

#to_hashObject

Converts the node into a hash where the keys and values are nodes. This will raise an error if the hash doesn’t match the format we expect or if the hash contains any splats.



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

def to_hash
  if hash?
    if node.elements.all? { |assoc| assoc.is_a?(Prism::AssocNode) && assoc.key.is_a?(Prism::SymbolNode) }
      node.elements.to_h { |assoc| [RenderNode.new(assoc.key), RenderNode.new(assoc.value)] }
    end
  else
    raise CompilationError, "Unexpected node type: #{node.inspect}"
  end
end

#to_stringObject

Converts the node into a string value. Only handles plain string content, and will raise an error if the node contains interpolation.



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

def to_string
  if string?
    node.unescaped
  else
    raise CompilationError, "Unexpected node type: #{node.inspect}"
  end
end

#to_symbolObject

Converts the node into a symbol value. Only handles labels and plain symbols, and will raise an error if the node contains interpolation.



101
102
103
104
105
106
107
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 101

def to_symbol
  if symbol?
    node.unescaped.to_sym
  else
    raise CompilationError, "Unexpected node type: #{node.inspect}"
  end
end

#variable_nameObject



66
67
68
69
70
71
72
73
74
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 66

def variable_name
  case node.type
  when :class_variable_read_node, :instance_variable_read_node,
       :global_variable_read_node, :local_variable_read_node
    node.name.name
  when :call_node
    node.message
  end
end

#variable_reference?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 48

def variable_reference?
  case node.type
  when :class_variable_read_node, :instance_variable_read_node,
       :global_variable_read_node, :local_variable_read_node
    true
  else
    false
  end
end

#vcall?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/actionview_precompiler/ast_parser/prism.rb', line 58

def vcall?
  node.is_a?(Prism::CallNode) && node.variable_call?
end