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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Def.



3831
3832
3833
3834
3835
3836
3837
# File 'lib/syntax_tree/node.rb', line 3831

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



3823
3824
3825
# File 'lib/syntax_tree/node.rb', line 3823

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3829
3830
3831
# File 'lib/syntax_tree/node.rb', line 3829

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3826
3827
3828
# File 'lib/syntax_tree/node.rb', line 3826

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3817
3818
3819
# File 'lib/syntax_tree/node.rb', line 3817

def name
  @name
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



3820
3821
3822
# File 'lib/syntax_tree/node.rb', line 3820

def params
  @params
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



3839
3840
3841
# File 'lib/syntax_tree/node.rb', line 3839

def child_nodes
  [name, params, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



3845
3846
3847
3848
3849
3850
3851
3852
3853
# File 'lib/syntax_tree/node.rb', line 3845

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

#format(q) ⇒ Object



3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
# File 'lib/syntax_tree/node.rb', line 3855

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



3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
# File 'lib/syntax_tree/node.rb', line 3875

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



3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
# File 'lib/syntax_tree/node.rb', line 3892

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