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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of RAssign.



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

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



3228
3229
3230
# File 'lib/syntax_tree/node.rb', line 3228

def comments
  @comments
end

#operatorObject (readonly)

Kw | Op

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

either => or in



3222
3223
3224
# File 'lib/syntax_tree/node.rb', line 3222

def operator
  @operator
end

#patternObject (readonly)

Node

the pattern on the right-hand side of the expression



3225
3226
3227
# File 'lib/syntax_tree/node.rb', line 3225

def pattern
  @pattern
end

#valueObject (readonly)

Node

the left-hand expression



3218
3219
3220
# File 'lib/syntax_tree/node.rb', line 3218

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



3292
3293
3294
3295
# File 'lib/syntax_tree/node.rb', line 3292

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

#accept(visitor) ⇒ Object



3238
3239
3240
# File 'lib/syntax_tree/node.rb', line 3238

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

#child_nodesObject Also known as: deconstruct



3242
3243
3244
# File 'lib/syntax_tree/node.rb', line 3242

def child_nodes
  [value, operator, pattern]
end

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



3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
# File 'lib/syntax_tree/node.rb', line 3246

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



3261
3262
3263
3264
3265
3266
3267
3268
3269
# File 'lib/syntax_tree/node.rb', line 3261

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

#format(q) ⇒ Object



3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
# File 'lib/syntax_tree/node.rb', line 3271

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