Class: SyntaxTree::DefEndless

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

Overview

DefEndless represents defining a single-line method since Ruby 3.0+.

def method = result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, operator:, name:, paren:, statement:, location:, comments: []) ⇒ DefEndless

Returns a new instance of DefEndless.



4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
# File 'lib/syntax_tree.rb', line 4434

def initialize(
  target:,
  operator:,
  name:,
  paren:,
  statement:,
  location:,
  comments: []
)
  @target = target
  @operator = operator
  @name = name
  @paren = paren
  @statement = statement
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4432
4433
4434
# File 'lib/syntax_tree.rb', line 4432

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4429
4430
4431
# File 'lib/syntax_tree.rb', line 4429

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4420
4421
4422
# File 'lib/syntax_tree.rb', line 4420

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



4417
4418
4419
# File 'lib/syntax_tree.rb', line 4417

def operator
  @operator
end

#parenObject (readonly)

nil | Paren

the parameter declaration for the method



4423
4424
4425
# File 'lib/syntax_tree.rb', line 4423

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



4426
4427
4428
# File 'lib/syntax_tree.rb', line 4426

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



4414
4415
4416
# File 'lib/syntax_tree.rb', line 4414

def target
  @target
end

Instance Method Details

#child_nodesObject



4452
4453
4454
# File 'lib/syntax_tree.rb', line 4452

def child_nodes
  [target, operator, name, paren, statement]
end

#format(q) ⇒ Object



4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
# File 'lib/syntax_tree.rb', line 4456

def format(q)
  q.group do
    q.text("def ")

    if target
      q.format(target)
      q.format(CallOperatorFormatter.new(operator), stackable: false)
    end

    q.format(name)
    q.format(paren) if paren && !paren.contents.empty?

    q.text(" =")
    q.group do
      q.indent do
        q.breakable
        q.format(statement)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



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

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

    if target
      q.breakable
      q.pp(target)

      q.breakable
      q.pp(operator)
    end

    q.breakable
    q.pp(name)

    if paren
      q.breakable
      q.pp(paren)
    end

    q.breakable
    q.pp(statement)

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

#to_json(*opts) ⇒ Object



4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
# File 'lib/syntax_tree.rb', line 4505

def to_json(*opts)
  {
    type: :def_endless,
    name: name,
    paren: paren,
    stmt: statement,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end