123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::AST::NodePattern::Compiler::Debug::Colorizer::Result Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Struct
Instance Chain:
self, Struct
Inherits: Struct
  • Object
Defined in: lib/rubocop/ast/node_pattern/compiler/debug.rb

Overview

Result of a NodePattern run against a particular AST Consider constructor is private

Instance Attribute Summary

  • #colorizer rw Internal use only

    Result of a NodePattern run against a particular AST Consider constructor is private.

  • #returned rw Internal use only

    Result of a NodePattern run against a particular AST Consider constructor is private.

  • #ruby_ast rw Internal use only

    Result of a NodePattern run against a particular AST Consider constructor is private.

  • #trace rw Internal use only

    Result of a NodePattern run against a particular AST Consider constructor is private.

Instance Method Summary

Instance Attribute Details

#colorizer (rw)

Result of a NodePattern run against a particular AST Consider constructor is private

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 48

Result = Struct.new(:colorizer, :trace, :returned, :ruby_ast)

#returned (rw)

Result of a NodePattern run against a particular AST Consider constructor is private

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 48

Result = Struct.new(:colorizer, :trace, :returned, :ruby_ast)

#ruby_ast (rw)

Result of a NodePattern run against a particular AST Consider constructor is private

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 48

Result = Struct.new(:colorizer, :trace, :returned, :ruby_ast)

#trace (rw)

Result of a NodePattern run against a particular AST Consider constructor is private

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 48

Result = Struct.new(:colorizer, :trace, :returned, :ruby_ast)

Instance Method Details

#ast (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 89

def ast
  colorizer.node_pattern.ast
end

#color_map(color_scheme = COLOR_SCHEME) ⇒ Hash

Returns:

  • (Hash)

    a map for ⇒ color

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 58

def color_map(color_scheme = COLOR_SCHEME)
  @color_map ||=
    match_map
    .transform_values { |matched| color_scheme.fetch(matched) }
    .map { |node, color| color_map_for(node, color) }
    .inject(:merge)
    .tap { |h| h.default = color_scheme.fetch(:not_visitable) }
end

#color_map_for(node, color) (private)

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 83

def color_map_for(node, color)
  return {} unless (range = node.loc&.expression)

  range.to_a.to_h { |char| [char, color] }
end

#colorize(color_scheme = COLOR_SCHEME) ⇒ String

Returns:

  • (String)

    a Rainbow colorized version of ruby

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 50

def colorize(color_scheme = COLOR_SCHEME)
  map = color_map(color_scheme)
  ast.source_range.source_buffer.source.chars.map.with_index do |char, i|
    Rainbow(char).color(map[i])
  end.join
end

#match_mapHash

Returns:

  • (Hash)

    a map for ⇒ matched?, depth-first

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 68

def match_map
  @match_map ||=
    ast
    .each_node
    .to_h { |node| [node, matched?(node)] }
end

#matched?(node) ⇒ Boolean

Returns:

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern/compiler/debug.rb', line 76

def matched?(node)
  id = colorizer.compiler.node_ids.fetch(node) { return :not_visitable }
  trace.matched?(id)
end