Class: SyntaxTree::Undef

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Undef represents the use of the undef keyword.

undef method

Defined Under Namespace

Classes: UndefArgumentFormatter

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(symbols:, location:) ⇒ Undef

Returns a new instance of Undef.



11238
11239
11240
11241
11242
# File 'lib/syntax_tree/node.rb', line 11238

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11236
11237
11238
# File 'lib/syntax_tree/node.rb', line 11236

def comments
  @comments
end

#symbolsObject (readonly)

Array[ DynaSymbol | SymbolLiteral ]

the symbols to undefine



11233
11234
11235
# File 'lib/syntax_tree/node.rb', line 11233

def symbols
  @symbols
end

Instance Method Details

#===(other) ⇒ Object



11281
11282
11283
# File 'lib/syntax_tree/node.rb', line 11281

def ===(other)
  other.is_a?(Undef) && ArrayMatch.call(symbols, other.symbols)
end

#accept(visitor) ⇒ Object



11244
11245
11246
# File 'lib/syntax_tree/node.rb', line 11244

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

#child_nodesObject Also known as: deconstruct



11248
11249
11250
# File 'lib/syntax_tree/node.rb', line 11248

def child_nodes
  symbols
end

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



11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
# File 'lib/syntax_tree/node.rb', line 11252

def copy(symbols: nil, location: nil)
  node =
    Undef.new(
      symbols: symbols || self.symbols,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11265
11266
11267
# File 'lib/syntax_tree/node.rb', line 11265

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

#format(q) ⇒ Object



11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
# File 'lib/syntax_tree/node.rb', line 11269

def format(q)
  keyword = "undef "
  formatters = symbols.map { |symbol| UndefArgumentFormatter.new(symbol) }

  q.group do
    q.text(keyword)
    q.nest(keyword.length) do
      q.seplist(formatters) { |formatter| q.format(formatter) }
    end
  end
end