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:) ⇒ RAssign

Returns a new instance of RAssign.



3171
3172
3173
3174
3175
3176
3177
# File 'lib/syntax_tree/node.rb', line 3171

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3169
3170
3171
# File 'lib/syntax_tree/node.rb', line 3169

def comments
  @comments
end

#operatorObject (readonly)

Kw | Op

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

either => or in



3163
3164
3165
# File 'lib/syntax_tree/node.rb', line 3163

def operator
  @operator
end

#patternObject (readonly)

untyped

the pattern on the right-hand side of the expression



3166
3167
3168
# File 'lib/syntax_tree/node.rb', line 3166

def pattern
  @pattern
end

#valueObject (readonly)

untyped

the left-hand expression



3159
3160
3161
# File 'lib/syntax_tree/node.rb', line 3159

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



3233
3234
3235
3236
# File 'lib/syntax_tree/node.rb', line 3233

def ===(other)
  other.is_a?(RAssign) && value === other.value &&
    operator === other.operator && pattern === other.pattern
end

#accept(visitor) ⇒ Object



3179
3180
3181
# File 'lib/syntax_tree/node.rb', line 3179

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

#child_nodesObject Also known as: deconstruct



3183
3184
3185
# File 'lib/syntax_tree/node.rb', line 3183

def child_nodes
  [value, operator, pattern]
end

#copy(value: nil, operator: nil, pattern: nil, location: nil) ⇒ Object



3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
# File 'lib/syntax_tree/node.rb', line 3187

def copy(value: nil, operator: nil, pattern: nil, location: nil)
  node =
    RAssign.new(
      value: value || self.value,
      operator: operator || self.operator,
      pattern: pattern || self.pattern,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



3202
3203
3204
3205
3206
3207
3208
3209
3210
# File 'lib/syntax_tree/node.rb', line 3202

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

#format(q) ⇒ Object



3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
# File 'lib/syntax_tree/node.rb', line 3212

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