Class: Prism::Merge::NodeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/merge/node_wrapper.rb

Overview

Wraps tree_haver nodes with a unified interface for merging. Provides #canonical_type, semantic predicates, and method_missing delegation to the underlying node.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, backend: :prism) ⇒ NodeWrapper

Returns a new instance of NodeWrapper.

Parameters:

  • node (Object)

    TreeHaver::Backends::Prism::Node or similar

  • backend (Symbol) (defaults to: :prism)

    The backend identifier



26
27
28
29
# File 'lib/prism/merge/node_wrapper.rb', line 26

def initialize(node, backend: :prism)
  @node = node
  @backend = backend
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block_arg) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/prism/merge/node_wrapper.rb', line 69

def method_missing(name, *args, **kwargs, &block_arg)
  if @node.respond_to?(name)
    @node.public_send(name, *args, **kwargs, &block_arg)
  else
    super
  end
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



9
10
11
# File 'lib/prism/merge/node_wrapper.rb', line 9

def backend
  @backend
end

#nodeObject (readonly)

Returns the value of attribute node.



9
10
11
# File 'lib/prism/merge/node_wrapper.rb', line 9

def node
  @node
end

Class Method Details

.wrap(node, backend: :prism) ⇒ NodeWrapper?

Wrap a node, returning nil for nil input.

Parameters:

  • node (Object, nil)

    Node to wrap

  • backend (Symbol) (defaults to: :prism)

    The backend used for parsing (defaults to :prism)

Returns:

  • (NodeWrapper, nil)

    Wrapped node or nil if node is nil



17
18
19
20
21
# File 'lib/prism/merge/node_wrapper.rb', line 17

def wrap(node, backend: :prism)
  return if node.nil?

  new(node, backend: backend)
end

Instance Method Details

#begin?Boolean

Returns:

  • (Boolean)


63
# File 'lib/prism/merge/node_wrapper.rb', line 63

def begin? = canonical_type == :begin

#call?Boolean

Returns:

  • (Boolean)


48
# File 'lib/prism/merge/node_wrapper.rb', line 48

def call? = canonical_type == :call

#call_with_block?Boolean

Returns:

  • (Boolean)


49
# File 'lib/prism/merge/node_wrapper.rb', line 49

def call_with_block? = call? && block

#canonical_typeSymbol

Get the canonical (normalized) type for this node

Returns:

  • (Symbol)


39
40
41
# File 'lib/prism/merge/node_wrapper.rb', line 39

def canonical_type
  NodeTypeNormalizer.canonical_type(@node.type, @backend)
end

#case?Boolean

Returns:

  • (Boolean)


58
# File 'lib/prism/merge/node_wrapper.rb', line 58

def case? = canonical_type == :case

#case_match?Boolean

Returns:

  • (Boolean)


59
# File 'lib/prism/merge/node_wrapper.rb', line 59

def case_match? = canonical_type == :case_match

#class?Boolean

Returns:

  • (Boolean)


45
# File 'lib/prism/merge/node_wrapper.rb', line 45

def class? = canonical_type == :class

#const?Boolean

Returns:

  • (Boolean)


50
# File 'lib/prism/merge/node_wrapper.rb', line 50

def const? = canonical_type == :const

#cvar?Boolean

Returns:

  • (Boolean)


53
# File 'lib/prism/merge/node_wrapper.rb', line 53

def cvar? = canonical_type == :cvar

#def?Boolean

Semantic predicates

Returns:

  • (Boolean)


44
# File 'lib/prism/merge/node_wrapper.rb', line 44

def def? = canonical_type == :def

#for?Boolean

Returns:

  • (Boolean)


62
# File 'lib/prism/merge/node_wrapper.rb', line 62

def for? = canonical_type == :for

#gvar?Boolean

Returns:

  • (Boolean)


54
# File 'lib/prism/merge/node_wrapper.rb', line 54

def gvar? = canonical_type == :gvar

#if?Boolean

Returns:

  • (Boolean)


56
# File 'lib/prism/merge/node_wrapper.rb', line 56

def if? = canonical_type == :if

#ivar?Boolean

Returns:

  • (Boolean)


52
# File 'lib/prism/merge/node_wrapper.rb', line 52

def ivar? = canonical_type == :ivar

#lambda?Boolean

Returns:

  • (Boolean)


65
# File 'lib/prism/merge/node_wrapper.rb', line 65

def lambda? = canonical_type == :lambda

#local_var?Boolean

Returns:

  • (Boolean)


51
# File 'lib/prism/merge/node_wrapper.rb', line 51

def local_var? = canonical_type == :local_var

#module?Boolean

Returns:

  • (Boolean)


46
# File 'lib/prism/merge/node_wrapper.rb', line 46

def module? = canonical_type == :module

#multi_write?Boolean

Returns:

  • (Boolean)


55
# File 'lib/prism/merge/node_wrapper.rb', line 55

def multi_write? = canonical_type == :multi_write

#post_execution?Boolean

Returns:

  • (Boolean)


67
# File 'lib/prism/merge/node_wrapper.rb', line 67

def post_execution? = canonical_type == :post_execution

#pre_execution?Boolean

Returns:

  • (Boolean)


66
# File 'lib/prism/merge/node_wrapper.rb', line 66

def pre_execution? = canonical_type == :pre_execution

#rescue?Boolean

Returns:

  • (Boolean)


64
# File 'lib/prism/merge/node_wrapper.rb', line 64

def rescue? = canonical_type == :rescue

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/prism/merge/node_wrapper.rb', line 77

def respond_to_missing?(name, include_private = false)
  @node.respond_to?(name, include_private) || super
end

#singleton_class?Boolean

Returns:

  • (Boolean)


47
# File 'lib/prism/merge/node_wrapper.rb', line 47

def singleton_class? = canonical_type == :singleton_class

#typeString, Symbol

Get the raw type from the underlying node

Returns:

  • (String, Symbol)


33
34
35
# File 'lib/prism/merge/node_wrapper.rb', line 33

def type
  @node.type
end

#unless?Boolean

Returns:

  • (Boolean)


57
# File 'lib/prism/merge/node_wrapper.rb', line 57

def unless? = canonical_type == :unless

#until?Boolean

Returns:

  • (Boolean)


61
# File 'lib/prism/merge/node_wrapper.rb', line 61

def until? = canonical_type == :until

#while?Boolean

Returns:

  • (Boolean)


60
# File 'lib/prism/merge/node_wrapper.rb', line 60

def while? = canonical_type == :while