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

#pretty_print, #to_json

Constructor Details

#initialize(target:, operator:, name:, params:, bodystmt:, location:, comments: []) ⇒ Defs

Returns a new instance of Defs.



3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
# File 'lib/syntax_tree/node.rb', line 3549

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



3544
3545
3546
# File 'lib/syntax_tree/node.rb', line 3544

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3547
3548
3549
# File 'lib/syntax_tree/node.rb', line 3547

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3538
3539
3540
# File 'lib/syntax_tree/node.rb', line 3538

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



3535
3536
3537
# File 'lib/syntax_tree/node.rb', line 3535

def operator
  @operator
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



3541
3542
3543
# File 'lib/syntax_tree/node.rb', line 3541

def params
  @params
end

#targetObject (readonly)

untyped

the target where the method is being defined



3532
3533
3534
# File 'lib/syntax_tree/node.rb', line 3532

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



3567
3568
3569
# File 'lib/syntax_tree/node.rb', line 3567

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

#child_nodesObject Also known as: deconstruct



3571
3572
3573
# File 'lib/syntax_tree/node.rb', line 3571

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

#deconstruct_keys(keys) ⇒ Object



3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
# File 'lib/syntax_tree/node.rb', line 3577

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

#format(q) ⇒ Object



3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
# File 'lib/syntax_tree/node.rb', line 3589

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