Class: SyntaxTree::DefEndless

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

Overview

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

def method = result

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DefEndless.



3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
# File 'lib/syntax_tree/node.rb', line 3804

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



3802
3803
3804
# File 'lib/syntax_tree/node.rb', line 3802

def comments
  @comments
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3793
3794
3795
# File 'lib/syntax_tree/node.rb', line 3793

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



3790
3791
3792
# File 'lib/syntax_tree/node.rb', line 3790

def operator
  @operator
end

#parenObject (readonly)

nil | Params | Paren

the parameter declaration for the method



3796
3797
3798
# File 'lib/syntax_tree/node.rb', line 3796

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



3799
3800
3801
# File 'lib/syntax_tree/node.rb', line 3799

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



3787
3788
3789
# File 'lib/syntax_tree/node.rb', line 3787

def target
  @target
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



3822
3823
3824
# File 'lib/syntax_tree/node.rb', line 3822

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

#deconstruct_keys(keys) ⇒ Object



3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
# File 'lib/syntax_tree/node.rb', line 3828

def deconstruct_keys(keys)
  {
    target: target,
    operator: operator,
    name: name,
    paren: paren,
    statement: statement,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
# File 'lib/syntax_tree/node.rb', line 3840

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



3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
# File 'lib/syntax_tree/node.rb', line 3867

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



3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
# File 'lib/syntax_tree/node.rb', line 3894

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