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.



4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
# File 'lib/syntax_tree.rb', line 4559

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



4557
4558
4559
# File 'lib/syntax_tree.rb', line 4557

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4554
4555
4556
# File 'lib/syntax_tree.rb', line 4554

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4545
4546
4547
# File 'lib/syntax_tree.rb', line 4545

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



4542
4543
4544
# File 'lib/syntax_tree.rb', line 4542

def operator
  @operator
end

#parenObject (readonly)

nil | Params | Paren

the parameter declaration for the method



4548
4549
4550
# File 'lib/syntax_tree.rb', line 4548

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



4551
4552
4553
# File 'lib/syntax_tree.rb', line 4551

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



4539
4540
4541
# File 'lib/syntax_tree.rb', line 4539

def target
  @target
end

Instance Method Details

#child_nodesObject



4577
4578
4579
# File 'lib/syntax_tree.rb', line 4577

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

#format(q) ⇒ Object



4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
# File 'lib/syntax_tree.rb', line 4581

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)

    if paren
      params = paren
      params = params.contents if params.is_a?(Paren)
      q.format(paren) unless params.empty?
    end

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

#pretty_print(q) ⇒ Object



4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
# File 'lib/syntax_tree.rb', line 4608

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



4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
# File 'lib/syntax_tree.rb', line 4635

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