Class: SyntaxTree::Def

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

Overview

Def represents defining a regular method on the current self object.

def method(param) result end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

#initialize(name:, params:, bodystmt:, location:, comments: []) ⇒ Def

Returns a new instance of Def.



3708
3709
3710
3711
3712
3713
3714
# File 'lib/syntax_tree/node.rb', line 3708

def initialize(name:, params:, bodystmt:, location:, comments: [])
  @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



3703
3704
3705
# File 'lib/syntax_tree/node.rb', line 3703

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3706
3707
3708
# File 'lib/syntax_tree/node.rb', line 3706

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3697
3698
3699
# File 'lib/syntax_tree/node.rb', line 3697

def name
  @name
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



3700
3701
3702
# File 'lib/syntax_tree/node.rb', line 3700

def params
  @params
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



3716
3717
3718
# File 'lib/syntax_tree/node.rb', line 3716

def child_nodes
  [name, params, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



3722
3723
3724
3725
3726
3727
3728
3729
3730
# File 'lib/syntax_tree/node.rb', line 3722

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

#format(q) ⇒ Object



3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
# File 'lib/syntax_tree/node.rb', line 3732

def format(q)
  q.group do
    q.group do
      q.text("def ")
      q.format(name)
      q.format(params) if !params.is_a?(Params) || !params.empty?
    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

#pretty_print(q) ⇒ Object



3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
# File 'lib/syntax_tree/node.rb', line 3752

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("def")

    q.breakable
    q.pp(name)

    q.breakable
    q.pp(params)

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
# File 'lib/syntax_tree/node.rb', line 3769

def to_json(*opts)
  {
    type: :def,
    name: name,
    params: params,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end