Class: RuboCop::Cop::Performance::Sample::ShuffleAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/performance/sample.rb

Overview

An internal class for representing a shuffle + method node analyzer.

Instance Method Summary collapse

Constructor Details

#initialize(shuffle_node) ⇒ ShuffleAnalyzer

Returns a new instance of ShuffleAnalyzer.



43
44
45
46
# File 'lib/rubocop/cop/performance/sample.rb', line 43

def initialize(shuffle_node)
  @shuffle_node = shuffle_node
  @method_node  = shuffle_node.parent
end

Instance Method Details

#autocorrectObject



48
49
50
# File 'lib/rubocop/cop/performance/sample.rb', line 48

def autocorrect
  ->(corrector) { corrector.replace(source_range, correct) }
end

#messageObject



52
53
54
# File 'lib/rubocop/cop/performance/sample.rb', line 52

def message
  format(MSG, correct: correct, incorrect: source_range.source)
end

#offensive?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/rubocop/cop/performance/sample.rb', line 56

def offensive?
  shuffle_node.to_a[1] == :shuffle && corrigible?
end

#source_rangeObject



60
61
62
63
64
# File 'lib/rubocop/cop/performance/sample.rb', line 60

def source_range
  Parser::Source::Range.new(shuffle_node.source_range.source_buffer,
                            shuffle_node.loc.selector.begin_pos,
                            method_node.source_range.end_pos)
end