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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DefEndless.



3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
# File 'lib/syntax_tree/node.rb', line 3930

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



3928
3929
3930
# File 'lib/syntax_tree/node.rb', line 3928

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3925
3926
3927
# File 'lib/syntax_tree/node.rb', line 3925

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



3916
3917
3918
# File 'lib/syntax_tree/node.rb', line 3916

def name
  @name
end

#operatorObject (readonly)

Op | Period

the operator being used to declare the method



3913
3914
3915
# File 'lib/syntax_tree/node.rb', line 3913

def operator
  @operator
end

#parenObject (readonly)

nil | Params | Paren

the parameter declaration for the method



3919
3920
3921
# File 'lib/syntax_tree/node.rb', line 3919

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



3922
3923
3924
# File 'lib/syntax_tree/node.rb', line 3922

def statement
  @statement
end

#targetObject (readonly)

untyped

the target where the method is being defined



3910
3911
3912
# File 'lib/syntax_tree/node.rb', line 3910

def target
  @target
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



3948
3949
3950
# File 'lib/syntax_tree/node.rb', line 3948

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

#deconstruct_keys(keys) ⇒ Object



3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
# File 'lib/syntax_tree/node.rb', line 3954

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

#format(q) ⇒ Object



3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
# File 'lib/syntax_tree/node.rb', line 3966

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



3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
# File 'lib/syntax_tree/node.rb', line 3993

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



4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
# File 'lib/syntax_tree/node.rb', line 4020

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