Module: RuboCop::Cop::AutocorrectLogic

Included in:
Cop
Defined in:
lib/rubocop/cop/autocorrect_logic.rb

Overview

This module encapsulates the logic for autocorrect behavior for a cop.

Instance Method Summary collapse

Instance Method Details

#autocorrect?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/rubocop/cop/autocorrect_logic.rb', line 7

def autocorrect?
  autocorrect_requested? && correctable? && autocorrect_enabled?
end

#autocorrect_enabled?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/autocorrect_logic.rb', line 32

def autocorrect_enabled?
  # allow turning off autocorrect on a cop by cop basis
  return true unless cop_config

  return false if cop_config['AutoCorrect'] == false

  return safe_autocorrect? if @options.fetch(:safe_auto_correct, false)

  true
end

#autocorrect_requested?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/rubocop/cop/autocorrect_logic.rb', line 11

def autocorrect_requested?
  @options.fetch(:auto_correct, false)
end

#correctable?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/rubocop/cop/autocorrect_logic.rb', line 15

def correctable?
  support_autocorrect? || disable_uncorrectable?
end

#disable_offense(node) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/autocorrect_logic.rb', line 43

def disable_offense(node)
  range = node.location.expression
  eol_comment = " # rubocop:todo #{cop_name}"
  needed_line_length = range.column +
                       (range.source_line + eol_comment).length
  if needed_line_length <= max_line_length
    disable_offense_at_end_of_line(range_of_first_line(range),
                                   eol_comment)
  else
    disable_offense_before_and_after((range))
  end
end

#disable_uncorrectable?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rubocop/cop/autocorrect_logic.rb', line 23

def disable_uncorrectable?
  @options[:disable_uncorrectable] == true
end

#safe_autocorrect?Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/rubocop/cop/autocorrect_logic.rb', line 27

def safe_autocorrect?
  cop_config.fetch('Safe', true) &&
    cop_config.fetch('SafeAutoCorrect', true)
end

#support_autocorrect?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rubocop/cop/autocorrect_logic.rb', line 19

def support_autocorrect?
  respond_to?(:autocorrect)
end