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.



3373
3374
3375
3376
3377
3378
3379
# File 'lib/syntax_tree/node.rb', line 3373

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



3368
3369
3370
# File 'lib/syntax_tree/node.rb', line 3368

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3371
3372
3373
# File 'lib/syntax_tree/node.rb', line 3371

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3362
3363
3364
# File 'lib/syntax_tree/node.rb', line 3362

def name
  @name
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



3365
3366
3367
# File 'lib/syntax_tree/node.rb', line 3365

def params
  @params
end

Instance Method Details

#accept(visitor) ⇒ Object



3381
3382
3383
# File 'lib/syntax_tree/node.rb', line 3381

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

#child_nodesObject Also known as: deconstruct



3385
3386
3387
# File 'lib/syntax_tree/node.rb', line 3385

def child_nodes
  [name, params, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



3391
3392
3393
3394
3395
3396
3397
3398
3399
# File 'lib/syntax_tree/node.rb', line 3391

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

#format(q) ⇒ Object



3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
# File 'lib/syntax_tree/node.rb', line 3401

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