Class: SyntaxTree::DefEndless

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

Overview

DefEndless represents defining a single-line method since Ruby 3.0+.

def method = result

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(target:, operator:, name:, paren:, statement:, location:, comments: []) ⇒ DefEndless

Returns a new instance of DefEndless.



3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
# File 'lib/syntax_tree/node.rb', line 3448

def initialize(
  target:,
  operator:,
  name:,
  paren:,
  statement:,
  location:,
  comments: []
)
  @target = target
  @operator = operator
  @name = name
  @paren = paren
  @statement = statement
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3446
3447
3448
# File 'lib/syntax_tree/node.rb', line 3446

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3437
3438
3439
# File 'lib/syntax_tree/node.rb', line 3437

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



3434
3435
3436
# File 'lib/syntax_tree/node.rb', line 3434

def operator
  @operator
end

#parenObject (readonly)

nil | Params | Paren

the parameter declaration for the method



3440
3441
3442
# File 'lib/syntax_tree/node.rb', line 3440

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



3443
3444
3445
# File 'lib/syntax_tree/node.rb', line 3443

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



3431
3432
3433
# File 'lib/syntax_tree/node.rb', line 3431

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



3466
3467
3468
# File 'lib/syntax_tree/node.rb', line 3466

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

#child_nodesObject Also known as: deconstruct



3470
3471
3472
# File 'lib/syntax_tree/node.rb', line 3470

def child_nodes
  [target, operator, name, paren, statement]
end

#deconstruct_keys(_keys) ⇒ Object



3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
# File 'lib/syntax_tree/node.rb', line 3476

def deconstruct_keys(_keys)
  {
    target: target,
    operator: operator,
    name: name,
    paren: paren,
    statement: statement,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
# File 'lib/syntax_tree/node.rb', line 3488

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

    if target
      q.format(target)
      q.format(CallOperatorFormatter.new(operator), stackable: false)
    end

    q.format(name)

    if paren
      params = paren
      params = params.contents if params.is_a?(Paren)
      q.format(paren) unless params.empty?
    end

    q.text(" =")
    q.group do
      q.indent do
        q.breakable
        q.format(statement)
      end
    end
  end
end