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.



3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
# File 'lib/syntax_tree/node.rb', line 3481

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



3479
3480
3481
# File 'lib/syntax_tree/node.rb', line 3479

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



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

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



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

def operator
  @operator
end

#parenObject (readonly)

nil | Params | Paren

the parameter declaration for the method



3473
3474
3475
# File 'lib/syntax_tree/node.rb', line 3473

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



3476
3477
3478
# File 'lib/syntax_tree/node.rb', line 3476

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



3464
3465
3466
# File 'lib/syntax_tree/node.rb', line 3464

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



3499
3500
3501
# File 'lib/syntax_tree/node.rb', line 3499

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

#child_nodesObject Also known as: deconstruct



3503
3504
3505
# File 'lib/syntax_tree/node.rb', line 3503

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

#deconstruct_keys(_keys) ⇒ Object



3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
# File 'lib/syntax_tree/node.rb', line 3509

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

#format(q) ⇒ Object



3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
# File 'lib/syntax_tree/node.rb', line 3521

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