Class: SyntaxTree::Def

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



4048
4049
4050
4051
4052
4053
4054
# File 'lib/syntax_tree.rb', line 4048

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



4040
4041
4042
# File 'lib/syntax_tree.rb', line 4040

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4046
4047
4048
# File 'lib/syntax_tree.rb', line 4046

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4043
4044
4045
# File 'lib/syntax_tree.rb', line 4043

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4034
4035
4036
# File 'lib/syntax_tree.rb', line 4034

def name
  @name
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



4037
4038
4039
# File 'lib/syntax_tree.rb', line 4037

def params
  @params
end

Instance Method Details

#child_nodesObject



4056
4057
4058
# File 'lib/syntax_tree.rb', line 4056

def child_nodes
  [name, params, bodystmt]
end

#format(q) ⇒ Object



4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
# File 'lib/syntax_tree.rb', line 4060

def format(q)
  q.group do
    q.group do
      q.text("def ")
      q.format(name)

      if params.is_a?(Params) && !params.empty?
        q.text("(")
        q.format(params)
        q.text(")")
      else
        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

#pretty_print(q) ⇒ Object



4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
# File 'lib/syntax_tree.rb', line 4087

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



4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
# File 'lib/syntax_tree.rb', line 4104

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