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.



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

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

Instance Method Details

#autocorrectObject



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

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

#messageObject



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

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

#offensive?Boolean

Returns:

  • (Boolean)


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

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

#source_rangeObject



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

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