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(name:, paren:, statement:, location:, comments: []) ⇒ DefEndless

Returns a new instance of DefEndless.



4136
4137
4138
4139
4140
4141
4142
# File 'lib/syntax_tree.rb', line 4136

def initialize(name:, paren:, statement:, location:, comments: [])
  @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



4134
4135
4136
# File 'lib/syntax_tree.rb', line 4134

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4131
4132
4133
# File 'lib/syntax_tree.rb', line 4131

def location
  @location
end

#nameObject (readonly)

Backtick | Const | Ident | Kw | Op

the name of the method



4122
4123
4124
# File 'lib/syntax_tree.rb', line 4122

def name
  @name
end

#parenObject (readonly)

nil | Paren

the parameter declaration for the method



4125
4126
4127
# File 'lib/syntax_tree.rb', line 4125

def paren
  @paren
end

#statementObject (readonly)

untyped

the expression to be executed by the method



4128
4129
4130
# File 'lib/syntax_tree.rb', line 4128

def statement
  @statement
end

Instance Method Details

#child_nodesObject



4144
4145
4146
# File 'lib/syntax_tree.rb', line 4144

def child_nodes
  [name, paren, statement]
end

#format(q) ⇒ Object



4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
# File 'lib/syntax_tree.rb', line 4148

def format(q)
  q.group do
    q.text("def ")
    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



4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
# File 'lib/syntax_tree.rb', line 4163

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

    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



4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
# File 'lib/syntax_tree.rb', line 4182

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