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



3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
# File 'lib/syntax_tree/node.rb', line 3613

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



3611
3612
3613
# File 'lib/syntax_tree/node.rb', line 3611

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3602
3603
3604
# File 'lib/syntax_tree/node.rb', line 3602

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



3599
3600
3601
# File 'lib/syntax_tree/node.rb', line 3599

def operator
  @operator
end

#parenObject (readonly)

nil | Params | Paren

the parameter declaration for the method



3605
3606
3607
# File 'lib/syntax_tree/node.rb', line 3605

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



3608
3609
3610
# File 'lib/syntax_tree/node.rb', line 3608

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



3596
3597
3598
# File 'lib/syntax_tree/node.rb', line 3596

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



3631
3632
3633
# File 'lib/syntax_tree/node.rb', line 3631

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

#child_nodesObject Also known as: deconstruct



3635
3636
3637
# File 'lib/syntax_tree/node.rb', line 3635

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

#deconstruct_keys(_keys) ⇒ Object



3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
# File 'lib/syntax_tree/node.rb', line 3641

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

#format(q) ⇒ Object



3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
# File 'lib/syntax_tree/node.rb', line 3653

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_space
        q.format(statement)
      end
    end
  end
end