Class: OffenseToCorrector::AtomNode
- Inherits:
-
Object
- Object
- OffenseToCorrector::AtomNode
- 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
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#range ⇒ Object
readonly
Returns the value of attribute range.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(source:, range:, parent:) ⇒ AtomNode
constructor
A new instance of AtomNode.
- #to_s ⇒ Object
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
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/offense_to_corrector/atom_node.rb', line 6 def parent @parent end |
#range ⇒ Object (readonly)
Returns the value of attribute range.
6 7 8 |
# File 'lib/offense_to_corrector/atom_node.rb', line 6 def range @range end |
#source ⇒ Object (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_s ⇒ Object
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 |