rubocop 0.76.0 → 0.79.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 (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
@@ -35,7 +35,7 @@ module RuboCop
35
35
  return if !node.children.last.nil? && !node.multiline? && node.then?
36
36
 
37
37
  # With more than one statements after then, there's not offense
38
- return if node.children.last&.begin_type?
38
+ return if accept_node_type?(node.body)
39
39
 
40
40
  add_offense(node, location: :begin)
41
41
  end
@@ -49,6 +49,10 @@ module RuboCop
49
49
  )
50
50
  end
51
51
  end
52
+
53
+ def accept_node_type?(node)
54
+ node&.begin_type? || node&.array_type? || node&.hash_type?
55
+ end
52
56
  end
53
57
  end
54
58
  end
@@ -49,17 +49,17 @@ module RuboCop
49
49
  def allowed_omission?(send_node)
50
50
  !send_node.arguments? || send_node.parenthesized? ||
51
51
  send_node.setter_method? || send_node.operator_method? ||
52
- whitelisted?(send_node)
52
+ allowed?(send_node)
53
53
  end
54
54
 
55
- def whitelisted?(send_node)
55
+ def allowed?(send_node)
56
56
  send_node.parent.arguments.one? &&
57
- whitelisted_methods.include?(send_node.method_name.to_s) &&
57
+ allowed_methods.include?(send_node.method_name.to_s) &&
58
58
  send_node.arguments.one?
59
59
  end
60
60
 
61
- def whitelisted_methods
62
- cop_config['Whitelist'] || []
61
+ def allowed_methods
62
+ cop_config['AllowedMethods'] || []
63
63
  end
64
64
  end
65
65
  end
@@ -20,18 +20,18 @@ module RuboCop
20
20
  # end
21
21
  #
22
22
  # # good
23
- # [1, 2].each do |o|
24
- # puts o unless o == 1
23
+ # [1, 2].each do |a|
24
+ # puts a if a == 1
25
25
  # end
26
26
  #
27
27
  # @example EnforcedStyle: always
28
28
  # # With `always` all conditions at the end of an iteration needs to be
29
29
  # # replaced by next - with `skip_modifier_ifs` the modifier if like
30
- # # this one are ignored: `[1, 2].each { |a| return 'yes' if a == 1 }`
30
+ # # this one are ignored: `[1, 2].each { |a| puts a if a == 1 }`
31
31
  #
32
32
  # # bad
33
- # [1, 2].each do |o|
34
- # puts o unless o == 1
33
+ # [1, 2].each do |a|
34
+ # puts a if a == 1
35
35
  # end
36
36
  #
37
37
  # # bad
@@ -9,18 +9,22 @@ module RuboCop
9
9
  # @example
10
10
  #
11
11
  # # bad
12
- #
13
12
  # 1000000
14
13
  # 1_00_000
15
14
  # 1_0000
16
15
  #
17
16
  # # good
18
- #
19
17
  # 1_000_000
20
18
  # 1000
21
19
  #
22
- # # good unless Strict is set
20
+ # @example Strict: false (default)
23
21
  #
22
+ # # good
23
+ # 10_000_00 # typical representation of $10,000 in cents
24
+ #
25
+ # @example Strict: true
26
+ #
27
+ # # bad
24
28
  # 10_000_00 # typical representation of $10,000 in cents
25
29
  #
26
30
  class NumericLiterals < Cop
@@ -54,9 +54,10 @@ module RuboCop
54
54
  }.freeze
55
55
 
56
56
  def on_send(node)
57
- return if node.each_ancestor(:send, :block).any? do |ancestor|
58
- ignored_method?(ancestor.method_name)
59
- end
57
+ return if ignored_method?(node.method_name) ||
58
+ node.each_ancestor(:send, :block).any? do |ancestor|
59
+ ignored_method?(ancestor.method_name)
60
+ end
60
61
 
61
62
  numeric, replacement = check(node)
62
63
 
@@ -28,7 +28,7 @@ module RuboCop
28
28
 
29
29
  def on_args(node)
30
30
  return if super_used?(node)
31
- return if whitelist.include?(node.parent.method_name.to_s)
31
+ return if allowlist.include?(node.parent.method_name.to_s)
32
32
 
33
33
  option_hash(node) do |options|
34
34
  add_offense(options)
@@ -37,8 +37,8 @@ module RuboCop
37
37
 
38
38
  private
39
39
 
40
- def whitelist
41
- cop_config['Whitelist'] || []
40
+ def allowlist
41
+ cop_config['Allowlist'] || []
42
42
  end
43
43
 
44
44
  def suspicious_name?(arg_name)
@@ -88,27 +88,27 @@ module RuboCop
88
88
  end
89
89
 
90
90
  def contains_preferred_delimiter?(node, type)
91
- preferred_delimiters = preferred_delimiters_for(type)
92
- node
93
- .children.map { |n| string_source(n) }.compact
94
- .any? { |s| preferred_delimiters.any? { |d| s.include?(d) } }
91
+ contains_delimiter?(node, preferred_delimiters_for(type))
95
92
  end
96
93
 
97
94
  def include_same_character_as_used_for_delimiter?(node, type)
98
95
  return false unless %w[%w %i].include?(type)
99
96
 
100
97
  used_delimiters = matchpairs(begin_source(node)[-1])
101
- escaped_delimiters = used_delimiters.map { |d| "\\#{d}" }.join('|')
98
+ contains_delimiter?(node, used_delimiters)
99
+ end
102
100
 
101
+ def contains_delimiter?(node, delimiters)
102
+ delimiters_regexp = Regexp.union(delimiters)
103
103
  node
104
104
  .children.map { |n| string_source(n) }.compact
105
- .any? { |s| Regexp.new(escaped_delimiters) =~ s }
105
+ .any? { |s| delimiters_regexp =~ s }
106
106
  end
107
107
 
108
108
  def string_source(node)
109
109
  if node.is_a?(String)
110
110
  node
111
- elsif node.respond_to?(:type) && node.str_type?
111
+ elsif node.respond_to?(:type) && (node.str_type? || node.sym_type?)
112
112
  node.source
113
113
  end
114
114
  end
@@ -19,7 +19,7 @@ module RuboCop
19
19
  def_node_matcher :square_brackets?,
20
20
  '(send {(send _recv _msg) str array hash} :[] ...)'
21
21
  def_node_matcher :range_end?, '^^{irange erange}'
22
- def_node_matcher :method_node_and_args, '$(send _recv _msg $...)'
22
+ def_node_matcher :method_node_and_args, '$(call _recv _msg $...)'
23
23
  def_node_matcher :rescue?, '{^resbody ^^resbody}'
24
24
  def_node_matcher :arg_in_call_with_block?,
25
25
  '^^(block (send _ _ equal?(%0) ...) ...)'
@@ -102,7 +102,7 @@ module RuboCop
102
102
  return offense(begin_node, 'a variable') if node.variable?
103
103
  return offense(begin_node, 'a constant') if node.const_type?
104
104
 
105
- check_send(begin_node, node) if node.send_type?
105
+ check_send(begin_node, node) if node.call_type?
106
106
  end
107
107
 
108
108
  def check_send(begin_node, node)
@@ -195,7 +195,7 @@ module RuboCop
195
195
  end
196
196
 
197
197
  def method_call_with_redundant_parentheses?(node)
198
- return false unless node.send_type?
198
+ return false unless node.call_type?
199
199
  return false if node.prefix_not?
200
200
  return false if range_end?(node)
201
201
 
@@ -54,8 +54,6 @@ module RuboCop
54
54
  MULTI_RETURN_MSG = 'To return multiple values, use an array.'
55
55
 
56
56
  def on_def(node)
57
- return unless node.body
58
-
59
57
  check_branch(node.body)
60
58
  end
61
59
  alias on_defs on_def
@@ -152,12 +150,8 @@ module RuboCop
152
150
  end
153
151
 
154
152
  def check_begin_node(node)
155
- expressions = *node
156
- last_expr = expressions.last
157
-
158
- return unless last_expr&.return_type?
159
-
160
- check_return_node(last_expr)
153
+ last_expr = node.children.last
154
+ check_branch(last_expr)
161
155
  end
162
156
 
163
157
  def allow_multiple_return_values?
@@ -127,7 +127,7 @@ module RuboCop
127
127
  end
128
128
 
129
129
  def base(accessor, arg)
130
- if accessor == :first || (arg&.zero?)
130
+ if accessor == :first || arg&.zero?
131
131
  'min'
132
132
  elsif accessor == :last || arg == -1
133
133
  'max'
@@ -20,8 +20,14 @@ module RuboCop
20
20
  # a, *b, _ = foo()
21
21
  # # => The correction `a, *b, = foo()` is a syntax error
22
22
  #
23
- # # good if AllowNamedUnderscoreVariables is true
23
+ # @example AllowNamedUnderscoreVariables: true (default)
24
+ # # good
24
25
  # a, b, _something = foo()
26
+ #
27
+ # @example AllowNamedUnderscoreVariables: false
28
+ # # bad
29
+ # a, b, _something = foo()
30
+ #
25
31
  class TrailingUnderscoreVariable < Cop
26
32
  include SurroundingSpace
27
33
  include RangeHelp
@@ -60,7 +60,7 @@ module RuboCop
60
60
  when :module
61
61
  return true
62
62
  else
63
- return true if pnode.method_name == :instance_eval
63
+ return true if pnode.method?(:instance_eval)
64
64
  end
65
65
  end
66
66
  false
@@ -95,9 +95,9 @@ module RuboCop
95
95
  cop_config['IgnoreClassMethods']
96
96
  end
97
97
 
98
- def whitelist
99
- whitelist = cop_config['Whitelist']
100
- Array(whitelist).map(&:to_sym) + [:initialize]
98
+ def allowed_methods
99
+ allowed_methods = cop_config['AllowedMethods']
100
+ Array(allowed_methods).map(&:to_sym) + [:initialize]
101
101
  end
102
102
 
103
103
  def dsl_writer?(method_name)
@@ -124,7 +124,7 @@ module RuboCop
124
124
  PATTERN
125
125
 
126
126
  def allowed_method?(node)
127
- whitelist.include?(node.method_name) ||
127
+ allowed_methods.include?(node.method_name) ||
128
128
  exact_name_match? && !names_match?(node)
129
129
  end
130
130
 
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Style
6
6
  # Checks for while and until statements that would fit on one line
7
7
  # if written as a modifier while/until. The maximum line length is
8
- # configured in the `Metrics/LineLength` cop.
8
+ # configured in the `Layout/LineLength` cop.
9
9
  #
10
10
  # @example
11
11
  # # bad
@@ -67,9 +67,16 @@ module RuboCop
67
67
 
68
68
  NONCOMMUTATIVE_OPERATORS = %i[===].freeze
69
69
 
70
+ PROGRAM_NAMES = %i[$0 $PROGRAM_NAME].freeze
71
+
72
+ def_node_matcher :file_constant_equal_program_name?, <<~PATTERN
73
+ (send #source_file_path_constant? {:== :!=} (gvar #program_name?))
74
+ PATTERN
75
+
70
76
  def on_send(node)
71
77
  return unless yoda_compatible_condition?(node)
72
- return if equality_only? && non_equality_operator?(node)
78
+ return if equality_only? && non_equality_operator?(node) ||
79
+ file_constant_equal_program_name?(node)
73
80
 
74
81
  valid_yoda?(node) || add_offense(node)
75
82
  end
@@ -135,6 +142,14 @@ module RuboCop
135
142
  def noncommutative_operator?(node)
136
143
  NONCOMMUTATIVE_OPERATORS.include?(node.method_name)
137
144
  end
145
+
146
+ def source_file_path_constant?(node)
147
+ node.source == '__FILE__'
148
+ end
149
+
150
+ def program_name?(name)
151
+ PROGRAM_NAMES.include?(name)
152
+ end
138
153
  end
139
154
  end
140
155
  end
@@ -86,6 +86,11 @@ module RuboCop
86
86
  raise e.cause
87
87
  end
88
88
 
89
+ def external_dependency_checksum
90
+ keys = cops.map(&:external_dependency_checksum).compact
91
+ Digest::SHA1.hexdigest(keys.join)
92
+ end
93
+
89
94
  private
90
95
 
91
96
  def offenses(processed_source)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Metrics/LineLength
3
+ # rubocop:disable Layout/LineLength
4
4
 
5
5
  module RuboCop
6
6
  module Formatter
@@ -41,7 +41,7 @@ module RuboCop
41
41
  # * `#finished`
42
42
  #
43
43
  class BaseFormatter
44
- # rubocop:enable Metrics/LineLength
44
+ # rubocop:enable Layout/LineLength
45
45
 
46
46
  # @api public
47
47
  #
@@ -24,16 +24,14 @@ module RuboCop
24
24
  message: message(offense)
25
25
  )
26
26
 
27
- # rubocop:disable Lint/HandleExceptions
28
27
  begin
29
28
  return unless valid_line?(offense)
30
29
 
31
30
  report_line(offense.location)
32
31
  report_highlighted_area(offense.highlighted_area)
33
- rescue IndexError
32
+ rescue IndexError # rubocop:disable Lint/SuppressedException
34
33
  # range is not on a valid line; perhaps the source file is empty
35
34
  end
36
- # rubocop:enable Lint/HandleExceptions
37
35
  end
38
36
 
39
37
  def valid_line?(offense)
@@ -53,11 +53,12 @@ module RuboCop
53
53
 
54
54
  def hash_for_offense(offense)
55
55
  {
56
- severity: offense.severity.name,
57
- message: offense.message,
58
- cop_name: offense.cop_name,
59
- corrected: offense.corrected?,
60
- location: hash_for_location(offense)
56
+ severity: offense.severity.name,
57
+ message: offense.message,
58
+ cop_name: offense.cop_name,
59
+ corrected: offense.corrected?,
60
+ correctable: offense.correctable?,
61
+ location: hash_for_location(offense)
61
62
  }
62
63
  end
63
64
 
@@ -51,16 +51,14 @@ module RuboCop
51
51
  message: message(offense)
52
52
  )
53
53
 
54
- # rubocop:disable Lint/HandleExceptions
55
54
  begin
56
55
  return unless valid_line?(offense)
57
56
 
58
57
  report_line(offense.location)
59
58
  report_highlighted_area(offense.highlighted_area)
60
- rescue IndexError
59
+ rescue IndexError # rubocop:disable Lint/SuppressedException
61
60
  # range is not on a valid line; perhaps the source file is empty
62
61
  end
63
- # rubocop:enable Lint/HandleExceptions
64
62
  end
65
63
 
66
64
  def annotate_message(msg)
@@ -380,7 +380,7 @@ module RuboCop
380
380
  def compile_seq_head
381
381
  return unless seq_head?
382
382
 
383
- fail_due_to 'sequences can not start with <' \
383
+ fail_due_to 'sequences cannot start with <' \
384
384
  if @terms[0].respond_to? :call
385
385
 
386
386
  with_seq_head_context(@terms[0])
@@ -264,18 +264,18 @@ module RuboCop
264
264
  # rubocop:disable Metrics/AbcSize
265
265
  def validate_compatibility # rubocop:disable Metrics/MethodLength
266
266
  if only_includes_redundant_disable?
267
- raise OptionArgumentError, 'Lint/RedundantCopDisableDirective can ' \
268
- 'not be used with --only.'
267
+ raise OptionArgumentError, 'Lint/RedundantCopDisableDirective cannot ' \
268
+ 'be used with --only.'
269
269
  end
270
270
  if except_syntax?
271
- raise OptionArgumentError, 'Syntax checking can not be turned off.'
271
+ raise OptionArgumentError, 'Syntax checking cannot be turned off.'
272
272
  end
273
273
  unless boolean_or_empty_cache?
274
274
  raise OptionArgumentError, '-C/--cache argument must be true or false'
275
275
  end
276
276
 
277
277
  if display_only_fail_level_offenses_with_autocorrect?
278
- raise OptionArgumentError, '--autocorrect can not be used with ' \
278
+ raise OptionArgumentError, '--autocorrect cannot be used with ' \
279
279
  '--display-only-fail-level-offenses'
280
280
  end
281
281
  validate_auto_gen_config
@@ -329,8 +329,8 @@ module RuboCop
329
329
  auto_gen_config: '-P/--parallel uses caching to speed up execution, ' \
330
330
  'while --auto-gen-config needs a non-cached run, ' \
331
331
  'so they cannot be combined.',
332
- fail_fast: '-P/--parallel can not be combined with -F/--fail-fast.',
333
- auto_correct: '-P/--parallel can not be combined with --auto-correct.'
332
+ fail_fast: '-P/--parallel cannot be combined with -F/--fail-fast.',
333
+ auto_correct: '-P/--parallel cannot be combined with --auto-correct.'
334
334
  }
335
335
 
336
336
  combos.each do |key, msg|
@@ -373,7 +373,7 @@ module RuboCop
373
373
  # This module contains help texts for command line options.
374
374
  module OptionsHelp
375
375
  MAX_EXCL = RuboCop::Options::DEFAULT_MAXIMUM_EXCLUSION_ITEMS.to_s
376
- # rubocop:disable Metrics/LineLength
376
+ # rubocop:disable Layout/LineLength
377
377
  FORMATTER_OPTION_LIST = RuboCop::Formatter::FormatterSet::BUILTIN_FORMATTERS_FOR_KEYS.keys
378
378
 
379
379
  TEXT = {
@@ -455,6 +455,6 @@ module RuboCop
455
455
  'reports. This is useful for editor integration.'],
456
456
  init: 'Generate a .rubocop.yml file in the current directory.'
457
457
  }.freeze
458
- # rubocop:enable Metrics/LineLength
458
+ # rubocop:enable Layout/LineLength
459
459
  end
460
460
  end
@@ -163,7 +163,7 @@ module RuboCop
163
163
  ast, comments, tokens = parser.tokenize(@buffer)
164
164
 
165
165
  ast.respond_to?(:complete!) && ast.complete!
166
- rescue Parser::SyntaxError # rubocop:disable Lint/HandleExceptions
166
+ rescue Parser::SyntaxError # rubocop:disable Lint/SuppressedException
167
167
  # All errors are in diagnostics. No need to handle exception.
168
168
  end
169
169
 
@@ -70,6 +70,7 @@ module RuboCop
70
70
  RakeFileUtils.verbose(verbose) do
71
71
  yield(*[self, task_args].slice(0, task_block.arity)) if block_given?
72
72
  options = full_options.unshift('--auto-correct')
73
+ options.delete('--parallel')
73
74
  run_cli(verbose, options)
74
75
  end
75
76
  end
@@ -77,12 +77,13 @@ module RuboCop
77
77
  config_store.for('.').for_all_cops['AllowSymlinksInCacheRootDirectory']
78
78
  end
79
79
 
80
- def initialize(file, options, config_store, cache_root = nil)
80
+ def initialize(file, team, options, config_store, cache_root = nil)
81
81
  cache_root ||= ResultCache.cache_root(config_store)
82
82
  @allow_symlinks_in_cache_location =
83
83
  ResultCache.allow_symlinks_in_cache_location?(config_store)
84
- @path = File.join(cache_root, rubocop_checksum,
85
- relevant_options_digest(options),
84
+ @path = File.join(cache_root,
85
+ rubocop_checksum,
86
+ context_checksum(team, options),
86
87
  file_checksum(file, config_store))
87
88
  @cached_data = CachedData.new(file)
88
89
  end
@@ -182,10 +183,25 @@ module RuboCop
182
183
  # don't affect caching.
183
184
  def relevant_options_digest(options)
184
185
  options = options.reject { |key, _| NON_CHANGING.include?(key) }
185
- options = options.to_s.gsub(/[^a-z]+/i, '_')
186
- # We must avoid making file names too long for some filesystems to handle
187
- # If they are short, we can leave them human-readable
188
- options.length <= 32 ? options : Digest::SHA1.hexdigest(options)
186
+ options.to_s.gsub(/[^a-z]+/i, '_')
187
+ end
188
+
189
+ # The external dependency checksums are cached per RuboCop team so that
190
+ # the checksums don't need to be recomputed for each file.
191
+ def team_checksum(team)
192
+ @checksum_by_team ||= {}
193
+ @checksum_by_team[team.object_id] ||= team.external_dependency_checksum
194
+ end
195
+
196
+ # We combine team and options into a single "context" checksum to avoid
197
+ # making file names that are too long for some filesystems to handle.
198
+ # This context is for anything that's not (1) the RuboCop executable
199
+ # checksum or (2) the inspected file checksum.
200
+ def context_checksum(team, options)
201
+ Digest::SHA1.hexdigest([
202
+ team_checksum(team),
203
+ relevant_options_digest(options)
204
+ ].join)
189
205
  end
190
206
  end
191
207
  end
@@ -52,6 +52,7 @@ RSpec.shared_context 'config', :config do
52
52
  cop_name = described_class.cop_name
53
53
  hash[cop_name] = RuboCop::ConfigLoader
54
54
  .default_configuration[cop_name]
55
+ .merge('Enabled' => true) # in case it is 'pending'
55
56
  .merge(cop_config)
56
57
  end
57
58
 
@@ -88,3 +89,7 @@ end
88
89
  RSpec.shared_context 'ruby 2.6', :ruby26 do
89
90
  let(:ruby_version) { 2.6 }
90
91
  end
92
+
93
+ RSpec.shared_context 'ruby 2.7', :ruby27 do
94
+ let(:ruby_version) { 2.7 }
95
+ end
@@ -121,8 +121,14 @@ module RuboCop
121
121
  end
122
122
  end
123
123
 
124
+ def cached_result(file, team)
125
+ ResultCache.new(file, team, @options, @config_store)
126
+ end
127
+
124
128
  def file_offense_cache(file)
125
- cache = ResultCache.new(file, @options, @config_store) if cached_run?
129
+ config = @config_store.for(file)
130
+ cache = cached_result(file, standby_team(config)) if cached_run?
131
+
126
132
  if cache&.valid?
127
133
  offenses = cache.load
128
134
  # If we're running --auto-correct and the cache says there are
@@ -208,7 +214,7 @@ module RuboCop
208
214
  @config_store.for(Dir.pwd).for_all_cops['UseCache']) &&
209
215
  # When running --auto-gen-config, there's some processing done in the
210
216
  # cops related to calculating the Max parameters for Metrics cops. We
211
- # need to do that processing and can not use caching.
217
+ # need to do that processing and cannot use caching.
212
218
  !@options[:auto_gen_config] &&
213
219
  # We can't cache results from code which is piped in to stdin
214
220
  !@options[:stdin]
@@ -359,5 +365,15 @@ module RuboCop
359
365
  ProcessedSource.from_file(file, ruby_version)
360
366
  end
361
367
  end
368
+
369
+ # A Cop::Team instance is stateful and may change when inspecting.
370
+ # The "standby" team for a given config is an initialized but
371
+ # otherwise dormant team that can be used for config- and option-
372
+ # level caching in ResultCache.
373
+ def standby_team(config)
374
+ @team_by_config ||= {}
375
+ @team_by_config[config.object_id] ||=
376
+ Cop::Team.new(mobilized_cop_classes(config), config, @options)
377
+ end
362
378
  end
363
379
  end