Class: SyntaxTree::Def

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

Overview

Def represents defining a regular method on the current self object.

def method(param) result end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Def.



4472
4473
4474
4475
4476
4477
4478
# File 'lib/syntax_tree.rb', line 4472

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



4464
4465
4466
# File 'lib/syntax_tree.rb', line 4464

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4470
4471
4472
# File 'lib/syntax_tree.rb', line 4470

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4467
4468
4469
# File 'lib/syntax_tree.rb', line 4467

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4458
4459
4460
# File 'lib/syntax_tree.rb', line 4458

def name
  @name
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



4461
4462
4463
# File 'lib/syntax_tree.rb', line 4461

def params
  @params
end

Instance Method Details

#child_nodesObject



4480
4481
4482
# File 'lib/syntax_tree.rb', line 4480

def child_nodes
  [name, params, bodystmt]
end

#format(q) ⇒ Object



4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
# File 'lib/syntax_tree.rb', line 4484

def format(q)
  q.group do
    q.group do
      q.text("def ")
      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

#pretty_print(q) ⇒ Object



4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
# File 'lib/syntax_tree.rb', line 4504

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("def")

    q.breakable
    q.pp(name)

    q.breakable
    q.pp(params)

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
# File 'lib/syntax_tree.rb', line 4521

def to_json(*opts)
  {
    type: :def,
    name: name,
    params: params,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end