Class: OffenseToCorrector::AtomNode

Inherits:
Object
  • Object
show all
Defined in:
lib/offense_to_corrector/atom_node.rb

Overview

The annoying thing is that ‘RuboCop::AST::Node` ’s children / descendant methods don’t capture all the relevant data, so we have to cheat a bit by wrapping atoms (String, Symbol, Int, etc) in a class to get around that.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, range:, parent:) ⇒ AtomNode

Returns a new instance of AtomNode.



8
9
10
11
12
# File 'lib/offense_to_corrector/atom_node.rb', line 8

def initialize(source:, range:, parent:)
  @source = source
  @range = range
  @parent = parent
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/offense_to_corrector/atom_node.rb', line 6

def parent
  @parent
end

#rangeObject (readonly)

Returns the value of attribute range.



6
7
8
# File 'lib/offense_to_corrector/atom_node.rb', line 6

def range
  @range
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/offense_to_corrector/atom_node.rb', line 6

def source
  @source
end

Instance Method Details

#deconstruct_keys(keys) ⇒ Object



32
33
34
# File 'lib/offense_to_corrector/atom_node.rb', line 32

def deconstruct_keys(keys)
  { source:, range:, parent: }
end

#to_sObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/offense_to_corrector/atom_node.rb', line 14

def to_s
  relevant_children = @parent.children.map do |child|
    next "..." unless child.to_s == self.source.to_s # Wildcard

    case child
    when String then %("#{child}") # Literal string
    when Symbol then ":#{child}"   # Literal symbol
    when nil    then "nil?"
    else child
    end
  end

  # The trick here is that these aren't nodes, but we do care about
  # what the "parent" is that contains it to get something we can
  # work with. All other children are replaced with wildcards.
  "(#{self.parent.type} #{relevant_children.join(' ')})"
end