Class: SyntaxTree::Defs

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

Overview

Defs represents defining a singleton method on an object.

def object.method(param) result end

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:, params:, bodystmt:, location:, comments: []) ⇒ Defs

Returns a new instance of Defs.



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

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed by the method



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

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3613
3614
3615
# File 'lib/syntax_tree/node.rb', line 3613

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



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

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



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

def operator
  @operator
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



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

def params
  @params
end

#targetObject (readonly)

untyped

the target where the method is being defined



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

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



3633
3634
3635
# File 'lib/syntax_tree/node.rb', line 3633

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

#child_nodesObject Also known as: deconstruct



3637
3638
3639
# File 'lib/syntax_tree/node.rb', line 3637

def child_nodes
  [target, operator, name, params, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



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

def deconstruct_keys(_keys)
  {
    target: target,
    operator: operator,
    name: name,
    params: params,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



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 3655

def format(q)
  q.group do
    q.group do
      q.text("def ")
      q.format(target)
      q.format(CallOperatorFormatter.new(operator), stackable: false)
      q.format(name)

      if !params.is_a?(Params) || !params.empty? || params.comments.any?
        q.format(params)
      end
    end

    unless bodystmt.empty?
      q.indent do
        q.breakable(force: true)
        q.format(bodystmt)
      end
    end

    q.breakable(force: true)
    q.text("end")
  end
end