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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Def.



3407
3408
3409
3410
3411
3412
3413
# File 'lib/syntax_tree/node.rb', line 3407

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

Instance Attribute Details

#bodystmt ⇒ Object (readonly)

[BodyStmt] the expressions to be executed by the method



3402
3403
3404
# File 'lib/syntax_tree/node.rb', line 3402

def bodystmt
  @bodystmt
end

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



3405
3406
3407
# File 'lib/syntax_tree/node.rb', line 3405

def comments
  @comments
end

#name ⇒ Object (readonly)

[Backtick | Const | Ident | Kw | Op] the name of the method



3396
3397
3398
# File 'lib/syntax_tree/node.rb', line 3396

def name
  @name
end

#params ⇒ Object (readonly)

[Params | Paren] the parameter declaration for the method



3399
3400
3401
# File 'lib/syntax_tree/node.rb', line 3399

def params
  @params
end

Instance Method Details

#accept(visitor) ⇒ Object



3415
3416
3417
# File 'lib/syntax_tree/node.rb', line 3415

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

#child_nodes ⇒ Object Also known as: deconstruct



3419
3420
3421
# File 'lib/syntax_tree/node.rb', line 3419

def child_nodes
  [name, params, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



3425
3426
3427
3428
3429
3430
3431
3432
3433
# File 'lib/syntax_tree/node.rb', line 3425

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

#format(q) ⇒ Object



3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
# File 'lib/syntax_tree/node.rb', line 3435

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

      if !params.is_a?(Params) || !params.empty? || params.comments.any?
        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