rubocop 0.76.0 → 0.79.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +290 -264
  4. data/lib/rubocop/ast/builder.rb +43 -42
  5. data/lib/rubocop/ast/node/block_node.rb +2 -0
  6. data/lib/rubocop/ast/node/def_node.rb +11 -0
  7. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  8. data/lib/rubocop/ast/node.rb +1 -1
  9. data/lib/rubocop/ast/traversal.rb +11 -3
  10. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  11. data/lib/rubocop/cli/command/base.rb +33 -0
  12. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  13. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  14. data/lib/rubocop/cli/command/show_cops.rb +80 -0
  15. data/lib/rubocop/cli/command/version.rb +17 -0
  16. data/lib/rubocop/cli/command.rb +21 -0
  17. data/lib/rubocop/cli/environment.rb +21 -0
  18. data/lib/rubocop/cli.rb +11 -230
  19. data/lib/rubocop/config.rb +1 -1
  20. data/lib/rubocop/config_loader.rb +19 -19
  21. data/lib/rubocop/config_obsoletion.rb +65 -12
  22. data/lib/rubocop/config_validator.rb +56 -98
  23. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  24. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  25. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  26. data/lib/rubocop/cop/cop.rb +21 -0
  27. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  28. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  29. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  30. data/lib/rubocop/cop/generator.rb +3 -4
  31. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  32. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  33. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  34. data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
  35. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +1 -1
  36. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  37. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +5 -5
  38. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  39. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +3 -3
  40. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  41. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +10 -6
  42. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
  43. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  44. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +40 -110
  45. data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
  46. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  47. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  48. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  49. data/lib/rubocop/cop/layout/space_around_operators.rb +32 -7
  50. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  51. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  52. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  53. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  54. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  55. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  56. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  57. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  58. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  59. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +3 -3
  60. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +4 -4
  61. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  62. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  63. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  64. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  65. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  66. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  67. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  68. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  69. data/lib/rubocop/cop/migration/department_name.rb +16 -1
  70. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  71. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -7
  72. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  73. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  74. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  75. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  76. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  77. data/lib/rubocop/cop/mixin/trailing_comma.rb +6 -3
  78. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  79. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  80. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
  81. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  82. data/lib/rubocop/cop/offense.rb +11 -0
  83. data/lib/rubocop/cop/registry.rb +7 -2
  84. data/lib/rubocop/cop/style/alias.rb +1 -1
  85. data/lib/rubocop/cop/style/array_join.rb +1 -1
  86. data/lib/rubocop/cop/style/attr.rb +8 -0
  87. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  88. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  89. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  90. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  91. data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -3
  92. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  93. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  94. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +168 -0
  95. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  96. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +4 -207
  97. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  98. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  99. data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
  100. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
  101. data/lib/rubocop/cop/style/next.rb +5 -5
  102. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  103. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  104. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  105. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  106. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  107. data/lib/rubocop/cop/style/redundant_return.rb +2 -8
  108. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  109. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  110. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  111. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  112. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  113. data/lib/rubocop/cop/team.rb +5 -0
  114. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  115. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -3
  116. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  117. data/lib/rubocop/formatter/tap_formatter.rb +1 -3
  118. data/lib/rubocop/node_pattern.rb +1 -1
  119. data/lib/rubocop/options.rb +8 -8
  120. data/lib/rubocop/processed_source.rb +1 -1
  121. data/lib/rubocop/rake_task.rb +1 -0
  122. data/lib/rubocop/result_cache.rb +23 -7
  123. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  124. data/lib/rubocop/runner.rb +18 -2
  125. data/lib/rubocop/target_ruby.rb +151 -0
  126. data/lib/rubocop/version.rb +1 -1
  127. data/lib/rubocop.rb +38 -22
  128. metadata +40 -25
  129. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
@@ -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
@@ -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
 
@@ -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)
@@ -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
@@ -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'
@@ -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 ' \
@@ -20,7 +20,7 @@ module RuboCop
20
20
  # a = ['run',
21
21
  # 'forrest',
22
22
  # 'run']
23
- class AlignArray < Cop
23
+ class ArrayAlignment < Cop
24
24
  include Alignment
25
25
 
26
26
  MSG = 'Align the elements of an array literal if they span more ' \
@@ -21,7 +21,7 @@ module RuboCop
21
21
  #
22
22
  # The indentation of the remaining lines can be corrected with
23
23
  # other cops such as `IndentationConsistency` and `EndAlignment`.
24
- class IndentAssignment < Cop
24
+ class AssignmentIndentation < Cop
25
25
  include CheckAssignment
26
26
  include Alignment
27
27
 
@@ -114,7 +114,7 @@ module RuboCop
114
114
 
115
115
  # Returns an array of ranges that should not be reported. It's the
116
116
  # extra spaces between the keys and values in a multiline hash,
117
- # since those are handled by the Style/AlignHash cop.
117
+ # since those are handled by the Layout/HashAlignment cop.
118
118
  def ignored_ranges(ast)
119
119
  return [] unless ast
120
120
 
@@ -2,14 +2,14 @@
2
2
 
3
3
  module RuboCop
4
4
  module Cop
5
- # rubocop:disable Metrics/LineLength
5
+ # rubocop:disable Layout/LineLength
6
6
  module Layout
7
7
  # This cop checks the indentation of the first argument in a method call.
8
- # Arguments after the first one are checked by Layout/AlignArguments,
8
+ # Arguments after the first one are checked by Layout/ArgumentAlignment,
9
9
  # not by this cop.
10
10
  #
11
11
  # For indenting the first parameter of method *definitions*, check out
12
- # Layout/IndentFirstParameter.
12
+ # Layout/FirstParameterIndentation.
13
13
  #
14
14
  # @example
15
15
  #
@@ -142,8 +142,8 @@ module RuboCop
142
142
  # nested_first_param),
143
143
  # second_param
144
144
  #
145
- class IndentFirstArgument < Cop
146
- # rubocop:enable Metrics/LineLength
145
+ class FirstArgumentIndentation < Cop
146
+ # rubocop:enable Layout/LineLength
147
147
  include Alignment
148
148
  include ConfigurableEnforcedStyle
149
149
  include RangeHelp
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Layout
6
6
  # This cop checks the indentation of the first element in an array literal
7
7
  # where the opening bracket and the first element are on separate lines.
8
- # The other elements' indentations are handled by the AlignArray cop.
8
+ # The other elements' indentations are handled by the ArrayAlignment cop.
9
9
  #
10
10
  # By default, array literals that are arguments in a method call with
11
11
  # parentheses, and where the opening square bracket of the array is on the
@@ -22,7 +22,7 @@ module RuboCop
22
22
  # @example EnforcedStyle: special_inside_parentheses (default)
23
23
  # # The `special_inside_parentheses` style enforces that the first
24
24
  # # element in an array literal where the opening bracket and first
25
- # # element are on seprate lines is indented one step (two spaces) more
25
+ # # element are on separate lines is indented one step (two spaces) more
26
26
  # # than the position inside the opening parenthesis.
27
27
  #
28
28
  # #bad
@@ -44,7 +44,7 @@ module RuboCop
44
44
  # @example EnforcedStyle: consistent
45
45
  # # The `consistent` style enforces that the first element in an array
46
46
  # # literal where the opening bracket and the first element are on
47
- # # seprate lines is indented the same as an array literal which is not
47
+ # # separate lines is indented the same as an array literal which is not
48
48
  # # defined inside a method call.
49
49
  #
50
50
  # #bad
@@ -79,7 +79,7 @@ module RuboCop
79
79
  # and_now_for_something = [
80
80
  # :completely_different
81
81
  # ]
82
- class IndentFirstArrayElement < Cop
82
+ class FirstArrayElementIndentation < Cop
83
83
  include Alignment
84
84
  include ConfigurableEnforcedStyle
85
85
  include MultilineElementIndentation
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Layout
6
6
  # This cop checks the indentation of the first key in a hash literal
7
7
  # where the opening brace and the first key are on separate lines. The
8
- # other keys' indentations are handled by the AlignHash cop.
8
+ # other keys' indentations are handled by the HashAlignment cop.
9
9
  #
10
10
  # By default, Hash literals that are arguments in a method call with
11
11
  # parentheses, and where the opening curly brace of the hash is on the
@@ -77,7 +77,7 @@ module RuboCop
77
77
  # and_now_for_something = {
78
78
  # completely: :different
79
79
  # }
80
- class IndentFirstHashElement < Cop
80
+ class FirstHashElementIndentation < Cop
81
81
  include Alignment
82
82
  include ConfigurableEnforcedStyle
83
83
  include MultilineElementIndentation
@@ -149,7 +149,7 @@ module RuboCop
149
149
  def separator_style?(first_pair)
150
150
  separator = first_pair.loc.operator
151
151
  key = "Enforced#{separator.is?(':') ? 'Colon' : 'HashRocket'}Style"
152
- config.for_cop('Layout/AlignHash')[key] == 'separator'
152
+ config.for_cop('Layout/HashAlignment')[key] == 'separator'
153
153
  end
154
154
 
155
155
  def check_based_on_longest_key(hash_node, left_brace, left_parenthesis)
@@ -5,10 +5,10 @@ module RuboCop
5
5
  module Layout
6
6
  # This cop checks the indentation of the first parameter in a method
7
7
  # definition. Parameters after the first one are checked by
8
- # Layout/AlignParameters, not by this cop.
8
+ # Layout/ParameterAlignment, not by this cop.
9
9
  #
10
10
  # For indenting the first argument of method *calls*, check out
11
- # Layout/IndentFirstArgument, which supports options related to
11
+ # Layout/FirstArgumentIndentation, which supports options related to
12
12
  # nesting that are irrelevant for method *definitions*.
13
13
  #
14
14
  # @example
@@ -41,7 +41,7 @@ module RuboCop
41
41
  # second_param)
42
42
  # 123
43
43
  # end
44
- class IndentFirstParameter < Cop
44
+ class FirstParameterIndentation < Cop
45
45
  include Alignment
46
46
  include ConfigurableEnforcedStyle
47
47
  include MultilineElementIndentation
@@ -175,12 +175,16 @@ module RuboCop
175
175
  # do_something({foo: 1,
176
176
  # bar: 2})
177
177
  #
178
- class AlignHash < Cop
179
- include HashAlignment
178
+ class HashAlignment < Cop
179
+ include HashAlignmentStyles
180
180
  include RangeHelp
181
181
 
182
- MSG = 'Align the elements of a hash literal if they span more than ' \
183
- 'one line.'
182
+ MESSAGES = { KeyAlignment => 'Align the keys of a hash literal if ' \
183
+ 'they span more than one line.',
184
+ SeparatorAlignment => 'Align the separators of a hash ' \
185
+ 'literal if they span more than one line.',
186
+ TableAlignment => 'Align the keys and values of a hash ' \
187
+ 'literal if they span more than one line.' }.freeze
184
188
 
185
189
  def on_send(node)
186
190
  return if double_splat?(node)
@@ -249,9 +253,9 @@ module RuboCop
249
253
  end
250
254
 
251
255
  def add_offences
252
- _format, offences = offences_by.min_by { |_, v| v.length }
256
+ format, offences = offences_by.min_by { |_, v| v.length }
253
257
  (offences || []).each do |offence|
254
- add_offense offence
258
+ add_offense(offence, message: MESSAGES[format])
255
259
  end
256
260
  end
257
261