rubocop 0.76.0 → 0.77.0

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 (81) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +252 -239
  4. data/lib/rubocop.rb +27 -20
  5. data/lib/rubocop/ast/node.rb +1 -1
  6. data/lib/rubocop/ast/node/block_node.rb +2 -0
  7. data/lib/rubocop/cli.rb +11 -230
  8. data/lib/rubocop/cli/command.rb +21 -0
  9. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  10. data/lib/rubocop/cli/command/base.rb +33 -0
  11. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  12. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  13. data/lib/rubocop/cli/command/show_cops.rb +73 -0
  14. data/lib/rubocop/cli/command/version.rb +17 -0
  15. data/lib/rubocop/cli/environment.rb +21 -0
  16. data/lib/rubocop/config_obsoletion.rb +62 -9
  17. data/lib/rubocop/config_validator.rb +1 -3
  18. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  19. data/lib/rubocop/cop/cop.rb +19 -0
  20. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  21. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  22. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  23. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  24. data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
  25. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +1 -1
  26. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  27. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +3 -3
  28. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  29. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +3 -3
  30. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  31. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +2 -2
  32. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +1 -1
  33. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  34. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  35. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  36. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  37. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  38. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  39. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  40. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  41. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  42. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  43. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
  44. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  45. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  46. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  47. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  48. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  49. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  50. data/lib/rubocop/cop/metrics/line_length.rb +41 -38
  51. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  52. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  53. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  54. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  55. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  56. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +3 -3
  57. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  58. data/lib/rubocop/cop/style/alias.rb +1 -1
  59. data/lib/rubocop/cop/style/array_join.rb +1 -1
  60. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  61. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  62. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  63. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  64. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
  65. data/lib/rubocop/cop/style/next.rb +5 -5
  66. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  67. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  68. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  69. data/lib/rubocop/cop/style/redundant_return.rb +2 -8
  70. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  71. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  72. data/lib/rubocop/cop/team.rb +5 -0
  73. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -3
  74. data/lib/rubocop/formatter/tap_formatter.rb +1 -3
  75. data/lib/rubocop/processed_source.rb +1 -1
  76. data/lib/rubocop/rake_task.rb +1 -0
  77. data/lib/rubocop/result_cache.rb +21 -7
  78. data/lib/rubocop/runner.rb +13 -1
  79. data/lib/rubocop/version.rb +1 -1
  80. metadata +30 -22
  81. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
@@ -127,9 +127,7 @@ module RuboCop
127
127
  valid_cop_names.each do |name|
128
128
  validate_section_presence(name)
129
129
  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
130
+ warn Rainbow(<<~MESSAGE).yellow
133
131
  Warning: #{name} does not support #{param} parameter.
134
132
 
135
133
  Supported parameters are:
@@ -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
@@ -217,6 +217,25 @@ module RuboCop
217
217
  !relevant_file?(file)
218
218
  end
219
219
 
220
+ # This method should be overriden when a cop's behavior depends
221
+ # on state that lives outside of these locations:
222
+ #
223
+ # (1) the file under inspection
224
+ # (2) the cop's source code
225
+ # (3) the config (eg a .rubocop.yml file)
226
+ #
227
+ # For example, some cops may want to look at other parts of
228
+ # the codebase being inspected to find violations. A cop may
229
+ # use the presence or absence of file `foo.rb` to determine
230
+ # whether a certain violation exists in `bar.rb`.
231
+ #
232
+ # Overriding this method allows the cop to indicate to RuboCop's
233
+ # ResultCache system when those external dependencies change,
234
+ # ie when the ResultCache should be invalidated.
235
+ def external_dependency_checksum
236
+ nil
237
+ end
238
+
220
239
  private
221
240
 
222
241
  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
@@ -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 ' \
@@ -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
 
@@ -5,11 +5,11 @@ module RuboCop
5
5
  # rubocop:disable Metrics/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,7 +142,7 @@ module RuboCop
142
142
  # nested_first_param),
143
143
  # second_param
144
144
  #
145
- class IndentFirstArgument < Cop
145
+ class FirstArgumentIndentation < Cop
146
146
  # rubocop:enable Metrics/LineLength
147
147
  include Alignment
148
148
  include ConfigurableEnforcedStyle
@@ -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,8 +175,8 @@ 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
182
  MSG = 'Align the elements of a hash literal if they span more than ' \
@@ -49,7 +49,7 @@ module RuboCop
49
49
  # something
50
50
  # RUBY
51
51
  #
52
- class IndentHeredoc < Cop
52
+ class HeredocIndentation < Cop
53
53
  include Heredoc
54
54
  include ConfigurableEnforcedStyle
55
55
 
@@ -27,7 +27,7 @@ module RuboCop
27
27
  # # good
28
28
  # # (start of file)
29
29
  # # a comment
30
- class LeadingBlankLines < Cop
30
+ class LeadingEmptyLines < Cop
31
31
  MSG = 'Unnecessary blank line at the beginning of the source.'
32
32
 
33
33
  def investigate(processed_source)
@@ -26,7 +26,7 @@ module RuboCop
26
26
  'on a separate line.'
27
27
 
28
28
  def on_send(node)
29
- return if node.method_name == :[]=
29
+ return if node.method?(:[]=)
30
30
 
31
31
  args = node.arguments
32
32
 
@@ -68,7 +68,7 @@ module RuboCop
68
68
  # baz)
69
69
  # 123
70
70
  # end
71
- class AlignParameters < Cop
71
+ class ParameterAlignment < Cop
72
72
  include Alignment
73
73
 
74
74
  ALIGN_PARAMS_MSG = 'Align the parameters of a method definition if ' \
@@ -30,10 +30,12 @@ module RuboCop
30
30
 
31
31
  DO = 'do'
32
32
  SAFE_NAVIGATION = '&.'
33
+ NAMESPACE_OPERATOR = '::'
33
34
  ACCEPT_LEFT_PAREN =
34
35
  %w[break defined? next not rescue return super yield].freeze
35
36
  ACCEPT_LEFT_SQUARE_BRACKET =
36
37
  %w[super yield].freeze
38
+ ACCEPT_NAMESPACE_OPERATOR = 'super'
37
39
 
38
40
  def on_and(node)
39
41
  check(node, [:operator].freeze) if node.keyword?
@@ -193,6 +195,8 @@ module RuboCop
193
195
 
194
196
  return false if accepted_opening_delimiter?(range, char)
195
197
  return false if safe_navigation_call?(range, pos)
198
+ return false if accept_namespace_operator?(range) &&
199
+ namespace_operator?(range, pos)
196
200
 
197
201
  char !~ /[\s;,#\\\)\}\]\.]/
198
202
  end
@@ -212,10 +216,18 @@ module RuboCop
212
216
  ACCEPT_LEFT_SQUARE_BRACKET.include?(range.source)
213
217
  end
214
218
 
219
+ def accept_namespace_operator?(range)
220
+ ACCEPT_NAMESPACE_OPERATOR == range.source
221
+ end
222
+
215
223
  def safe_navigation_call?(range, pos)
216
224
  range.source_buffer.source[pos, 2].start_with?(SAFE_NAVIGATION)
217
225
  end
218
226
 
227
+ def namespace_operator?(range, pos)
228
+ range.source_buffer.source[pos, 2].start_with?(NAMESPACE_OPERATOR)
229
+ end
230
+
219
231
  def preceded_by_operator?(node, _range)
220
232
  # regular dotted method calls bind more tightly than operators
221
233
  # so we need to climb up the AST past them
@@ -169,7 +169,7 @@ module RuboCop
169
169
  end
170
170
 
171
171
  def align_hash_cop_config
172
- config.for_cop('Layout/AlignHash')
172
+ config.for_cop('Layout/HashAlignment')
173
173
  end
174
174
 
175
175
  def hash_table_style?
@@ -37,7 +37,7 @@ module RuboCop
37
37
  # class Foo; end
38
38
  # # EOF
39
39
  #
40
- class TrailingBlankLines < Cop
40
+ class TrailingEmptyLines < Cop
41
41
  include ConfigurableEnforcedStyle
42
42
  include RangeHelp
43
43
 
@@ -33,7 +33,7 @@ module RuboCop
33
33
 
34
34
  # @param [DefNode] node a constructor definition
35
35
  def check(node)
36
- return unless node.method_name == :initialize
36
+ return unless node.method?(:initialize)
37
37
 
38
38
  check_body(node.body)
39
39
  end
@@ -18,7 +18,7 @@ module RuboCop
18
18
  # # good
19
19
  #
20
20
  # hash = { food: 'apple', other_food: 'orange' }
21
- class DuplicatedKey < Cop
21
+ class DuplicateHashKey < Cop
22
22
  include Duplication
23
23
 
24
24
  MSG = 'Duplicated key in hash literal.'
@@ -117,7 +117,7 @@ module RuboCop
117
117
  end
118
118
 
119
119
  def correct_visibility?(node, modifier, ignored_methods)
120
- return true if modifier.nil? || modifier.method_name == :public
120
+ return true if modifier.nil? || modifier.method?(:public)
121
121
 
122
122
  ignored_methods.include?(node.method_name)
123
123
  end
@@ -21,7 +21,7 @@ module RuboCop
21
21
  #
22
22
  # x < y && y < z
23
23
  # 10 <= x && x <= 20
24
- class MultipleCompare < Cop
24
+ class MultipleComparison < Cop
25
25
  MSG = 'Use the `&&` operator to compare multiple values.'
26
26
 
27
27
  def_node_matcher :multiple_compare?, <<~PATTERN
@@ -146,7 +146,7 @@ module RuboCop
146
146
  grandparent = node.parent.parent
147
147
 
148
148
  parent.when_type? || parent.send_type? || part_of_an_array?(node) ||
149
- (grandparent&.resbody_type?)
149
+ grandparent&.resbody_type?
150
150
  end
151
151
 
152
152
  def remove_brackets(array)