Class: SyntaxTree::RAssign

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

RAssign represents a single-line pattern match.

value in pattern
value => pattern

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(value:, operator:, pattern:, location:, comments: []) ⇒ RAssign

Returns a new instance of RAssign.



2850
2851
2852
2853
2854
2855
2856
# File 'lib/syntax_tree/node.rb', line 2850

def initialize(value:, operator:, pattern:, location:, comments: [])
  @value = value
  @operator = operator
  @pattern = pattern
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2848
2849
2850
# File 'lib/syntax_tree/node.rb', line 2848

def comments
  @comments
end

#operatorObject (readonly)

Kw | Op

the operator being used to match against the pattern, which is

either => or in



2842
2843
2844
# File 'lib/syntax_tree/node.rb', line 2842

def operator
  @operator
end

#patternObject (readonly)

untyped

the pattern on the right-hand side of the expression



2845
2846
2847
# File 'lib/syntax_tree/node.rb', line 2845

def pattern
  @pattern
end

#valueObject (readonly)

untyped

the left-hand expression



2838
2839
2840
# File 'lib/syntax_tree/node.rb', line 2838

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



2858
2859
2860
# File 'lib/syntax_tree/node.rb', line 2858

def accept(visitor)
  visitor.visit_rassign(self)
end

#child_nodesObject Also known as: deconstruct



2862
2863
2864
# File 'lib/syntax_tree/node.rb', line 2862

def child_nodes
  [value, operator, pattern]
end

#deconstruct_keys(_keys) ⇒ Object



2868
2869
2870
2871
2872
2873
2874
2875
2876
# File 'lib/syntax_tree/node.rb', line 2868

def deconstruct_keys(_keys)
  {
    value: value,
    operator: operator,
    pattern: pattern,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
# File 'lib/syntax_tree/node.rb', line 2878

def format(q)
  q.group do
    q.format(value)
    q.text(" ")
    q.format(operator)

    case pattern
    when AryPtn, FndPtn, HshPtn
      q.text(" ")
      q.format(pattern)
    else
      q.group do
        q.indent do
          q.breakable_space
          q.format(pattern)
        end
      end
    end
  end
end