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



4169
4170
4171
4172
4173
# File 'lib/syntax_tree/node.rb', line 4169

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4167
4168
4169
# File 'lib/syntax_tree/node.rb', line 4167

def comments
  @comments
end

#valueObject (readonly)

untyped

the value being sent to the keyword



4164
4165
4166
# File 'lib/syntax_tree/node.rb', line 4164

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



4212
4213
4214
# File 'lib/syntax_tree/node.rb', line 4212

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

#accept(visitor) ⇒ Object



4175
4176
4177
# File 'lib/syntax_tree/node.rb', line 4175

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [value]
end

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



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

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



4196
4197
4198
# File 'lib/syntax_tree/node.rb', line 4196

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

#format(q) ⇒ Object



4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
# File 'lib/syntax_tree/node.rb', line 4200

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