Sha256: 8b8c3653854b731781405102ecfd5e45b70fb26f1f30d9f7365635253036192d

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module OffenseToCorrector
  # 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.
  class AtomNode
    attr_reader :source, :range, :parent

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

    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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
offense_to_corrector-0.0.2 lib/offense_to_corrector/atom_node.rb