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.



4347
4348
4349
4350
4351
4352
4353
# File 'lib/syntax_tree.rb', line 4347

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



4339
4340
4341
# File 'lib/syntax_tree.rb', line 4339

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4345
4346
4347
# File 'lib/syntax_tree.rb', line 4345

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4342
4343
4344
# File 'lib/syntax_tree.rb', line 4342

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4333
4334
4335
# File 'lib/syntax_tree.rb', line 4333

def name
  @name
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



4336
4337
4338
# File 'lib/syntax_tree.rb', line 4336

def params
  @params
end

Instance Method Details

#child_nodesObject



4355
4356
4357
# File 'lib/syntax_tree.rb', line 4355

def child_nodes
  [name, params, bodystmt]
end

#format(q) ⇒ Object



4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
# File 'lib/syntax_tree.rb', line 4359

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



4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
# File 'lib/syntax_tree.rb', line 4379

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



4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
# File 'lib/syntax_tree.rb', line 4396

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