Class: SyntaxTree::Defs

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Defs represents defining a singleton method on an object.

def object.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(target:, operator:, name:, params:, bodystmt:, location:, comments: []) ⇒ Defs

Returns a new instance of Defs.



3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
# File 'lib/syntax_tree/node.rb', line 3198

def initialize(
  target:,
  operator:,
  name:,
  params:,
  bodystmt:,
  location:,
  comments: []
)
  @target = target
  @operator = operator
  @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



3193
3194
3195
# File 'lib/syntax_tree/node.rb', line 3193

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3196
3197
3198
# File 'lib/syntax_tree/node.rb', line 3196

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3187
3188
3189
# File 'lib/syntax_tree/node.rb', line 3187

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



3184
3185
3186
# File 'lib/syntax_tree/node.rb', line 3184

def operator
  @operator
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



3190
3191
3192
# File 'lib/syntax_tree/node.rb', line 3190

def params
  @params
end

#targetObject (readonly)

untyped

the target where the method is being defined



3181
3182
3183
# File 'lib/syntax_tree/node.rb', line 3181

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



3216
3217
3218
# File 'lib/syntax_tree/node.rb', line 3216

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

#child_nodesObject Also known as: deconstruct



3220
3221
3222
# File 'lib/syntax_tree/node.rb', line 3220

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

#deconstruct_keys(keys) ⇒ Object



3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
# File 'lib/syntax_tree/node.rb', line 3226

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

#format(q) ⇒ Object



3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
# File 'lib/syntax_tree/node.rb', line 3238

def format(q)
  q.group do
    q.group do
      q.text("def ")
      q.format(target)
      q.format(CallOperatorFormatter.new(operator), stackable: false)
      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