Class: SyntaxTree::Defs

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

Overview

Defs represents defining a singleton method on an object.

def object.method(param) result end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, operator:, name:, params:, bodystmt:, location:, comments: []) ⇒ Defs

Returns a new instance of Defs.



4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
# File 'lib/syntax_tree.rb', line 4345

def initialize(
  target:,
  operator:,
  name:,
  params:,
  bodystmt:,
  location:,
  comments: []
)
  @target = target
  @operator = operator
  @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



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

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



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

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4331
4332
4333
# File 'lib/syntax_tree.rb', line 4331

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



4328
4329
4330
# File 'lib/syntax_tree.rb', line 4328

def operator
  @operator
end

#paramsObject (readonly)

Params | Paren

the parameter declaration for the method



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

def params
  @params
end

#targetObject (readonly)

untyped

the target where the method is being defined



4325
4326
4327
# File 'lib/syntax_tree.rb', line 4325

def target
  @target
end

Instance Method Details

#child_nodesObject



4363
4364
4365
# File 'lib/syntax_tree.rb', line 4363

def child_nodes
  [target, operator, name, params, bodystmt]
end

#format(q) ⇒ Object



4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
# File 'lib/syntax_tree.rb', line 4367

def format(q)
  q.group do
    q.group do
      q.text("def ")
      q.format(target)
      q.format(CallOperatorFormatter.new(operator))
      q.format(name)

      if params.is_a?(Params) && !params.empty?
        q.text("(")
        q.format(params)
        q.text(")")
      else
        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

#pretty_print(q) ⇒ Object



4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
# File 'lib/syntax_tree.rb', line 4396

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

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(operator)

    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



4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
# File 'lib/syntax_tree.rb', line 4419

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