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.



4310
4311
4312
4313
4314
4315
4316
# File 'lib/syntax_tree.rb', line 4310

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



4302
4303
4304
# File 'lib/syntax_tree.rb', line 4302

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4308
4309
4310
# File 'lib/syntax_tree.rb', line 4308

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4305
4306
4307
# File 'lib/syntax_tree.rb', line 4305

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4296
4297
4298
# File 'lib/syntax_tree.rb', line 4296

def name
  @name
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



4299
4300
4301
# File 'lib/syntax_tree.rb', line 4299

def params
  @params
end

Instance Method Details

#child_nodesObject



4318
4319
4320
# File 'lib/syntax_tree.rb', line 4318

def child_nodes
  [name, params, bodystmt]
end

#format(q) ⇒ Object



4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
# File 'lib/syntax_tree.rb', line 4322

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



4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
# File 'lib/syntax_tree.rb', line 4342

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



4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
# File 'lib/syntax_tree.rb', line 4359

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