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.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#valueObject (readonly)

untyped

the value being sent to the keyword



4187
4188
4189
# File 'lib/syntax_tree/node.rb', line 4187

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



4235
4236
4237
# File 'lib/syntax_tree/node.rb', line 4235

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [value]
end

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



4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
# File 'lib/syntax_tree/node.rb', line 4206

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



4219
4220
4221
# File 'lib/syntax_tree/node.rb', line 4219

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

#format(q) ⇒ Object



4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
# File 'lib/syntax_tree/node.rb', line 4223

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