rubocop 0.85.0 → 0.88.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -17
  3. data/bin/rubocop-profile +31 -0
  4. data/config/default.yml +132 -11
  5. data/lib/rubocop.rb +17 -1
  6. data/lib/rubocop/cli.rb +2 -4
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +42 -7
  8. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  9. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  10. data/lib/rubocop/config.rb +1 -1
  11. data/lib/rubocop/config_loader.rb +39 -67
  12. data/lib/rubocop/config_loader_resolver.rb +1 -1
  13. data/lib/rubocop/config_obsoletion.rb +0 -1
  14. data/lib/rubocop/config_store.rb +4 -0
  15. data/lib/rubocop/cop/autocorrect_logic.rb +14 -24
  16. data/lib/rubocop/cop/badge.rb +1 -1
  17. data/lib/rubocop/cop/base.rb +407 -0
  18. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
  19. data/lib/rubocop/cop/commissioner.rb +48 -50
  20. data/lib/rubocop/cop/cop.rb +91 -235
  21. data/lib/rubocop/cop/corrector.rb +38 -115
  22. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +26 -0
  23. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +7 -2
  24. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  25. data/lib/rubocop/cop/generator.rb +1 -1
  26. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  27. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
  28. data/lib/rubocop/cop/layout/case_indentation.rb +18 -19
  29. data/lib/rubocop/cop/layout/class_structure.rb +2 -37
  30. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
  31. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  32. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
  33. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +3 -8
  34. data/lib/rubocop/cop/layout/end_alignment.rb +3 -2
  35. data/lib/rubocop/cop/layout/end_of_line.rb +1 -1
  36. data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -1
  37. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
  38. data/lib/rubocop/cop/layout/hash_alignment.rb +2 -3
  39. data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
  40. data/lib/rubocop/cop/layout/multiline_block_layout.rb +17 -7
  41. data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +22 -27
  43. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  44. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +27 -68
  45. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  46. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  47. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +4 -3
  48. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  49. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  50. data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
  51. data/lib/rubocop/cop/legacy/corrector.rb +29 -0
  52. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  53. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -4
  54. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +8 -2
  55. data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +39 -0
  56. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
  57. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  58. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
  59. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +3 -2
  60. data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
  61. data/lib/rubocop/cop/lint/literal_as_condition.rb +11 -1
  62. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +8 -1
  63. data/lib/rubocop/cop/lint/nested_method_definition.rb +14 -20
  64. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +69 -2
  65. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -3
  66. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  67. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  68. data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
  69. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  70. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +31 -25
  71. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
  72. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  73. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +9 -1
  74. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  75. data/lib/rubocop/cop/lint/syntax.rb +11 -26
  76. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
  77. data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -1
  78. data/lib/rubocop/cop/metrics/block_length.rb +22 -0
  79. data/lib/rubocop/cop/metrics/class_length.rb +25 -2
  80. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  81. data/lib/rubocop/cop/metrics/method_length.rb +23 -0
  82. data/lib/rubocop/cop/metrics/module_length.rb +25 -2
  83. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  84. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
  85. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  86. data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
  87. data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
  88. data/lib/rubocop/cop/mixin/code_length.rb +4 -0
  89. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
  90. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  91. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  92. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  93. data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
  94. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  95. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  96. data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
  97. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
  98. data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
  99. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  100. data/lib/rubocop/cop/mixin/range_help.rb +1 -1
  101. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +27 -0
  102. data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
  103. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  104. data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -5
  105. data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
  106. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  107. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +6 -4
  108. data/lib/rubocop/cop/mixin/visibility_help.rb +50 -0
  109. data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
  110. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +3 -3
  111. data/lib/rubocop/cop/naming/file_name.rb +1 -3
  112. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  113. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  114. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  115. data/lib/rubocop/cop/naming/predicate_name.rb +3 -5
  116. data/lib/rubocop/cop/naming/variable_name.rb +1 -1
  117. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  118. data/lib/rubocop/cop/offense.rb +16 -2
  119. data/lib/rubocop/cop/registry.rb +62 -7
  120. data/lib/rubocop/cop/style/accessor_grouping.rb +147 -0
  121. data/lib/rubocop/cop/style/array_coercion.rb +63 -0
  122. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +3 -2
  123. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  124. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +146 -0
  125. data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
  126. data/lib/rubocop/cop/style/case_like_if.rb +217 -0
  127. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  128. data/lib/rubocop/cop/style/class_vars.rb +21 -0
  129. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  130. data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
  131. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  132. data/lib/rubocop/cop/style/copyright.rb +3 -3
  133. data/lib/rubocop/cop/style/date_time.rb +1 -1
  134. data/lib/rubocop/cop/style/dir.rb +2 -2
  135. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  136. data/lib/rubocop/cop/style/documentation.rb +2 -2
  137. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  138. data/lib/rubocop/cop/style/empty_literal.rb +5 -5
  139. data/lib/rubocop/cop/style/encoding.rb +1 -1
  140. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  141. data/lib/rubocop/cop/style/exponential_notation.rb +8 -10
  142. data/lib/rubocop/cop/style/float_division.rb +7 -10
  143. data/lib/rubocop/cop/style/format_string_token.rb +5 -5
  144. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  145. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +62 -0
  146. data/lib/rubocop/cop/style/hash_like_case.rb +76 -0
  147. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  148. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  149. data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -11
  150. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
  151. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  152. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  153. data/lib/rubocop/cop/style/missing_else.rb +1 -11
  154. data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
  155. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  156. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  157. data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
  158. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
  159. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  160. data/lib/rubocop/cop/style/next.rb +2 -2
  161. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  162. data/lib/rubocop/cop/style/numeric_predicate.rb +3 -4
  163. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -3
  164. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  165. data/lib/rubocop/cop/style/proc.rb +1 -1
  166. data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
  167. data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
  168. data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
  169. data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
  170. data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
  171. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +50 -0
  172. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  173. data/lib/rubocop/cop/style/redundant_parentheses.rb +8 -2
  174. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  175. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +4 -3
  176. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
  177. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  178. data/lib/rubocop/cop/style/redundant_sort.rb +3 -2
  179. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
  180. data/lib/rubocop/cop/style/sample.rb +1 -1
  181. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  182. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  183. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +3 -2
  184. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  185. data/lib/rubocop/cop/style/struct_inheritance.rb +23 -2
  186. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  187. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  188. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  189. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +9 -32
  190. data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
  191. data/lib/rubocop/cop/style/word_array.rb +1 -1
  192. data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
  193. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
  194. data/lib/rubocop/cop/team.rb +105 -81
  195. data/lib/rubocop/cop/util.rb +2 -2
  196. data/lib/rubocop/cop/utils/format_string.rb +19 -2
  197. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  198. data/lib/rubocop/file_finder.rb +12 -12
  199. data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -2
  200. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  201. data/lib/rubocop/name_similarity.rb +7 -3
  202. data/lib/rubocop/options.rb +15 -8
  203. data/lib/rubocop/path_util.rb +4 -19
  204. data/lib/rubocop/platform.rb +1 -1
  205. data/lib/rubocop/rake_task.rb +6 -9
  206. data/lib/rubocop/result_cache.rb +12 -8
  207. data/lib/rubocop/rspec/cop_helper.rb +4 -4
  208. data/lib/rubocop/rspec/expect_offense.rb +65 -21
  209. data/lib/rubocop/rspec/shared_contexts.rb +19 -16
  210. data/lib/rubocop/runner.rb +34 -33
  211. data/lib/rubocop/target_finder.rb +3 -3
  212. data/lib/rubocop/target_ruby.rb +2 -2
  213. data/lib/rubocop/version.rb +1 -1
  214. metadata +34 -9
  215. data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
@@ -30,6 +30,19 @@ module RuboCop
30
30
  add_offense(node)
31
31
  end
32
32
 
33
+ def autocorrect(node)
34
+ lambda do |corrector|
35
+ starting_token, ending_token = if node.source.include?('"')
36
+ ['%{', '}']
37
+ else
38
+ ['"', '"']
39
+ end
40
+
41
+ corrector.replace(node.loc.begin, starting_token)
42
+ corrector.replace(node.loc.end, ending_token)
43
+ end
44
+ end
45
+
33
46
  def heredoc?(node)
34
47
  node.loc.is_a?(Parser::Source::Map::Heredoc) ||
35
48
  (node.parent && heredoc?(node.parent))
@@ -30,6 +30,8 @@ module RuboCop
30
30
  # break if condition
31
31
  # end
32
32
  class LiteralAsCondition < Cop
33
+ include RangeHelp
34
+
33
35
  MSG = 'Literal `%<literal>s` appeared as a condition.'
34
36
 
35
37
  def on_if(node)
@@ -57,7 +59,8 @@ module RuboCop
57
59
  case_node.each_when do |when_node|
58
60
  next unless when_node.conditions.all?(&:literal?)
59
61
 
60
- add_offense(when_node)
62
+ range = when_conditions_range(when_node)
63
+ add_offense(when_node, location: range, message: message(range))
61
64
  end
62
65
  end
63
66
  end
@@ -129,6 +132,13 @@ module RuboCop
129
132
  node.condition
130
133
  end
131
134
  end
135
+
136
+ def when_conditions_range(when_node)
137
+ range_between(
138
+ when_node.conditions.first.source_range.begin_pos,
139
+ when_node.conditions.last.source_range.end_pos
140
+ )
141
+ end
132
142
  end
133
143
  end
134
144
  end
@@ -27,7 +27,14 @@ module RuboCop
27
27
  def on_regexp(node)
28
28
  return if contain_non_literal?(node)
29
29
 
30
- tree = Regexp::Parser.parse(node.content)
30
+ begin
31
+ tree = Regexp::Parser.parse(node.content)
32
+ # Returns if a regular expression that cannot be processed by regexp_parser gem.
33
+ # https://github.com/rubocop-hq/rubocop/issues/8083
34
+ rescue Regexp::Scanner::ScannerError
35
+ return
36
+ end
37
+
31
38
  return unless named_capture?(tree)
32
39
  return unless numbered_capture?(tree)
33
40
 
@@ -59,31 +59,25 @@ module RuboCop
59
59
  'Use `lambda` instead.'
60
60
 
61
61
  def on_def(node)
62
- find_nested_defs(node) do |nested_def_node|
63
- add_offense(nested_def_node)
64
- end
65
- end
66
- alias on_defs on_def
67
-
68
- private
62
+ subject, = *node
63
+ return if node.defs_type? && subject.lvar_type?
69
64
 
70
- def find_nested_defs(node, &block)
71
- node.each_child_node do |child|
72
- if child.def_type?
73
- yield child
74
- elsif child.defs_type?
75
- subject, = *child
76
- next if subject.lvar_type?
65
+ def_ancestor = node.each_ancestor(:def, :defs).first
66
+ return unless def_ancestor
77
67
 
78
- yield child
79
- elsif !scoping_method_call?(child)
80
- find_nested_defs(child, &block)
68
+ within_scoping_def =
69
+ node.each_ancestor(:block, :sclass).any? do |ancestor|
70
+ scoping_method_call?(ancestor)
81
71
  end
82
- end
72
+
73
+ add_offense(node) if def_ancestor && !within_scoping_def
83
74
  end
75
+ alias on_defs on_def
76
+
77
+ private
84
78
 
85
79
  def scoping_method_call?(child)
86
- eval_call?(child) || exec_call?(child) || child.sclass_type? ||
80
+ child.sclass_type? || eval_call?(child) || exec_call?(child) ||
87
81
  class_or_module_or_struct_new_call?(child)
88
82
  end
89
83
 
@@ -96,7 +90,7 @@ module RuboCop
96
90
  PATTERN
97
91
 
98
92
  def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
99
- (block (send (const nil? {:Class :Module :Struct}) :new ...) ...)
93
+ (block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
100
94
  PATTERN
101
95
  end
102
96
  end
@@ -35,6 +35,22 @@ module RuboCop
35
35
  # require file
36
36
  # end
37
37
  #
38
+ # @example
39
+ #
40
+ # # bad
41
+ # Dir['./lib/**/*.rb'].each(&method(:require))
42
+ #
43
+ # # good
44
+ # Dir['./lib/**/*.rb'].sort.each(&method(:require))
45
+ #
46
+ # @example
47
+ #
48
+ # # bad
49
+ # Dir.glob(Rails.root.join('test', '*.rb'), &method(:require))
50
+ #
51
+ # # good
52
+ # Dir.glob(Rails.root.join('test', '*.rb')).sort.each(&method(:require))
53
+ #
38
54
  class NonDeterministicRequireOrder < Cop
39
55
  MSG = 'Sort files before requiring them.'
40
56
 
@@ -49,7 +65,16 @@ module RuboCop
49
65
  end
50
66
  end
51
67
 
68
+ def on_block_pass(node)
69
+ return unless method_require?(node)
70
+ return unless unsorted_dir_pass?(node.parent)
71
+
72
+ add_offense(node.parent)
73
+ end
74
+
52
75
  def autocorrect(node)
76
+ return correct_block_pass(node) if node.arguments.last&.block_pass_type?
77
+
53
78
  if unsorted_dir_block?(node)
54
79
  lambda do |corrector|
55
80
  corrector.replace(node, "#{node.source}.sort.each")
@@ -64,16 +89,58 @@ module RuboCop
64
89
 
65
90
  private
66
91
 
92
+ def correct_block_pass(node)
93
+ if unsorted_dir_glob_pass?(node)
94
+ lambda do |corrector|
95
+ block_arg = node.arguments.last
96
+ corrector.remove(last_arg_range(node))
97
+ corrector.insert_after(node, ".sort.each(#{block_arg.source})")
98
+ end
99
+ else
100
+ lambda do |corrector|
101
+ corrector.replace(node.loc.selector, 'sort.each')
102
+ end
103
+ end
104
+ end
105
+
106
+ # Returns range of last argument including comma and whitespace.
107
+ #
108
+ # @return [Parser::Source::Range]
109
+ #
110
+ def last_arg_range(node)
111
+ node.arguments.last.source_range.with(
112
+ begin_pos: node.arguments[-2].source_range.end_pos
113
+ )
114
+ end
115
+
67
116
  def unsorted_dir_loop?(node)
68
117
  unsorted_dir_block?(node) || unsorted_dir_each?(node)
69
118
  end
70
119
 
120
+ def unsorted_dir_pass?(node)
121
+ unsorted_dir_glob_pass?(node) || unsorted_dir_each_pass?(node)
122
+ end
123
+
71
124
  def_node_matcher :unsorted_dir_block?, <<~PATTERN
72
- (send (const nil? :Dir) :glob ...)
125
+ (send (const {nil? cbase} :Dir) :glob ...)
73
126
  PATTERN
74
127
 
75
128
  def_node_matcher :unsorted_dir_each?, <<~PATTERN
76
- (send (send (const nil? :Dir) {:[] :glob} ...) :each)
129
+ (send (send (const {nil? cbase} :Dir) {:[] :glob} ...) :each)
130
+ PATTERN
131
+
132
+ def_node_matcher :method_require?, <<~PATTERN
133
+ (block-pass (send nil? :method (sym :require)))
134
+ PATTERN
135
+
136
+ def_node_matcher :unsorted_dir_glob_pass?, <<~PATTERN
137
+ (send (const {nil? cbase} :Dir) :glob ...
138
+ (block-pass (send nil? :method (sym :require))))
139
+ PATTERN
140
+
141
+ def_node_matcher :unsorted_dir_each_pass?, <<~PATTERN
142
+ (send (send (const {nil? cbase} :Dir) {:[] :glob} ...) :each
143
+ (block-pass (send nil? :method (sym :require))))
77
144
  PATTERN
78
145
 
79
146
  def_node_matcher :loop_variable, <<~PATTERN
@@ -48,17 +48,22 @@ module RuboCop
48
48
  return true
49
49
  end
50
50
 
51
- node.operator_method? || node.setter_method? || grouped_parentheses?(node)
51
+ node.operator_method? || node.setter_method? || chained_calls?(node) ||
52
+ operator_keyword?(node)
52
53
  end
53
54
 
54
55
  def first_argument_starts_with_left_parenthesis?(node)
55
56
  node.first_argument.source.start_with?('(')
56
57
  end
57
58
 
58
- def grouped_parentheses?(node)
59
+ def chained_calls?(node)
59
60
  first_argument = node.first_argument
61
+ first_argument.send_type? && (node.children.last&.children&.count || 0) > 1
62
+ end
60
63
 
61
- first_argument.send_type? && first_argument.receiver&.begin_type?
64
+ def operator_keyword?(node)
65
+ first_argument = node.first_argument
66
+ first_argument.operator_keyword?
62
67
  end
63
68
 
64
69
  def spaces_before_left_parenthesis(node)
@@ -62,7 +62,7 @@ module RuboCop
62
62
  # To avoid likely false positives (e.g. a single ' or ")
63
63
  next if literal.gsub(/[^[[:alnum:]]]/, '').empty?
64
64
 
65
- QUOTES_AND_COMMAS.any? { |pat| literal =~ pat }
65
+ QUOTES_AND_COMMAS.any? { |pat| literal.match?(pat) }
66
66
  end
67
67
  end
68
68
  end
@@ -61,7 +61,7 @@ module RuboCop
61
61
  end
62
62
 
63
63
  def non_alphanumeric_literal?(literal)
64
- literal !~ /[[:alnum:]]/
64
+ !/[[:alnum:]]/.match?(literal)
65
65
  end
66
66
  end
67
67
  end
@@ -31,12 +31,12 @@ module RuboCop
31
31
  MSG = 'Use `StandardError` over `Exception`.'
32
32
 
33
33
  def_node_matcher :exception?, <<~PATTERN
34
- (send nil? {:raise :fail} (const ${cbase nil?} :Exception) ... )
34
+ (send nil? {:raise :fail} $(const ${cbase nil?} :Exception) ... )
35
35
  PATTERN
36
36
 
37
37
  def_node_matcher :exception_new_with_message?, <<~PATTERN
38
38
  (send nil? {:raise :fail}
39
- (send (const ${cbase nil?} :Exception) :new ... ))
39
+ (send $(const ${cbase nil?} :Exception) :new ... ))
40
40
  PATTERN
41
41
 
42
42
  def on_send(node)
@@ -44,13 +44,21 @@ module RuboCop
44
44
  exception_new_with_message?(node, &check(node))
45
45
  end
46
46
 
47
+ def autocorrect(node)
48
+ lambda do |corrector|
49
+ exception_class = node.children.first&.cbase_type? ? '::StandardError' : 'StandardError'
50
+
51
+ corrector.replace(node, exception_class)
52
+ end
53
+ end
54
+
47
55
  private
48
56
 
49
57
  def check(node)
50
- lambda do |cbase|
58
+ lambda do |exception_class, cbase|
51
59
  return if cbase.nil? && implicit_namespace?(node)
52
60
 
53
- add_offense(node)
61
+ add_offense(exception_class)
54
62
  end
55
63
  end
56
64
 
@@ -25,7 +25,7 @@ module RuboCop
25
25
  'Perhaps you meant `rand(2)` or `rand`?'
26
26
 
27
27
  def_node_matcher :rand_one?, <<~PATTERN
28
- (send {(const nil? :Kernel) nil?} :rand {(int {-1 1}) (float {-1.0 1.0})})
28
+ (send {(const {nil? cbase} :Kernel) nil?} :rand {(int {-1 1}) (float {-1.0 1.0})})
29
29
  PATTERN
30
30
 
31
31
  def on_send(node)
@@ -25,34 +25,34 @@ module RuboCop
25
25
  #
26
26
  # # good
27
27
  # x += 1
28
- class RedundantCopDisableDirective < Cop
28
+ class RedundantCopDisableDirective < Base
29
29
  include RangeHelp
30
+ extend AutoCorrector
30
31
 
31
32
  COP_NAME = 'Lint/RedundantCopDisableDirective'
32
33
 
33
- def check(offenses, cop_disabled_line_ranges, comments)
34
+ attr_accessor :offenses_to_check
35
+
36
+ def initialize(config = nil, options = nil, offenses = nil)
37
+ @offenses_to_check = offenses
38
+ super(config, options)
39
+ end
40
+
41
+ def on_new_investigation
42
+ return unless offenses_to_check
43
+
44
+ comments = processed_source.comments
45
+ cop_disabled_line_ranges = processed_source.disabled_line_ranges
46
+
34
47
  redundant_cops = Hash.new { |h, k| h[k] = Set.new }
35
48
 
36
49
  each_redundant_disable(cop_disabled_line_ranges,
37
- offenses, comments) do |comment, redundant_cop|
50
+ offenses_to_check, comments) do |comment, redundant_cop|
38
51
  redundant_cops[comment].add(redundant_cop)
39
52
  end
40
53
 
41
54
  add_offenses(redundant_cops)
42
- end
43
-
44
- def autocorrect(args)
45
- lambda do |corrector|
46
- ranges, range = *args # Ranges are sorted by position.
47
-
48
- range = if range.source.start_with?('#')
49
- comment_range_with_surrounding_space(range)
50
- else
51
- directive_range_in_list(range, ranges)
52
- end
53
-
54
- corrector.remove(range)
55
- end
55
+ super
56
56
  end
57
57
 
58
58
  private
@@ -135,6 +135,7 @@ module RuboCop
135
135
  end
136
136
  end
137
137
 
138
+ # rubocop:todo Metrics/CyclomaticComplexity
138
139
  def find_redundant(comment, offenses, cop, line_range, next_line_range)
139
140
  if all_disabled?(comment)
140
141
  # If there's a disable all comment followed by a comment
@@ -152,9 +153,10 @@ module RuboCop
152
153
  cop if cop_offenses.none? { |o| line_range.cover?(o.line) }
153
154
  end
154
155
  end
156
+ # rubocop:enable Metrics/CyclomaticComplexity
155
157
 
156
158
  def all_disabled?(comment)
157
- comment.text =~ /rubocop\s*:\s*(?:disable|todo)\s+all\b/
159
+ /rubocop\s*:\s*(?:disable|todo)\s+all\b/.match?(comment.text)
158
160
  end
159
161
 
160
162
  def ignore_offense?(disabled_ranges, line_range)
@@ -185,10 +187,12 @@ module RuboCop
185
187
  cop_list = cops.sort.map { |c| describe(c) }
186
188
 
187
189
  add_offense(
188
- [[location], location],
189
- location: location,
190
+ location,
190
191
  message: "Unnecessary disabling of #{cop_list.join(', ')}."
191
- )
192
+ ) do |corrector|
193
+ range = comment_range_with_surrounding_space(location)
194
+ corrector.remove(range)
195
+ end
192
196
  end
193
197
 
194
198
  def add_offense_for_some_cops(comment, cops)
@@ -198,10 +202,12 @@ module RuboCop
198
202
 
199
203
  cop_ranges.each do |cop, range|
200
204
  add_offense(
201
- [ranges, range],
202
- location: range,
205
+ range,
203
206
  message: "Unnecessary disabling of #{describe(cop)}."
204
- )
207
+ ) do |corrector|
208
+ range = directive_range_in_list(range, ranges)
209
+ corrector.remove(range)
210
+ end
205
211
  end
206
212
  end
207
213
 
@@ -225,7 +231,7 @@ module RuboCop
225
231
  .drop_while { |r| !r.equal?(range) }
226
232
  .each_cons(2)
227
233
  .map { |range1, range2| range1.end.join(range2.begin).source }
228
- .all? { |intervening| intervening =~ /\A\s*,\s*\Z/ }
234
+ .all? { |intervening| /\A\s*,\s*\Z/.match?(intervening) }
229
235
  end
230
236
 
231
237
  def describe(cop)
@@ -60,8 +60,8 @@ module RuboCop
60
60
 
61
61
  def_node_matcher :array_new?, <<~PATTERN
62
62
  {
63
- $(send (const nil? :Array) :new ...)
64
- $(block (send (const nil? :Array) :new ...) ...)
63
+ $(send (const {nil? cbase} :Array) :new ...)
64
+ $(block (send (const {nil? cbase} :Array) :new ...) ...)
65
65
  }
66
66
  PATTERN
67
67
 
@@ -23,6 +23,12 @@ module RuboCop
23
23
  def on_match_current_line(node)
24
24
  add_offense(node)
25
25
  end
26
+
27
+ def autocorrect(node)
28
+ lambda do |corrector|
29
+ corrector.replace(node, "#{node.source} =~ $_")
30
+ end
31
+ end
26
32
  end
27
33
  end
28
34
  end