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

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of Def.



2992
2993
2994
2995
2996
2997
2998
# File 'lib/syntax_tree/node.rb', line 2992

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



2987
2988
2989
# File 'lib/syntax_tree/node.rb', line 2987

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2990
2991
2992
# File 'lib/syntax_tree/node.rb', line 2990

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



2981
2982
2983
# File 'lib/syntax_tree/node.rb', line 2981

def name
  @name
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



2984
2985
2986
# File 'lib/syntax_tree/node.rb', line 2984

def params
  @params
end

Instance Method Details

#accept(visitor) ⇒ Object



3000
3001
3002
# File 'lib/syntax_tree/node.rb', line 3000

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

#child_nodesObject Also known as: deconstruct



3004
3005
3006
# File 'lib/syntax_tree/node.rb', line 3004

def child_nodes
  [name, params, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



3010
3011
3012
3013
3014
3015
3016
3017
3018
# File 'lib/syntax_tree/node.rb', line 3010

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

#format(q) ⇒ Object



3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
# File 'lib/syntax_tree/node.rb', line 3020

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