rubocop 0.76.0 → 0.80.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +325 -283
  5. data/lib/rubocop.rb +43 -23
  6. data/lib/rubocop/ast/builder.rb +43 -42
  7. data/lib/rubocop/ast/node.rb +1 -13
  8. data/lib/rubocop/ast/node/block_node.rb +2 -0
  9. data/lib/rubocop/ast/node/def_node.rb +11 -0
  10. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  11. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  12. data/lib/rubocop/ast/traversal.rb +20 -3
  13. data/lib/rubocop/cli.rb +11 -230
  14. data/lib/rubocop/cli/command.rb +21 -0
  15. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  16. data/lib/rubocop/cli/command/base.rb +33 -0
  17. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  18. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  19. data/lib/rubocop/cli/command/show_cops.rb +80 -0
  20. data/lib/rubocop/cli/command/version.rb +17 -0
  21. data/lib/rubocop/cli/environment.rb +21 -0
  22. data/lib/rubocop/comment_config.rb +6 -1
  23. data/lib/rubocop/config.rb +28 -10
  24. data/lib/rubocop/config_loader.rb +19 -19
  25. data/lib/rubocop/config_obsoletion.rb +65 -11
  26. data/lib/rubocop/config_validator.rb +56 -98
  27. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  28. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  30. data/lib/rubocop/cop/cop.rb +21 -0
  31. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  32. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  33. data/lib/rubocop/cop/generator.rb +3 -4
  34. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  35. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  36. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  37. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  38. data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
  39. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +1 -1
  40. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  41. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  42. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +5 -5
  43. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  44. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +3 -3
  45. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  46. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +10 -6
  47. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
  48. data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -2
  49. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  50. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +67 -108
  51. data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
  52. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  53. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  54. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  55. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  56. data/lib/rubocop/cop/layout/space_around_operators.rb +50 -7
  57. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  58. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  59. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  60. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  61. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  62. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  63. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  64. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  65. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  66. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  67. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  68. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +3 -3
  69. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +13 -8
  70. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  71. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  72. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  73. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  74. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  75. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  76. data/lib/rubocop/cop/lint/useless_setter_call.rb +5 -1
  77. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  78. data/lib/rubocop/cop/migration/department_name.rb +30 -2
  79. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  80. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  81. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
  82. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  83. data/lib/rubocop/cop/mixin/hash_transform_method.rb +171 -0
  84. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  85. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  86. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  87. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  88. data/lib/rubocop/cop/mixin/trailing_comma.rb +8 -12
  89. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  90. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  91. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  92. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
  93. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  94. data/lib/rubocop/cop/offense.rb +11 -0
  95. data/lib/rubocop/cop/registry.rb +7 -2
  96. data/lib/rubocop/cop/style/alias.rb +1 -1
  97. data/lib/rubocop/cop/style/array_join.rb +1 -1
  98. data/lib/rubocop/cop/style/attr.rb +8 -0
  99. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  100. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  101. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  102. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  103. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
  104. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  105. data/lib/rubocop/cop/style/hash_each_methods.rb +87 -0
  106. data/lib/rubocop/cop/style/hash_transform_keys.rb +79 -0
  107. data/lib/rubocop/cop/style/hash_transform_values.rb +79 -0
  108. data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -3
  109. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  110. data/lib/rubocop/cop/style/inverse_methods.rb +8 -4
  111. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  112. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
  113. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
  114. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  115. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  116. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  117. data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
  118. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +7 -7
  119. data/lib/rubocop/cop/style/next.rb +5 -5
  120. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  121. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  122. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  123. data/lib/rubocop/cop/style/or_assignment.rb +3 -2
  124. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  125. data/lib/rubocop/cop/style/redundant_condition.rb +17 -4
  126. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  127. data/lib/rubocop/cop/style/redundant_return.rb +2 -8
  128. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  129. data/lib/rubocop/cop/style/symbol_array.rb +2 -2
  130. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  131. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -22
  132. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  133. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  134. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  135. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  136. data/lib/rubocop/cop/team.rb +5 -0
  137. data/lib/rubocop/cop/variable_force.rb +4 -1
  138. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  139. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -3
  140. data/lib/rubocop/formatter/formatter_set.rb +1 -0
  141. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  142. data/lib/rubocop/formatter/junit_formatter.rb +63 -0
  143. data/lib/rubocop/formatter/tap_formatter.rb +1 -3
  144. data/lib/rubocop/node_pattern.rb +97 -11
  145. data/lib/rubocop/options.rb +8 -8
  146. data/lib/rubocop/processed_source.rb +1 -1
  147. data/lib/rubocop/rake_task.rb +1 -0
  148. data/lib/rubocop/result_cache.rb +23 -7
  149. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  150. data/lib/rubocop/runner.rb +18 -2
  151. data/lib/rubocop/target_ruby.rb +151 -0
  152. data/lib/rubocop/version.rb +1 -1
  153. metadata +60 -27
  154. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
  155. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
@@ -14,25 +14,19 @@ module RuboCop
14
14
  VersionAdded VersionChanged VersionRemoved
15
15
  Reference Safe SafeAutoCorrect].freeze
16
16
 
17
- # 2.3 is the oldest officially supported Ruby version.
18
- DEFAULT_RUBY_VERSION = 2.3
19
- KNOWN_RUBIES = [2.3, 2.4, 2.5, 2.6, 2.7].freeze
20
- OBSOLETE_RUBIES = {
21
- 1.9 => '0.50', 2.0 => '0.50', 2.1 => '0.58', 2.2 => '0.69'
22
- }.freeze
23
- RUBY_VERSION_FILENAME = '.ruby-version'
24
-
25
- def_delegators :@config,
26
- :smart_loaded_path, :for_all_cops, :find_file_upwards,
27
- :base_dir_for_path_parameters, :bundler_lock_file_path
17
+ def_delegators :@config, :smart_loaded_path, :for_all_cops
28
18
 
29
19
  def initialize(config)
30
20
  @config = config
31
21
  @config_obsoletion = ConfigObsoletion.new(config)
22
+ @target_ruby = TargetRuby.new(config)
32
23
  end
33
24
 
34
25
  def validate
35
- # Don't validate RuboCop's own files. Avoids infinite recursion.
26
+ check_cop_config_value(@config)
27
+ reject_conflicting_safe_settings
28
+
29
+ # Don't validate RuboCop's own files further. Avoids infinite recursion.
36
30
  return if @config.internal?
37
31
 
38
32
  valid_cop_names, invalid_cop_names = @config.keys.partition do |key|
@@ -41,7 +35,7 @@ module RuboCop
41
35
 
42
36
  @config_obsoletion.reject_obsolete_cops_and_parameters
43
37
 
44
- warn_about_unrecognized_cops(invalid_cop_names)
38
+ alert_about_unrecognized_cops(invalid_cop_names)
45
39
  check_target_ruby
46
40
  validate_parameter_names(valid_cop_names)
47
41
  validate_enforced_styles(valid_cop_names)
@@ -50,23 +44,7 @@ module RuboCop
50
44
  end
51
45
 
52
46
  def target_ruby_version
53
- @target_ruby_version ||= begin
54
- if for_all_cops['TargetRubyVersion']
55
- @target_ruby_version_source = :rubocop_yml
56
-
57
- for_all_cops['TargetRubyVersion'].to_f
58
- elsif target_ruby_version_from_version_file
59
- @target_ruby_version_source = :ruby_version_file
60
-
61
- target_ruby_version_from_version_file
62
- elsif target_ruby_version_from_bundler_lock_file
63
- @target_ruby_version_source = :bundler_lock_file
64
-
65
- target_ruby_version_from_bundler_lock_file
66
- else
67
- DEFAULT_RUBY_VERSION
68
- end
69
- end
47
+ target_ruby.version
70
48
  end
71
49
 
72
50
  def validate_section_presence(name)
@@ -78,25 +56,30 @@ module RuboCop
78
56
 
79
57
  private
80
58
 
59
+ attr_reader :target_ruby
60
+
81
61
  def check_target_ruby
82
- return if KNOWN_RUBIES.include?(target_ruby_version)
62
+ return if target_ruby.supported?
83
63
 
84
- msg = if OBSOLETE_RUBIES.include?(target_ruby_version)
64
+ source = target_ruby.source
65
+ last_version = target_ruby.rubocop_version_with_support
66
+
67
+ msg = if last_version
85
68
  "RuboCop found unsupported Ruby version #{target_ruby_version} " \
86
- "in #{target_ruby_source}. #{target_ruby_version}-compatible " \
87
- 'analysis was dropped after version ' \
88
- "#{OBSOLETE_RUBIES[target_ruby_version]}."
69
+ "in #{source}. #{target_ruby_version}-compatible " \
70
+ "analysis was dropped after version #{last_version}."
89
71
  else
90
72
  'RuboCop found unknown Ruby version ' \
91
- "#{target_ruby_version.inspect} in #{target_ruby_source}."
73
+ "#{target_ruby_version.inspect} in #{source}."
92
74
  end
93
75
 
94
- msg += "\nSupported versions: #{KNOWN_RUBIES.join(', ')}"
76
+ msg += "\nSupported versions: #{TargetRuby.supported_versions.join(', ')}"
95
77
 
96
78
  raise ValidationError, msg
97
79
  end
98
80
 
99
- def warn_about_unrecognized_cops(invalid_cop_names)
81
+ def alert_about_unrecognized_cops(invalid_cop_names)
82
+ unknown_cops = []
100
83
  invalid_cop_names.each do |name|
101
84
  # There could be a custom cop with this name. If so, don't warn
102
85
  next if Cop::Cop.registry.contains_cop_matching?([name])
@@ -106,9 +89,10 @@ module RuboCop
106
89
  # to do so than to pass the value around to various methods.
107
90
  next if name == 'inherit_mode'
108
91
 
109
- warn Rainbow("Warning: unrecognized cop #{name} found in " \
110
- "#{smart_loaded_path}").yellow
92
+ unknown_cops << "unrecognized cop #{name} found in " \
93
+ "#{smart_loaded_path}"
111
94
  end
95
+ raise ValidationError, unknown_cops.join(', ') if unknown_cops.any?
112
96
  end
113
97
 
114
98
  def validate_syntax_cop
@@ -127,9 +111,7 @@ module RuboCop
127
111
  valid_cop_names.each do |name|
128
112
  validate_section_presence(name)
129
113
  each_invalid_parameter(name) do |param, supported_params|
130
- # FIXME: Remove .to_s, which works around a JRuby bug:
131
- # https://github.com/jruby/jruby/issues/5935
132
- warn Rainbow(<<~MESSAGE).yellow.to_s
114
+ warn Rainbow(<<~MESSAGE).yellow
133
115
  Warning: #{name} does not support #{param} parameter.
134
116
 
135
117
  Supported parameters are:
@@ -178,71 +160,47 @@ module RuboCop
178
160
  formats.all? { |format| valid.include?(format) }
179
161
  end
180
162
 
181
- def target_ruby_source
182
- case @target_ruby_version_source
183
- when :ruby_version_file
184
- "`#{RUBY_VERSION_FILENAME}`"
185
- when :bundler_lock_file
186
- "`#{bundler_lock_file_path}`"
187
- when :rubocop_yml
188
- "`TargetRubyVersion` parameter (in #{smart_loaded_path})"
189
- end
190
- end
163
+ def reject_mutually_exclusive_defaults
164
+ disabled_by_default = for_all_cops['DisabledByDefault']
165
+ enabled_by_default = for_all_cops['EnabledByDefault']
166
+ return unless disabled_by_default && enabled_by_default
191
167
 
192
- def ruby_version_file
193
- @ruby_version_file ||=
194
- find_file_upwards(RUBY_VERSION_FILENAME, base_dir_for_path_parameters)
168
+ msg = 'Cops cannot be both enabled by default and disabled by default'
169
+ raise ValidationError, msg
195
170
  end
196
171
 
197
- def target_ruby_version_from_version_file
198
- file = ruby_version_file
199
- return unless file && File.file?(file)
172
+ def reject_conflicting_safe_settings
173
+ @config.each do |name, cop_config|
174
+ next unless cop_config.is_a?(Hash)
175
+ next unless cop_config['Safe'] == false &&
176
+ cop_config['SafeAutoCorrect'] == true
200
177
 
201
- @target_ruby_version_from_version_file ||=
202
- File.read(file).match(/\A(ruby-)?(?<version>\d+\.\d+)/) do |md|
203
- md[:version].to_f
204
- end
178
+ msg = 'Unsafe cops cannot have a safe auto-correction ' \
179
+ "(section #{name} in #{smart_loaded_path})"
180
+ raise ValidationError, msg
181
+ end
205
182
  end
206
183
 
207
- def target_ruby_version_from_bundler_lock_file
208
- @target_ruby_version_from_bundler_lock_file ||=
209
- read_ruby_version_from_bundler_lock_file
210
- end
184
+ def check_cop_config_value(hash, parent = nil)
185
+ hash.each do |key, value|
186
+ check_cop_config_value(value, key) if value.is_a?(Hash)
187
+
188
+ next unless %w[Enabled
189
+ Safe
190
+ SafeAutoCorrect
191
+ AutoCorrect].include?(key) && value.is_a?(String)
192
+
193
+ next if key == 'Enabled' && value == 'pending'
211
194
 
212
- def read_ruby_version_from_bundler_lock_file
213
- lock_file_path = bundler_lock_file_path
214
- return nil unless lock_file_path
215
-
216
- in_ruby_section = false
217
- File.foreach(lock_file_path) do |line|
218
- # If ruby is in Gemfile.lock or gems.lock, there should be two lines
219
- # towards the bottom of the file that look like:
220
- # RUBY VERSION
221
- # ruby W.X.YpZ
222
- # We ultimately want to match the "ruby W.X.Y.pZ" line, but there's
223
- # extra logic to make sure we only start looking once we've seen the
224
- # "RUBY VERSION" line.
225
- in_ruby_section ||= line.match(/^\s*RUBY\s*VERSION\s*$/)
226
- next unless in_ruby_section
227
-
228
- # We currently only allow this feature to work with MRI ruby. If jruby
229
- # (or something else) is used by the project, it's lock file will have a
230
- # line that looks like:
231
- # RUBY VERSION
232
- # ruby W.X.YpZ (jruby x.x.x.x)
233
- # The regex won't match in this situation.
234
- result = line.match(/^\s*ruby\s+(\d+\.\d+)[p.\d]*\s*$/)
235
- return result.captures.first.to_f if result
195
+ raise ValidationError, msg_not_boolean(parent, key, value)
236
196
  end
237
197
  end
238
198
 
239
- def reject_mutually_exclusive_defaults
240
- disabled_by_default = for_all_cops['DisabledByDefault']
241
- enabled_by_default = for_all_cops['EnabledByDefault']
242
- return unless disabled_by_default && enabled_by_default
243
-
244
- msg = 'Cops cannot be both enabled by default and disabled by default'
245
- raise ValidationError, msg
199
+ # FIXME: Handling colors in exception messages like this is ugly.
200
+ def msg_not_boolean(parent, key, value)
201
+ "#{Rainbow('').reset}" \
202
+ "Property #{Rainbow(key).yellow} of cop #{Rainbow(parent).yellow}" \
203
+ " is supposed to be a boolean and #{Rainbow(value).yellow} is not."
246
204
  end
247
205
  end
248
206
  end
@@ -24,15 +24,18 @@ module RuboCop
24
24
  @options[:disable_uncorrectable] == true
25
25
  end
26
26
 
27
+ def safe_autocorrect?
28
+ cop_config.fetch('Safe', true) &&
29
+ cop_config.fetch('SafeAutoCorrect', true)
30
+ end
31
+
27
32
  def autocorrect_enabled?
28
33
  # allow turning off autocorrect on a cop by cop basis
29
34
  return true unless cop_config
30
35
 
31
36
  return false if cop_config['AutoCorrect'] == false
32
37
 
33
- if @options.fetch(:safe_auto_correct, false)
34
- return cop_config.fetch('SafeAutoCorrect', true)
35
- end
38
+ return safe_autocorrect? if @options.fetch(:safe_auto_correct, false)
36
39
 
37
40
  true
38
41
  end
@@ -76,7 +79,7 @@ module RuboCop
76
79
  end
77
80
 
78
81
  def max_line_length
79
- config.for_cop('Metrics/LineLength')['Max'] || 80
82
+ config.for_cop('Layout/LineLength')['Max'] || 80
80
83
  end
81
84
 
82
85
  def disable_offense_at_end_of_line(range, eol_comment)
@@ -24,7 +24,7 @@ module RuboCop
24
24
 
25
25
  def on_send(node)
26
26
  return unless gem_declaration?(node)
27
- return if whitelisted_gem?(node)
27
+ return if ignored_gem?(node)
28
28
  return if commented?(node)
29
29
 
30
30
  add_offense(node)
@@ -54,9 +54,9 @@ module RuboCop
54
54
  comment_line?(node2.loc.expression.source)
55
55
  end
56
56
 
57
- def whitelisted_gem?(node)
58
- whitelist = Array(cop_config['Whitelist'])
59
- whitelist.include?(node.first_argument.value)
57
+ def ignored_gem?(node)
58
+ ignored_gems = Array(cop_config['IgnoredGems'])
59
+ ignored_gems.include?(node.first_argument.value)
60
60
  end
61
61
  end
62
62
  end
@@ -13,8 +13,8 @@ module RuboCop
13
13
  #
14
14
  # However, it don't replace all `sources` of `http://` with `https://`.
15
15
  # For example, when specifying an internal gem server using HTTP on the
16
- # intranet, a use case where HTTPS can not be specified was considered.
17
- # Consider using HTTP only if you can not use HTTPS.
16
+ # intranet, a use case where HTTPS cannot be specified was considered.
17
+ # Consider using HTTP only if you cannot use HTTPS.
18
18
  #
19
19
  # @example
20
20
  # # bad
@@ -172,6 +172,8 @@ module RuboCop
172
172
  end
173
173
 
174
174
  def disable_uncorrectable(node)
175
+ return unless node
176
+
175
177
  @disabled_lines ||= {}
176
178
  line = node.location.line
177
179
  return if @disabled_lines.key?(line)
@@ -217,6 +219,25 @@ module RuboCop
217
219
  !relevant_file?(file)
218
220
  end
219
221
 
222
+ # This method should be overridden when a cop's behavior depends
223
+ # on state that lives outside of these locations:
224
+ #
225
+ # (1) the file under inspection
226
+ # (2) the cop's source code
227
+ # (3) the config (eg a .rubocop.yml file)
228
+ #
229
+ # For example, some cops may want to look at other parts of
230
+ # the codebase being inspected to find violations. A cop may
231
+ # use the presence or absence of file `foo.rb` to determine
232
+ # whether a certain violation exists in `bar.rb`.
233
+ #
234
+ # Overriding this method allows the cop to indicate to RuboCop's
235
+ # ResultCache system when those external dependencies change,
236
+ # ie when the ResultCache should be invalidated.
237
+ def external_dependency_checksum
238
+ nil
239
+ end
240
+
220
241
  private
221
242
 
222
243
  def find_message(node, message)
@@ -12,12 +12,11 @@ module RuboCop
12
12
  def empty_corrections(processed_source, corrector, empty_config,
13
13
  left_token, right_token)
14
14
  @processed_source = processed_source
15
+ range = range_between(left_token.end_pos, right_token.begin_pos)
15
16
  if offending_empty_space?(empty_config, left_token, right_token)
16
- range = side_space_range(range: left_token.pos, side: :right)
17
17
  corrector.remove(range)
18
18
  corrector.insert_after(left_token.pos, ' ')
19
19
  elsif offending_empty_no_space?(empty_config, left_token, right_token)
20
- range = side_space_range(range: left_token.pos, side: :right)
21
20
  corrector.remove(range)
22
21
  end
23
22
  end
@@ -98,7 +98,7 @@ module RuboCop
98
98
  end
99
99
 
100
100
  def_node_search :dependency_declarations, <<~PATTERN
101
- (send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} ...)
101
+ (send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} (str _) ...)
102
102
  PATTERN
103
103
  end
104
104
  end
@@ -104,10 +104,9 @@ module RuboCop
104
104
  end
105
105
  SPEC
106
106
 
107
- CONFIGURATION_ADDED_MESSAGE = <<~MESSAGE
108
- [modify] A configuration for the cop is added into %<configuration_file_path>s.
109
- If you want to disable the cop by default, set `Enabled` option to false.
110
- MESSAGE
107
+ CONFIGURATION_ADDED_MESSAGE =
108
+ '[modify] A configuration for the cop is added into ' \
109
+ '%<configuration_file_path>s.'
111
110
 
112
111
  def initialize(name, github_user, output: $stdout)
113
112
  @badge = Badge.parse(name)
@@ -10,7 +10,7 @@ module RuboCop
10
10
  TEMPLATE = <<~YAML
11
11
  %<badge>s:
12
12
  Description: 'TODO: Write a description of the cop.'
13
- Enabled: true
13
+ Enabled: pending
14
14
  VersionAdded: '%<version_added>s'
15
15
  YAML
16
16
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'internal_affairs/method_name_equal'
3
4
  require_relative 'internal_affairs/node_destructuring'
4
5
  require_relative 'internal_affairs/node_type_predicate'
5
6
  require_relative 'internal_affairs/offense_location_keyword'
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Checks that method names are checked using `method?` method.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # node.method_name == :do_something
11
+ #
12
+ # # good
13
+ # node.method?(:do_something)
14
+ #
15
+ class MethodNameEqual < Cop
16
+ include RangeHelp
17
+
18
+ MSG = 'Use `method?(%<method_name>s)` instead of ' \
19
+ '`method_name == %<method_name>s`.'
20
+
21
+ def_node_matcher :method_name?, <<~PATTERN
22
+ (send
23
+ $(send
24
+ (...) :method_name) :==
25
+ $...)
26
+ PATTERN
27
+
28
+ def on_send(node)
29
+ method_name?(node) do |method_name_node, method_name_arg|
30
+ message = format(MSG, method_name: method_name_arg.first.source)
31
+
32
+ range = range(method_name_node, node)
33
+
34
+ add_offense(node, location: range, message: message)
35
+ end
36
+ end
37
+
38
+ def autocorrect(node)
39
+ lambda do |corrector|
40
+ method_name?(node) do |method_name_node, method_name_arg|
41
+ corrector.replace(
42
+ range(method_name_node, node),
43
+ "method?(#{method_name_arg.first.source})"
44
+ )
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def range(method_name_node, node)
52
+ range_between(
53
+ method_name_node.loc.selector.begin_pos, node.source_range.end_pos
54
+ )
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -37,7 +37,7 @@ module RuboCop
37
37
  #
38
38
  # foo :bar,
39
39
  # :baz
40
- class AlignArguments < Cop
40
+ class ArgumentAlignment < Cop
41
41
  include Alignment
42
42
 
43
43
  ALIGN_PARAMS_MSG = 'Align the arguments of a method call if ' \