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.



3445
3446
3447
3448
3449
3450
3451
# File 'lib/syntax_tree/node.rb', line 3445

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



3440
3441
3442
# File 'lib/syntax_tree/node.rb', line 3440

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3443
3444
3445
# File 'lib/syntax_tree/node.rb', line 3443

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3434
3435
3436
# File 'lib/syntax_tree/node.rb', line 3434

def name
  @name
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



3437
3438
3439
# File 'lib/syntax_tree/node.rb', line 3437

def params
  @params
end

Instance Method Details

#accept(visitor) ⇒ Object



3453
3454
3455
# File 'lib/syntax_tree/node.rb', line 3453

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

#child_nodesObject Also known as: deconstruct



3457
3458
3459
# File 'lib/syntax_tree/node.rb', line 3457

def child_nodes
  [name, params, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



3463
3464
3465
3466
3467
3468
3469
3470
3471
# File 'lib/syntax_tree/node.rb', line 3463

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

#format(q) ⇒ Object



3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
# File 'lib/syntax_tree/node.rb', line 3473

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