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, #pretty_print, #to_json

Constructor Details

#initialize(value:, location:) ⇒ Defined

Returns a new instance of Defined.



4183
4184
4185
4186
4187
# File 'lib/syntax_tree/node.rb', line 4183

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4181
4182
4183
# File 'lib/syntax_tree/node.rb', line 4181

def comments
  @comments
end

#valueObject (readonly)

untyped

the value being sent to the keyword



4178
4179
4180
# File 'lib/syntax_tree/node.rb', line 4178

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



4226
4227
4228
# File 'lib/syntax_tree/node.rb', line 4226

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

#accept(visitor) ⇒ Object



4189
4190
4191
# File 'lib/syntax_tree/node.rb', line 4189

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

#child_nodesObject Also known as: deconstruct



4193
4194
4195
# File 'lib/syntax_tree/node.rb', line 4193

def child_nodes
  [value]
end

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



4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
# File 'lib/syntax_tree/node.rb', line 4197

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



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

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

#format(q) ⇒ Object



4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
# File 'lib/syntax_tree/node.rb', line 4214

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