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.



4204
4205
4206
4207
4208
# File 'lib/syntax_tree/node.rb', line 4204

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4202
4203
4204
# File 'lib/syntax_tree/node.rb', line 4202

def comments
  @comments
end

#valueObject (readonly)

Node

the value being sent to the keyword



4199
4200
4201
# File 'lib/syntax_tree/node.rb', line 4199

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



4210
4211
4212
# File 'lib/syntax_tree/node.rb', line 4210

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

#child_nodesObject Also known as: deconstruct



4214
4215
4216
# File 'lib/syntax_tree/node.rb', line 4214

def child_nodes
  [value]
end

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



4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
# File 'lib/syntax_tree/node.rb', line 4218

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



4231
4232
4233
# File 'lib/syntax_tree/node.rb', line 4231

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

#format(q) ⇒ Object



4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
# File 'lib/syntax_tree/node.rb', line 4235

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