Class: SyntaxTree::Defined

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

Overview

Defined represents the use of the defined? operator. It can be used with and without parentheses.

defined?(variable)

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(value:, location:) ⇒ Defined

Returns a new instance of Defined.



4256
4257
4258
4259
4260
# File 'lib/syntax_tree/node.rb', line 4256

def initialize(value:, location:)
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4254
4255
4256
# File 'lib/syntax_tree/node.rb', line 4254

def comments
  @comments
end

#valueObject (readonly)

Node

the value being sent to the keyword



4251
4252
4253
# File 'lib/syntax_tree/node.rb', line 4251

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



4299
4300
4301
# File 'lib/syntax_tree/node.rb', line 4299

def ===(other)
  other.is_a?(Defined) && value === other.value
end

#accept(visitor) ⇒ Object



4262
4263
4264
# File 'lib/syntax_tree/node.rb', line 4262

def accept(visitor)
  visitor.visit_defined(self)
end

#child_nodesObject Also known as: deconstruct



4266
4267
4268
# File 'lib/syntax_tree/node.rb', line 4266

def child_nodes
  [value]
end

#copy(value: nil, location: nil) ⇒ Object



4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
# File 'lib/syntax_tree/node.rb', line 4270

def copy(value: nil, location: nil)
  node =
    Defined.new(
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



4283
4284
4285
# File 'lib/syntax_tree/node.rb', line 4283

def deconstruct_keys(_keys)
  { value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
# File 'lib/syntax_tree/node.rb', line 4287

def format(q)
  q.text("defined?(")
  q.group do
    q.indent do
      q.breakable_empty
      q.format(value)
    end
    q.breakable_empty
  end
  q.text(")")
end