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.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#valueObject (readonly)

Node

the value being sent to the keyword



4248
4249
4250
# File 'lib/syntax_tree/node.rb', line 4248

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



4296
4297
4298
# File 'lib/syntax_tree/node.rb', line 4296

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

#accept(visitor) ⇒ Object



4259
4260
4261
# File 'lib/syntax_tree/node.rb', line 4259

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [value]
end

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



4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
# File 'lib/syntax_tree/node.rb', line 4267

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



4280
4281
4282
# File 'lib/syntax_tree/node.rb', line 4280

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

#format(q) ⇒ Object



4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
# File 'lib/syntax_tree/node.rb', line 4284

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