class SyntaxTree::DefNode

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

def method(param) result end
def object.method(param) result end

Attributes

bodystmt[R]
BodyStmt | Node

the expressions to be executed by the method

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

name[R]
Backtick | Const | Ident | Kw | Op

the name of the method

operator[R]
nil | Op | Period

the operator being used to declare the method

params[R]
nil | Params | Paren

the parameter declaration for the method

target[R]
nil | Node

the target where the method is being defined

Public Class Methods

new(target:, operator:, name:, params:, bodystmt:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 4117
def initialize(target:, operator:, name:, params:, bodystmt:, location:)
  @target = target
  @operator = operator
  @name = name
  @params = params
  @bodystmt = bodystmt
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 4217
def ===(other)
  other.is_a?(DefNode) && target === other.target &&
    operator === other.operator && name === other.name &&
    params === other.params && bodystmt === other.bodystmt
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 4127
def accept(visitor)
  visitor.visit_def(self)
end
arity() click to toggle source
# File lib/syntax_tree/node.rb, line 4230
def arity
  params = self.params

  case params
  when Params
    params.arity
  when Paren
    params.contents.arity
  else
    0..0
  end
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 4131
def child_nodes
  [target, operator, name, params, bodystmt]
end
Also aliased as: deconstruct
copy( target: nil, operator: nil, name: nil, params: nil, bodystmt: nil, location: nil ) click to toggle source
# File lib/syntax_tree/node.rb, line 4135
def copy(
  target: nil,
  operator: nil,
  name: nil,
  params: nil,
  bodystmt: nil,
  location: nil
)
  node =
    DefNode.new(
      target: target || self.target,
      operator: operator || self.operator,
      name: name || self.name,
      params: params || self.params,
      bodystmt: bodystmt || self.bodystmt,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 4159
def deconstruct_keys(_keys)
  {
    target: target,
    operator: operator,
    name: name,
    params: params,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end
endless?() click to toggle source

Returns true if the method was found in the source in the “endless” form, i.e. where the method body is defined using the ‘=` operator after the method name and parameters.

# File lib/syntax_tree/node.rb, line 4226
def endless?
  !bodystmt.is_a?(BodyStmt)
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 4171
def format(q)
  params = self.params
  bodystmt = self.bodystmt

  q.group do
    q.group do
      q.text("def")
      q.text(" ") if target || name.comments.empty?

      if target
        q.format(target)
        q.format(CallOperatorFormatter.new(operator), stackable: false)
      end

      q.format(name)

      case params
      when Paren
        q.format(params)
      when Params
        q.format(params) if !params.empty? || params.comments.any?
      end
    end

    if endless?
      q.text(" =")
      q.group do
        q.indent do
          q.breakable_space
          q.format(bodystmt)
        end
      end
    else
      unless bodystmt.empty?
        q.indent do
          q.breakable_force
          q.format(bodystmt)
        end
      end

      q.breakable_force
      q.text("end")
    end
  end
end