rubocop 0.89.0 → 0.89.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  4. data/lib/rubocop/comment_config.rb +3 -5
  5. data/lib/rubocop/config.rb +1 -1
  6. data/lib/rubocop/config_loader.rb +1 -1
  7. data/lib/rubocop/config_validator.rb +1 -1
  8. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  9. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  10. data/lib/rubocop/cop/layout/class_structure.rb +3 -7
  11. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  12. data/lib/rubocop/cop/layout/extra_spacing.rb +14 -21
  13. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  14. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -1
  15. data/lib/rubocop/cop/lint/missing_super.rb +9 -9
  16. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +33 -4
  17. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +14 -17
  18. data/lib/rubocop/cop/lint/uri_regexp.rb +9 -25
  19. data/lib/rubocop/cop/migration/department_name.rb +2 -2
  20. data/lib/rubocop/cop/mixin/alignment.rb +2 -1
  21. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  22. data/lib/rubocop/cop/mixin/enforce_superclass.rb +0 -2
  23. data/lib/rubocop/cop/mixin/hash_transform_method.rb +5 -11
  24. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -3
  25. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +1 -2
  26. data/lib/rubocop/cop/mixin/percent_array.rb +2 -6
  27. data/lib/rubocop/cop/mixin/range_help.rb +18 -4
  28. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
  29. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
  30. data/lib/rubocop/cop/style/access_modifier_declarations.rb +5 -8
  31. data/lib/rubocop/cop/style/accessor_grouping.rb +20 -20
  32. data/lib/rubocop/cop/style/alias.rb +34 -33
  33. data/lib/rubocop/cop/style/and_or.rb +9 -11
  34. data/lib/rubocop/cop/style/array_join.rb +6 -8
  35. data/lib/rubocop/cop/style/ascii_comments.rb +4 -4
  36. data/lib/rubocop/cop/style/attr.rb +11 -9
  37. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +2 -5
  38. data/lib/rubocop/cop/style/bare_percent_literals.rb +10 -12
  39. data/lib/rubocop/cop/style/begin_block.rb +2 -2
  40. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +9 -12
  41. data/lib/rubocop/cop/style/block_comments.rb +14 -18
  42. data/lib/rubocop/cop/style/block_delimiters.rb +23 -23
  43. data/lib/rubocop/cop/style/case_like_if.rb +17 -14
  44. data/lib/rubocop/cop/style/class_and_module_children.rb +14 -11
  45. data/lib/rubocop/cop/style/class_check.rb +7 -9
  46. data/lib/rubocop/cop/style/class_methods.rb +7 -11
  47. data/lib/rubocop/cop/style/class_vars.rb +6 -10
  48. data/lib/rubocop/cop/style/collection_methods.rb +11 -17
  49. data/lib/rubocop/cop/style/colon_method_call.rb +6 -7
  50. data/lib/rubocop/cop/style/colon_method_definition.rb +6 -6
  51. data/lib/rubocop/cop/style/command_literal.rb +23 -24
  52. data/lib/rubocop/cop/style/comment_annotation.rb +15 -15
  53. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  54. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  55. data/lib/rubocop/cop/style/constant_visibility.rb +3 -2
  56. data/lib/rubocop/cop/style/copyright.rb +12 -12
  57. data/lib/rubocop/cop/style/date_time.rb +1 -1
  58. data/lib/rubocop/cop/style/def_with_parentheses.rb +8 -10
  59. data/lib/rubocop/cop/style/dir.rb +7 -10
  60. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +7 -9
  61. data/lib/rubocop/cop/style/documentation.rb +2 -4
  62. data/lib/rubocop/cop/style/documentation_method.rb +1 -1
  63. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +12 -15
  64. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  65. data/lib/rubocop/cop/style/each_for_simple_loop.rb +5 -8
  66. data/lib/rubocop/cop/style/each_with_object.rb +16 -17
  67. data/lib/rubocop/cop/style/empty_case_condition.rb +19 -20
  68. data/lib/rubocop/cop/style/empty_else.rb +17 -19
  69. data/lib/rubocop/cop/style/empty_literal.rb +15 -16
  70. data/lib/rubocop/cop/style/empty_method.rb +5 -8
  71. data/lib/rubocop/cop/style/encoding.rb +5 -9
  72. data/lib/rubocop/cop/style/end_block.rb +4 -6
  73. data/lib/rubocop/cop/style/eval_with_location.rb +5 -7
  74. data/lib/rubocop/cop/style/even_odd.rb +7 -11
  75. data/lib/rubocop/cop/style/expand_path_arguments.rb +21 -24
  76. data/lib/rubocop/cop/style/exponential_notation.rb +1 -1
  77. data/lib/rubocop/cop/style/float_division.rb +1 -1
  78. data/lib/rubocop/cop/style/for.rb +11 -15
  79. data/lib/rubocop/cop/style/format_string.rb +17 -19
  80. data/lib/rubocop/cop/style/format_string_token.rb +4 -7
  81. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +19 -41
  82. data/lib/rubocop/cop/style/global_vars.rb +2 -2
  83. data/lib/rubocop/cop/style/guard_clause.rb +3 -4
  84. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  85. data/lib/rubocop/cop/style/hash_each_methods.rb +5 -8
  86. data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
  87. data/lib/rubocop/cop/style/hash_transform_keys.rb +3 -2
  88. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -1
  89. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  90. data/lib/rubocop/cop/style/if_inside_else.rb +2 -2
  91. data/lib/rubocop/cop/style/if_unless_modifier.rb +10 -12
  92. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -7
  93. data/lib/rubocop/cop/style/if_with_semicolon.rb +3 -6
  94. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -1
  95. data/lib/rubocop/cop/style/infinite_loop.rb +23 -23
  96. data/lib/rubocop/cop/style/inline_comment.rb +3 -3
  97. data/lib/rubocop/cop/style/inverse_methods.rb +20 -29
  98. data/lib/rubocop/cop/style/lambda.rb +7 -12
  99. data/lib/rubocop/cop/style/lambda_call.rb +14 -13
  100. data/lib/rubocop/cop/style/line_end_concatenation.rb +19 -16
  101. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -1
  102. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +11 -11
  103. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +4 -8
  104. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +7 -6
  105. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -2
  106. data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -16
  107. data/lib/rubocop/cop/style/min_max.rb +8 -12
  108. data/lib/rubocop/cop/style/missing_else.rb +13 -13
  109. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +1 -1
  110. data/lib/rubocop/cop/style/mixin_grouping.rb +24 -27
  111. data/lib/rubocop/cop/style/mixin_usage.rb +1 -1
  112. data/lib/rubocop/cop/style/module_function.rb +10 -13
  113. data/lib/rubocop/cop/style/multiline_if_modifier.rb +3 -10
  114. data/lib/rubocop/cop/style/multiline_if_then.rb +4 -10
  115. data/lib/rubocop/cop/style/multiline_memoization.rb +12 -10
  116. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  117. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +4 -6
  118. data/lib/rubocop/cop/style/multiline_when_then.rb +7 -9
  119. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  120. data/lib/rubocop/cop/style/mutable_constant.rb +23 -20
  121. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +3 -1
  122. data/lib/rubocop/cop/style/redundant_sort.rb +0 -2
  123. data/lib/rubocop/cop/style/safe_navigation.rb +4 -4
  124. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  125. data/lib/rubocop/cop/util.rb +2 -0
  126. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  127. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  128. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  129. data/lib/rubocop/options.rb +3 -3
  130. data/lib/rubocop/rspec/shared_contexts.rb +1 -5
  131. data/lib/rubocop/runner.rb +1 -1
  132. data/lib/rubocop/version.rb +1 -1
  133. metadata +4 -4
@@ -9,27 +9,23 @@ module RuboCop
9
9
  # # encoding: UTF-8
10
10
  # # coding: UTF-8
11
11
  # # -*- coding: UTF-8 -*-
12
- class Encoding < Cop
12
+ class Encoding < Base
13
13
  include RangeHelp
14
+ extend AutoCorrector
14
15
 
15
16
  MSG_UNNECESSARY = 'Unnecessary utf-8 encoding comment.'
16
17
  ENCODING_PATTERN = /#.*coding\s?[:=]\s?(?:UTF|utf)-8/.freeze
17
18
  SHEBANG = '#!'
18
19
 
19
- def investigate(processed_source)
20
+ def on_new_investigation
20
21
  return if processed_source.buffer.source.empty?
21
22
 
22
23
  line_number = encoding_line_number(processed_source)
23
24
  return unless (@message = offense(processed_source, line_number))
24
25
 
25
26
  range = processed_source.buffer.line_range(line_number + 1)
26
- add_offense(range, location: range, message: @message)
27
- end
28
-
29
- def autocorrect(range)
30
- lambda do |corrector|
31
- corrector.remove(range_with_surrounding_space(range: range,
32
- side: :right))
27
+ add_offense(range, message: @message) do |corrector|
28
+ corrector.remove(range_with_surrounding_space(range: range, side: :right))
33
29
  end
34
30
  end
35
31
 
@@ -12,16 +12,14 @@ module RuboCop
12
12
  # # good
13
13
  # at_exit { puts 'Goodbye!' }
14
14
  #
15
- class EndBlock < Cop
15
+ class EndBlock < Base
16
+ extend AutoCorrector
17
+
16
18
  MSG = 'Avoid the use of `END` blocks. ' \
17
19
  'Use `Kernel#at_exit` instead.'
18
20
 
19
21
  def on_postexe(node)
20
- add_offense(node, location: :keyword)
21
- end
22
-
23
- def autocorrect(node)
24
- lambda do |corrector|
22
+ add_offense(node.loc.keyword) do |corrector|
25
23
  corrector.replace(node.loc.keyword, 'at_exit')
26
24
  end
27
25
  end
@@ -31,7 +31,7 @@ module RuboCop
31
31
  # def do_something
32
32
  # end
33
33
  # RUBY
34
- class EvalWithLocation < Cop
34
+ class EvalWithLocation < Base
35
35
  MSG = 'Pass `__FILE__` and `__LINE__` to `eval` method, ' \
36
36
  'as they are used by backtraces.'
37
37
  MSG_INCORRECT_LINE = 'Use `%<expected>s` instead of `%<actual>s`, ' \
@@ -126,23 +126,21 @@ module RuboCop
126
126
  end
127
127
  end
128
128
 
129
- def add_offense_for_same_line(node, line_node)
129
+ def add_offense_for_same_line(_node, line_node)
130
130
  return if special_line_keyword?(line_node)
131
131
 
132
132
  add_offense(
133
- node,
134
- location: line_node.loc.expression,
133
+ line_node.loc.expression,
135
134
  message: message_incorrect_line(line_node, nil, 0)
136
135
  )
137
136
  end
138
137
 
139
- def add_offense_for_different_line(node, line_node, line_diff)
138
+ def add_offense_for_different_line(_node, line_node, line_diff)
140
139
  sign = line_diff.positive? ? :+ : :-
141
140
  return if line_with_offset?(line_node, sign, line_diff.abs)
142
141
 
143
142
  add_offense(
144
- node,
145
- location: line_node.loc.expression,
143
+ line_node.loc.expression,
146
144
  message: message_incorrect_line(line_node, sign, line_diff.abs)
147
145
  )
148
146
  end
@@ -15,7 +15,9 @@ module RuboCop
15
15
  # # good
16
16
  # if x.even?
17
17
  # end
18
- class EvenOdd < Cop
18
+ class EvenOdd < Base
19
+ extend AutoCorrector
20
+
19
21
  MSG = 'Replace with `Integer#%<method>s?`.'
20
22
 
21
23
  def_node_matcher :even_odd_candidate?, <<~PATTERN
@@ -27,18 +29,12 @@ module RuboCop
27
29
  PATTERN
28
30
 
29
31
  def on_send(node)
30
- even_odd_candidate?(node) do |_base_number, method, arg|
31
- replacement_method = replacement_method(arg, method)
32
- add_offense(node, message: format(MSG, method: replacement_method))
33
- end
34
- end
35
-
36
- def autocorrect(node)
37
32
  even_odd_candidate?(node) do |base_number, method, arg|
38
33
  replacement_method = replacement_method(arg, method)
39
-
40
- correction = "#{base_number.source}.#{replacement_method}?"
41
- ->(corrector) { corrector.replace(node, correction) }
34
+ add_offense(node, message: format(MSG, method: replacement_method)) do |corrector|
35
+ correction = "#{base_number.source}.#{replacement_method}?"
36
+ corrector.replace(node, correction)
37
+ end
42
38
  end
43
39
  end
44
40
 
@@ -40,8 +40,9 @@ module RuboCop
40
40
  # # good
41
41
  # Pathname.new(__dir__).expand_path
42
42
  #
43
- class ExpandPathArguments < Cop
43
+ class ExpandPathArguments < Base
44
44
  include RangeHelp
45
+ extend AutoCorrector
45
46
 
46
47
  MSG = 'Use `expand_path(%<new_path>s%<new_default_dir>s)` instead of ' \
47
48
  '`expand_path(%<current_path>s, __FILE__)`.'
@@ -73,49 +74,43 @@ module RuboCop
73
74
  $_) :parent) :expand_path)
74
75
  PATTERN
75
76
 
76
- # rubocop:disable Metrics/PerceivedComplexity
77
77
  def on_send(node)
78
78
  return unless node.method?(:expand_path)
79
79
 
80
- if (captured_values = file_expand_path(node))
81
- current_path, default_dir = captured_values
82
-
80
+ if (current_path, default_dir = file_expand_path(node))
83
81
  inspect_offense_for_expand_path(node, current_path, default_dir)
84
82
  elsif (default_dir = pathname_parent_expand_path(node))
85
83
  return unless unrecommended_argument?(default_dir)
86
84
 
87
- add_offense(node, message: PATHNAME_MSG)
85
+ add_offense(node, message: PATHNAME_MSG) do |corrector|
86
+ autocorrect(corrector, node)
87
+ end
88
88
  elsif (default_dir = pathname_new_parent_expand_path(node))
89
89
  return unless unrecommended_argument?(default_dir)
90
90
 
91
- add_offense(node, message: PATHNAME_NEW_MSG)
92
- end
93
- end
94
- # rubocop:enable Metrics/PerceivedComplexity
95
-
96
- def autocorrect(node)
97
- lambda do |corrector|
98
- if (captured_values = file_expand_path(node))
99
- current_path, default_dir = captured_values
100
-
101
- autocorrect_expand_path(corrector, current_path, default_dir)
102
- elsif (default_dir = pathname_parent_expand_path(node)) ||
103
- (default_dir = pathname_new_parent_expand_path(node))
104
- corrector.replace(default_dir, '__dir__')
105
- remove_parent_method(corrector, default_dir)
91
+ add_offense(node, message: PATHNAME_NEW_MSG) do |corrector|
92
+ autocorrect(corrector, node)
106
93
  end
107
94
  end
108
95
  end
109
96
 
110
97
  private
111
98
 
99
+ def autocorrect(corrector, node)
100
+ if (current_path, default_dir = file_expand_path(node))
101
+ autocorrect_expand_path(corrector, current_path, default_dir)
102
+ elsif (dir = pathname_parent_expand_path(node) || pathname_new_parent_expand_path(node))
103
+ corrector.replace(dir, '__dir__')
104
+ remove_parent_method(corrector, dir)
105
+ end
106
+ end
107
+
112
108
  def unrecommended_argument?(default_dir)
113
109
  default_dir.source == '__FILE__'
114
110
  end
115
111
 
116
112
  def inspect_offense_for_expand_path(node, current_path, default_dir)
117
- return unless unrecommended_argument?(default_dir) &&
118
- current_path.str_type?
113
+ return unless unrecommended_argument?(default_dir) && current_path.str_type?
119
114
 
120
115
  current_path = strip_surrounded_quotes!(current_path.source)
121
116
 
@@ -131,7 +126,9 @@ module RuboCop
131
126
  current_path: "'#{current_path}'"
132
127
  )
133
128
 
134
- add_offense(node, location: :selector, message: message)
129
+ add_offense(node.loc.selector, message: message) do |corrector|
130
+ autocorrect(corrector, node)
131
+ end
135
132
  end
136
133
 
137
134
  def autocorrect_expand_path(corrector, current_path, default_dir)
@@ -58,7 +58,7 @@ module RuboCop
58
58
  # 1e4
59
59
  # 12e5
60
60
  #
61
- class ExponentialNotation < Cop
61
+ class ExponentialNotation < Base
62
62
  include ConfigurableEnforcedStyle
63
63
  MESSAGES = {
64
64
  scientific: 'Use a mantissa in [1, 10[.',
@@ -39,7 +39,7 @@ module RuboCop
39
39
  #
40
40
  # # good
41
41
  # a.fdiv(b)
42
- class FloatDivision < Cop
42
+ class FloatDivision < Base
43
43
  include ConfigurableEnforcedStyle
44
44
  MESSAGES = {
45
45
  left_coerce: 'Prefer using `.to_f` on the left side.',
@@ -38,9 +38,10 @@ module RuboCop
38
38
  # end
39
39
  # end
40
40
  #
41
- class For < Cop
41
+ class For < Base
42
42
  include ConfigurableEnforcedStyle
43
43
  include RangeHelp
44
+ extend AutoCorrector
44
45
 
45
46
  EACH_LENGTH = 'each'.length
46
47
  PREFER_EACH = 'Prefer `each` over `for`.'
@@ -48,8 +49,10 @@ module RuboCop
48
49
 
49
50
  def on_for(node)
50
51
  if style == :each
51
- add_offense(node, message: PREFER_EACH) do
52
- opposite_style_detected
52
+ return unless opposite_style_detected
53
+
54
+ add_offense(node, message: PREFER_EACH) do |corrector|
55
+ ForToEachCorrector.new(node).call(corrector)
53
56
  end
54
57
  else
55
58
  correct_style_detected
@@ -60,27 +63,20 @@ module RuboCop
60
63
  return unless suspect_enumerable?(node)
61
64
 
62
65
  if style == :for
63
- add_offense(node, message: PREFER_FOR) do
64
- opposite_style_detected
66
+ return unless opposite_style_detected
67
+
68
+ add_offense(node, message: PREFER_FOR) do |corrector|
69
+ EachToForCorrector.new(node).call(corrector)
65
70
  end
66
71
  else
67
72
  correct_style_detected
68
73
  end
69
74
  end
70
75
 
71
- def autocorrect(node)
72
- if style == :each
73
- ForToEachCorrector.new(node)
74
- else
75
- EachToForCorrector.new(node)
76
- end
77
- end
78
-
79
76
  private
80
77
 
81
78
  def suspect_enumerable?(node)
82
- node.multiline? &&
83
- node.send_node.method?(:each) && !node.send_node.arguments?
79
+ node.multiline? && node.send_node.method?(:each) && !node.send_node.arguments?
84
80
  end
85
81
  end
86
82
  end
@@ -35,8 +35,9 @@ module RuboCop
35
35
  # # good
36
36
  # puts '%10s' % 'hoge'
37
37
  #
38
- class FormatString < Cop
38
+ class FormatString < Base
39
39
  include ConfigurableEnforcedStyle
40
+ extend AutoCorrector
40
41
 
41
42
  MSG = 'Favor `%<prefer>s` over `%<current>s`.'
42
43
 
@@ -62,41 +63,38 @@ module RuboCop
62
63
 
63
64
  return if detected_style == style
64
65
 
65
- add_offense(node, location: :selector,
66
- message: message(detected_style))
66
+ add_offense(node.loc.selector, message: message(detected_style)) do |corrector|
67
+ autocorrect(corrector, node)
68
+ end
67
69
  end
68
70
  end
69
71
 
72
+ private
73
+
70
74
  def message(detected_style)
71
- format(MSG,
72
- prefer: method_name(style),
73
- current: method_name(detected_style))
75
+ format(MSG, prefer: method_name(style), current: method_name(detected_style))
74
76
  end
75
77
 
76
78
  def method_name(style_name)
77
79
  style_name == :percent ? 'String#%' : style_name
78
80
  end
79
81
 
80
- def autocorrect(node)
82
+ def autocorrect(corrector, node)
81
83
  return if variable_argument?(node)
82
84
 
83
- lambda do |corrector|
84
- case node.method_name
85
- when :%
86
- autocorrect_from_percent(corrector, node)
85
+ case node.method_name
86
+ when :%
87
+ autocorrect_from_percent(corrector, node)
88
+ when :format, :sprintf
89
+ case style
90
+ when :percent
91
+ autocorrect_to_percent(corrector, node)
87
92
  when :format, :sprintf
88
- case style
89
- when :percent
90
- autocorrect_to_percent(corrector, node)
91
- when :format, :sprintf
92
- corrector.replace(node.loc.selector, style.to_s)
93
- end
93
+ corrector.replace(node.loc.selector, style.to_s)
94
94
  end
95
95
  end
96
96
  end
97
97
 
98
- private
99
-
100
98
  def autocorrect_from_percent(corrector, node)
101
99
  percent_rhs = node.first_argument
102
100
  args = case percent_rhs.type
@@ -37,7 +37,7 @@ module RuboCop
37
37
  #
38
38
  # # good
39
39
  # format('%s', 'Hello')
40
- class FormatStringToken < Cop
40
+ class FormatStringToken < Base
41
41
  include ConfigurableEnforcedStyle
42
42
 
43
43
  def on_str(node)
@@ -45,13 +45,11 @@ module RuboCop
45
45
  return if node.each_ancestor(:xstr, :regexp).any?
46
46
 
47
47
  tokens(node) do |detected_style, token_range|
48
- if detected_style == style ||
49
- unannotated_format?(node, detected_style)
48
+ if detected_style == style || unannotated_format?(node, detected_style)
50
49
  correct_style_detected
51
50
  else
52
51
  style_detected(detected_style)
53
- add_offense(node, location: token_range,
54
- message: message(detected_style))
52
+ add_offense(token_range, message: message(detected_style))
55
53
  end
56
54
  end
57
55
  end
@@ -66,8 +64,7 @@ module RuboCop
66
64
  PATTERN
67
65
 
68
66
  def unannotated_format?(node, detected_style)
69
- detected_style == :unannotated &&
70
- !format_string_in_typical_context?(node)
67
+ detected_style == :unannotated && !format_string_in_typical_context?(node)
71
68
  end
72
69
 
73
70
  def message(detected_style)
@@ -73,10 +73,11 @@ module RuboCop
73
73
  # module Bar
74
74
  # # ...
75
75
  # end
76
- class FrozenStringLiteralComment < Cop
76
+ class FrozenStringLiteralComment < Base
77
77
  include ConfigurableEnforcedStyle
78
78
  include FrozenStringLiteral
79
79
  include RangeHelp
80
+ extend AutoCorrector
80
81
 
81
82
  MSG_MISSING_TRUE = 'Missing magic comment `# frozen_string_literal: '\
82
83
  'true`.'
@@ -85,7 +86,7 @@ module RuboCop
85
86
  MSG_DISABLED = 'Frozen string literal comment must be set to `true`.'
86
87
  SHEBANG = '#!'
87
88
 
88
- def investigate(processed_source)
89
+ def on_new_investigation
89
90
  return if processed_source.tokens.empty?
90
91
 
91
92
  case style
@@ -98,23 +99,6 @@ module RuboCop
98
99
  end
99
100
  end
100
101
 
101
- def autocorrect(node)
102
- lambda do |corrector|
103
- case style
104
- when :never
105
- remove_comment(corrector, node)
106
- when :always_true
107
- if frozen_string_literal_specified?
108
- enable_comment(corrector)
109
- else
110
- insert_comment(corrector)
111
- end
112
- else
113
- insert_comment(corrector)
114
- end
115
- end
116
- end
117
-
118
102
  private
119
103
 
120
104
  def ensure_no_comment(processed_source)
@@ -160,51 +144,45 @@ module RuboCop
160
144
  end
161
145
 
162
146
  def missing_offense(processed_source)
163
- last_special_comment = last_special_comment(processed_source)
164
147
  range = source_range(processed_source.buffer, 0, 0)
165
148
 
166
- add_offense(last_special_comment,
167
- location: range,
168
- message: MSG_MISSING)
149
+ add_offense(range, message: MSG_MISSING) do |corrector|
150
+ insert_comment(corrector)
151
+ end
169
152
  end
170
153
 
171
154
  def missing_true_offense(processed_source)
172
- last_special_comment = last_special_comment(processed_source)
173
155
  range = source_range(processed_source.buffer, 0, 0)
174
156
 
175
- add_offense(last_special_comment,
176
- location: range,
177
- message: MSG_MISSING_TRUE)
157
+ add_offense(range, message: MSG_MISSING_TRUE) do |corrector|
158
+ insert_comment(corrector)
159
+ end
178
160
  end
179
161
 
180
162
  def unnecessary_comment_offense(processed_source)
181
- frozen_string_literal_comment =
182
- frozen_string_literal_comment(processed_source)
163
+ frozen_string_literal_comment = frozen_string_literal_comment(processed_source)
183
164
 
184
- add_offense(frozen_string_literal_comment,
185
- location: frozen_string_literal_comment.pos,
186
- message: MSG_UNNECESSARY)
165
+ add_offense(frozen_string_literal_comment.pos, message: MSG_UNNECESSARY) do |corrector|
166
+ remove_comment(corrector, frozen_string_literal_comment)
167
+ end
187
168
  end
188
169
 
189
170
  def disabled_offense(processed_source)
190
- frozen_string_literal_comment =
191
- frozen_string_literal_comment(processed_source)
171
+ frozen_string_literal_comment = frozen_string_literal_comment(processed_source)
192
172
 
193
- add_offense(frozen_string_literal_comment,
194
- location: frozen_string_literal_comment.pos,
195
- message: MSG_DISABLED)
173
+ add_offense(frozen_string_literal_comment.pos, message: MSG_DISABLED) do |corrector|
174
+ enable_comment(corrector)
175
+ end
196
176
  end
197
177
 
198
178
  def remove_comment(corrector, node)
199
- corrector.remove(range_with_surrounding_space(range: node.pos,
200
- side: :right))
179
+ corrector.remove(range_with_surrounding_space(range: node.pos, side: :right))
201
180
  end
202
181
 
203
182
  def enable_comment(corrector)
204
183
  comment = frozen_string_literal_comment(processed_source)
205
184
 
206
- corrector.replace(line_range(comment.line),
207
- FROZEN_STRING_LITERAL_ENABLED)
185
+ corrector.replace(line_range(comment.line), FROZEN_STRING_LITERAL_ENABLED)
208
186
  end
209
187
 
210
188
  def insert_comment(corrector)