Class: SyntaxTree::DefNode

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
def object.method(param) result end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(target:, operator:, name:, params:, bodystmt:, location:) ⇒ DefNode

Returns a new instance of DefNode.



4101
4102
4103
4104
4105
4106
4107
4108
4109
# File 'lib/syntax_tree/node.rb', line 4101

def initialize(target:, operator:, name:, params:, bodystmt:, location:)
  @target = target
  @operator = operator
  @name = name
  @params = params
  @bodystmt = bodystmt
  @location = location
  @comments = []
end

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt | Node

the expressions to be executed by the method



4096
4097
4098
# File 'lib/syntax_tree/node.rb', line 4096

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4099
4100
4101
# File 'lib/syntax_tree/node.rb', line 4099

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4090
4091
4092
# File 'lib/syntax_tree/node.rb', line 4090

def name
  @name
end

#operatorObject (readonly)

nil | Op | Period

the operator being used to declare the method



4087
4088
4089
# File 'lib/syntax_tree/node.rb', line 4087

def operator
  @operator
end

#paramsObject (readonly)

nil | Params | Paren

the parameter declaration for the method



4093
4094
4095
# File 'lib/syntax_tree/node.rb', line 4093

def params
  @params
end

#targetObject (readonly)

nil | Node

the target where the method is being defined



4084
4085
4086
# File 'lib/syntax_tree/node.rb', line 4084

def target
  @target
end

Instance Method Details

#===(other) ⇒ Object



4198
4199
4200
4201
4202
# File 'lib/syntax_tree/node.rb', line 4198

def ===(other)
  other.is_a?(DefNode) && target === other.target &&
    operator === other.operator && name === other.name &&
    params === other.params && bodystmt === other.bodystmt
end

#accept(visitor) ⇒ Object



4111
4112
4113
# File 'lib/syntax_tree/node.rb', line 4111

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

#arityObject



4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
# File 'lib/syntax_tree/node.rb', line 4211

def arity
  case params
  when Params
    params.arity
  when Paren
    params.contents.arity
  else
    0..0
  end
end

#child_nodesObject Also known as: deconstruct



4115
4116
4117
# File 'lib/syntax_tree/node.rb', line 4115

def child_nodes
  [target, operator, name, params, bodystmt]
end

#copy(target: nil, operator: nil, name: nil, params: nil, bodystmt: nil, location: nil) ⇒ Object



4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
# File 'lib/syntax_tree/node.rb', line 4119

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_keys(_keys) ⇒ Object



4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
# File 'lib/syntax_tree/node.rb', line 4143

def deconstruct_keys(_keys)
  {
    target: target,
    operator: operator,
    name: name,
    params: params,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#endless?Boolean

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.

Returns:

  • (Boolean)


4207
4208
4209
# File 'lib/syntax_tree/node.rb', line 4207

def endless?
  !bodystmt.is_a?(BodyStmt)
end

#format(q) ⇒ Object



4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
# File 'lib/syntax_tree/node.rb', line 4155

def format(q)
  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