rubocop 0.82.0 → 0.86.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (280) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -19
  3. data/config/default.yml +111 -23
  4. data/lib/rubocop.rb +16 -59
  5. data/lib/rubocop/ast_aliases.rb +8 -0
  6. data/lib/rubocop/cli.rb +3 -3
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +2 -2
  8. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  9. data/lib/rubocop/cli/command/show_cops.rb +2 -6
  10. data/lib/rubocop/comment_config.rb +1 -1
  11. data/lib/rubocop/config.rb +6 -2
  12. data/lib/rubocop/config_loader.rb +19 -24
  13. data/lib/rubocop/config_loader_resolver.rb +45 -6
  14. data/lib/rubocop/config_store.rb +12 -2
  15. data/lib/rubocop/config_validator.rb +2 -1
  16. data/lib/rubocop/cop/autocorrect_logic.rb +1 -2
  17. data/lib/rubocop/cop/bundler/gem_comment.rb +70 -1
  18. data/lib/rubocop/cop/commissioner.rb +0 -21
  19. data/lib/rubocop/cop/cop.rb +36 -21
  20. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -6
  21. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +1 -1
  22. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -3
  23. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -3
  24. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
  25. data/lib/rubocop/cop/generator.rb +4 -3
  26. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  27. data/lib/rubocop/cop/ignored_node.rb +1 -3
  28. data/lib/rubocop/cop/layout/case_indentation.rb +3 -3
  29. data/lib/rubocop/cop/layout/class_structure.rb +19 -16
  30. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
  31. data/lib/rubocop/cop/layout/condition_position.rb +12 -2
  32. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  33. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -6
  34. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +128 -0
  35. data/lib/rubocop/cop/layout/end_of_line.rb +3 -3
  36. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -4
  37. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -3
  38. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
  39. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -3
  40. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  41. data/lib/rubocop/cop/layout/hash_alignment.rb +7 -7
  42. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +3 -7
  43. data/lib/rubocop/cop/layout/heredoc_indentation.rb +20 -103
  44. data/lib/rubocop/cop/layout/indentation_width.rb +1 -3
  45. data/lib/rubocop/cop/layout/line_length.rb +21 -18
  46. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  47. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -3
  48. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +13 -4
  49. data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
  50. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  51. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -3
  52. data/lib/rubocop/cop/layout/space_around_operators.rb +19 -2
  53. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  54. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -3
  55. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +2 -4
  56. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  57. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  58. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -3
  59. data/lib/rubocop/cop/layout/trailing_whitespace.rb +2 -2
  60. data/lib/rubocop/cop/lint/ambiguous_operator.rb +41 -0
  61. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +14 -0
  62. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  63. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +137 -0
  64. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -5
  65. data/lib/rubocop/cop/lint/empty_when.rb +29 -6
  66. data/lib/rubocop/cop/lint/ensure_return.rb +19 -2
  67. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -3
  68. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  69. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
  70. data/lib/rubocop/cop/lint/literal_as_condition.rb +10 -13
  71. data/lib/rubocop/cop/lint/loop.rb +1 -1
  72. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +69 -0
  73. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  74. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +7 -7
  75. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +33 -11
  76. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -4
  77. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  78. data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
  79. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +5 -8
  80. data/lib/rubocop/cop/lint/redundant_require_statement.rb +3 -3
  81. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  82. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  83. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  84. data/lib/rubocop/cop/lint/suppressed_exception.rb +11 -4
  85. data/lib/rubocop/cop/lint/syntax.rb +1 -3
  86. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  87. data/lib/rubocop/cop/lint/useless_access_modifier.rb +13 -3
  88. data/lib/rubocop/cop/lint/useless_assignment.rb +3 -2
  89. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +6 -1
  90. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  91. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  92. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  93. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  94. data/lib/rubocop/cop/migration/department_name.rb +7 -7
  95. data/lib/rubocop/cop/mixin/alignment.rb +1 -3
  96. data/lib/rubocop/cop/mixin/array_min_size.rb +1 -3
  97. data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -9
  98. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -3
  99. data/lib/rubocop/cop/mixin/configurable_formatting.rb +2 -4
  100. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  101. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  102. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  103. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  104. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +10 -1
  105. data/lib/rubocop/cop/mixin/hash_transform_method.rb +8 -1
  106. data/lib/rubocop/cop/mixin/ignored_pattern.rb +1 -1
  107. data/lib/rubocop/cop/mixin/line_length_help.rb +3 -2
  108. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  109. data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
  110. data/lib/rubocop/cop/mixin/parser_diagnostic.rb +1 -1
  111. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  112. data/lib/rubocop/cop/mixin/range_help.rb +1 -1
  113. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +43 -0
  114. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -23
  115. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  116. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  117. data/lib/rubocop/cop/mixin/target_ruby_version.rb +5 -1
  118. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
  119. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +3 -3
  120. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  121. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +11 -1
  122. data/lib/rubocop/cop/naming/file_name.rb +28 -17
  123. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  124. data/lib/rubocop/cop/naming/method_name.rb +1 -5
  125. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  126. data/lib/rubocop/cop/registry.rb +63 -10
  127. data/lib/rubocop/cop/severity.rb +1 -3
  128. data/lib/rubocop/cop/style/and_or.rb +2 -2
  129. data/lib/rubocop/cop/style/array_join.rb +1 -1
  130. data/lib/rubocop/cop/style/attr.rb +1 -3
  131. data/lib/rubocop/cop/style/bare_percent_literals.rb +2 -2
  132. data/lib/rubocop/cop/style/block_delimiters.rb +4 -12
  133. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  134. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  135. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  136. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  137. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -4
  138. data/lib/rubocop/cop/style/copyright.rb +5 -5
  139. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  140. data/lib/rubocop/cop/style/documentation.rb +2 -2
  141. data/lib/rubocop/cop/style/double_negation.rb +41 -4
  142. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  143. data/lib/rubocop/cop/style/empty_literal.rb +1 -3
  144. data/lib/rubocop/cop/style/empty_method.rb +1 -5
  145. data/lib/rubocop/cop/style/encoding.rb +1 -1
  146. data/lib/rubocop/cop/style/exponential_notation.rb +5 -5
  147. data/lib/rubocop/cop/style/format_string_token.rb +2 -3
  148. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -6
  149. data/lib/rubocop/cop/style/guard_clause.rb +25 -2
  150. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -1
  151. data/lib/rubocop/cop/style/hash_syntax.rb +16 -7
  152. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  153. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  154. data/lib/rubocop/cop/style/if_with_semicolon.rb +16 -0
  155. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  156. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  157. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  158. data/lib/rubocop/cop/style/lambda_call.rb +0 -20
  159. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +1 -3
  160. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +1 -3
  161. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  162. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  163. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  164. data/lib/rubocop/cop/style/multiline_when_then.rb +16 -1
  165. data/lib/rubocop/cop/style/negated_if.rb +3 -3
  166. data/lib/rubocop/cop/style/negated_unless.rb +3 -3
  167. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  168. data/lib/rubocop/cop/style/next.rb +2 -2
  169. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  170. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  171. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -6
  172. data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
  173. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  174. data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
  175. data/lib/rubocop/cop/style/redundant_fetch_block.rb +103 -0
  176. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -7
  177. data/lib/rubocop/cop/style/redundant_percent_q.rb +3 -3
  178. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +89 -0
  179. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +121 -0
  180. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  181. data/lib/rubocop/cop/style/safe_navigation.rb +2 -6
  182. data/lib/rubocop/cop/style/sample.rb +1 -1
  183. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  184. data/lib/rubocop/cop/style/slicing_with_range.rb +39 -0
  185. data/lib/rubocop/cop/style/special_global_vars.rb +2 -6
  186. data/lib/rubocop/cop/style/struct_inheritance.rb +21 -0
  187. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  188. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -4
  189. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +3 -3
  190. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +3 -3
  191. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +13 -13
  192. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +3 -3
  193. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -3
  194. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  195. data/lib/rubocop/cop/style/when_then.rb +1 -1
  196. data/lib/rubocop/cop/style/word_array.rb +1 -1
  197. data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
  198. data/lib/rubocop/cop/team.rb +69 -25
  199. data/lib/rubocop/cop/util.rb +27 -3
  200. data/lib/rubocop/cop/utils/format_string.rb +18 -0
  201. data/lib/rubocop/cop/variable_force.rb +3 -9
  202. data/lib/rubocop/cop/variable_force/assignment.rb +1 -0
  203. data/lib/rubocop/cop/variable_force/branch.rb +1 -3
  204. data/lib/rubocop/cop/variable_force/scope.rb +1 -0
  205. data/lib/rubocop/cop/variable_force/variable.rb +3 -6
  206. data/lib/rubocop/ext/processed_source.rb +18 -0
  207. data/lib/rubocop/formatter/base_formatter.rb +0 -4
  208. data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -13
  209. data/lib/rubocop/formatter/formatter_set.rb +2 -4
  210. data/lib/rubocop/formatter/junit_formatter.rb +14 -4
  211. data/lib/rubocop/magic_comment.rb +1 -1
  212. data/lib/rubocop/name_similarity.rb +18 -9
  213. data/lib/rubocop/options.rb +26 -11
  214. data/lib/rubocop/path_util.rb +2 -2
  215. data/lib/rubocop/platform.rb +1 -1
  216. data/lib/rubocop/remote_config.rb +1 -3
  217. data/lib/rubocop/result_cache.rb +5 -7
  218. data/lib/rubocop/rspec/cop_helper.rb +2 -25
  219. data/lib/rubocop/rspec/expect_offense.rb +58 -15
  220. data/lib/rubocop/rspec/shared_contexts.rb +54 -16
  221. data/lib/rubocop/runner.rb +20 -13
  222. data/lib/rubocop/target_finder.rb +8 -8
  223. data/lib/rubocop/target_ruby.rb +4 -1
  224. data/lib/rubocop/version.rb +5 -3
  225. metadata +51 -74
  226. data/lib/rubocop/ast/builder.rb +0 -85
  227. data/lib/rubocop/ast/node.rb +0 -637
  228. data/lib/rubocop/ast/node/alias_node.rb +0 -24
  229. data/lib/rubocop/ast/node/and_node.rb +0 -29
  230. data/lib/rubocop/ast/node/args_node.rb +0 -29
  231. data/lib/rubocop/ast/node/array_node.rb +0 -70
  232. data/lib/rubocop/ast/node/block_node.rb +0 -121
  233. data/lib/rubocop/ast/node/break_node.rb +0 -17
  234. data/lib/rubocop/ast/node/case_match_node.rb +0 -56
  235. data/lib/rubocop/ast/node/case_node.rb +0 -56
  236. data/lib/rubocop/ast/node/class_node.rb +0 -31
  237. data/lib/rubocop/ast/node/def_node.rb +0 -82
  238. data/lib/rubocop/ast/node/defined_node.rb +0 -17
  239. data/lib/rubocop/ast/node/ensure_node.rb +0 -17
  240. data/lib/rubocop/ast/node/float_node.rb +0 -12
  241. data/lib/rubocop/ast/node/for_node.rb +0 -53
  242. data/lib/rubocop/ast/node/forward_args_node.rb +0 -18
  243. data/lib/rubocop/ast/node/hash_node.rb +0 -109
  244. data/lib/rubocop/ast/node/if_node.rb +0 -175
  245. data/lib/rubocop/ast/node/int_node.rb +0 -12
  246. data/lib/rubocop/ast/node/keyword_splat_node.rb +0 -45
  247. data/lib/rubocop/ast/node/mixin/basic_literal_node.rb +0 -16
  248. data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +0 -43
  249. data/lib/rubocop/ast/node/mixin/collection_node.rb +0 -15
  250. data/lib/rubocop/ast/node/mixin/conditional_node.rb +0 -45
  251. data/lib/rubocop/ast/node/mixin/hash_element_node.rb +0 -125
  252. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +0 -269
  253. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +0 -114
  254. data/lib/rubocop/ast/node/mixin/modifier_node.rb +0 -17
  255. data/lib/rubocop/ast/node/mixin/numeric_node.rb +0 -21
  256. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +0 -61
  257. data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +0 -35
  258. data/lib/rubocop/ast/node/module_node.rb +0 -24
  259. data/lib/rubocop/ast/node/or_node.rb +0 -29
  260. data/lib/rubocop/ast/node/pair_node.rb +0 -63
  261. data/lib/rubocop/ast/node/range_node.rb +0 -18
  262. data/lib/rubocop/ast/node/regexp_node.rb +0 -33
  263. data/lib/rubocop/ast/node/resbody_node.rb +0 -24
  264. data/lib/rubocop/ast/node/retry_node.rb +0 -17
  265. data/lib/rubocop/ast/node/return_node.rb +0 -24
  266. data/lib/rubocop/ast/node/self_class_node.rb +0 -24
  267. data/lib/rubocop/ast/node/send_node.rb +0 -13
  268. data/lib/rubocop/ast/node/str_node.rb +0 -16
  269. data/lib/rubocop/ast/node/super_node.rb +0 -21
  270. data/lib/rubocop/ast/node/symbol_node.rb +0 -12
  271. data/lib/rubocop/ast/node/until_node.rb +0 -35
  272. data/lib/rubocop/ast/node/when_node.rb +0 -53
  273. data/lib/rubocop/ast/node/while_node.rb +0 -35
  274. data/lib/rubocop/ast/node/yield_node.rb +0 -21
  275. data/lib/rubocop/ast/sexp.rb +0 -16
  276. data/lib/rubocop/ast/traversal.rb +0 -202
  277. data/lib/rubocop/node_pattern.rb +0 -887
  278. data/lib/rubocop/processed_source.rb +0 -213
  279. data/lib/rubocop/string_util.rb +0 -14
  280. data/lib/rubocop/token.rb +0 -114
@@ -15,7 +15,7 @@ module RuboCop
15
15
  if style == :single_quotes
16
16
  !double_quotes_required?(src)
17
17
  else
18
- src !~ /" | \\[^'] | \#(@|\{)/x
18
+ !/" | \\[^'] | \#(@|\{)/x.match?(src)
19
19
  end
20
20
  end
21
21
  end
@@ -87,15 +87,15 @@ module RuboCop
87
87
  return false unless token
88
88
 
89
89
  if side == :left
90
- String(token.space_after?) =~ SINGLE_SPACE_REGEXP
90
+ SINGLE_SPACE_REGEXP.match?(String(token.space_after?))
91
91
  else
92
- String(token.space_before?) =~ SINGLE_SPACE_REGEXP
92
+ SINGLE_SPACE_REGEXP.match?(String(token.space_before?))
93
93
  end
94
94
  end
95
95
 
96
96
  def reposition(src, pos, step)
97
97
  offset = step == -1 ? -1 : 0
98
- pos += step while src[pos + offset] =~ SINGLE_SPACE_REGEXP
98
+ pos += step while SINGLE_SPACE_REGEXP.match?(src[pos + offset])
99
99
  pos.negative? ? 0 : pos
100
100
  end
101
101
 
@@ -4,12 +4,16 @@ module RuboCop
4
4
  module Cop
5
5
  # Common functionality for checking target ruby version.
6
6
  module TargetRubyVersion
7
+ def required_minimum_ruby_version
8
+ @minimum_target_ruby_version
9
+ end
10
+
7
11
  def minimum_target_ruby_version(version)
8
12
  @minimum_target_ruby_version = version
9
13
  end
10
14
 
11
15
  def support_target_ruby_version?(version)
12
- @minimum_target_ruby_version <= version
16
+ required_minimum_ruby_version <= version
13
17
  end
14
18
  end
15
19
  end
@@ -31,7 +31,7 @@ module RuboCop
31
31
  # If there is any heredoc in items, then match the comma succeeding
32
32
  # any whitespace (except newlines), otherwise allow for newlines
33
33
  comma_regex = any_heredoc?(items) ? /\A[^\S\n]*,/ : /\A\s*,/
34
- range.source =~ comma_regex && range.source.index(',')
34
+ comma_regex.match?(range.source) && range.source.index(',')
35
35
  end
36
36
 
37
37
  def check_comma(node, kind, comma_pos)
@@ -184,9 +184,7 @@ module RuboCop
184
184
  # ...
185
185
  # SOURCE
186
186
  # })
187
- if node.pair_type? || node.hash_type?
188
- return heredoc?(node.children.last)
189
- end
187
+ return heredoc?(node.children.last) if node.pair_type? || node.hash_type?
190
188
 
191
189
  false
192
190
  end
@@ -18,7 +18,7 @@ module RuboCop
18
18
  full_name = arg.children.first.to_s
19
19
  next if full_name == '_'
20
20
 
21
- name = full_name.gsub(/\A([_]+)/, '')
21
+ name = full_name.gsub(/\A(_+)/, '')
22
22
  next if (arg.restarg_type? || arg.kwrestarg_type?) && name.empty?
23
23
  next if allowed_names.include?(name)
24
24
 
@@ -44,7 +44,7 @@ module RuboCop
44
44
  end
45
45
 
46
46
  def uppercase?(name)
47
- name =~ /[[:upper:]]/
47
+ /[[:upper:]]/.match?(name)
48
48
  end
49
49
 
50
50
  def name_type(node)
@@ -62,7 +62,7 @@ module RuboCop
62
62
  end
63
63
 
64
64
  def ends_with_num?(name)
65
- name[-1] =~ /\d/
65
+ /\d/.match?(name[-1])
66
66
  end
67
67
 
68
68
  def length_offense(node, range)
@@ -35,7 +35,7 @@ module RuboCop
35
35
  def op_method?(name)
36
36
  return false if BLACKLISTED.include?(name)
37
37
 
38
- name !~ /\A\w/ || OP_LIKE_METHODS.include?(name)
38
+ !/\A\w/.match?(name) || OP_LIKE_METHODS.include?(name)
39
39
  end
40
40
  end
41
41
  end
@@ -6,6 +6,12 @@ module RuboCop
6
6
  # This cop checks for class and module names with
7
7
  # an underscore in them.
8
8
  #
9
+ # `AllowedNames` config takes an array of permitted names.
10
+ # Its default value is `['module_parent']`.
11
+ # These names can be full class/module names or part of the name.
12
+ # eg. Adding `my_class` to the `AllowedNames` config will allow names like
13
+ # `my_class`, `my_class::User`, `App::my_class`, `App::my_class::User`, etc.
14
+ #
9
15
  # @example
10
16
  # # bad
11
17
  # class My_Class
@@ -18,11 +24,15 @@ module RuboCop
18
24
  # end
19
25
  # module MyModule
20
26
  # end
27
+ # class module_parent::MyModule
28
+ # end
21
29
  class ClassAndModuleCamelCase < Cop
22
30
  MSG = 'Use CamelCase for classes and modules.'
23
31
 
24
32
  def on_class(node)
25
- return unless /_/.match?(node.loc.name.source)
33
+ allowed = /#{cop_config['AllowedNames'].join('|')}/
34
+ name = node.loc.name.source.gsub(allowed, '')
35
+ return unless /_/.match?(name)
26
36
 
27
37
  add_offense(node, location: :name)
28
38
  end
@@ -49,25 +49,36 @@ module RuboCop
49
49
 
50
50
  def for_bad_filename(file_path)
51
51
  basename = File.basename(file_path)
52
- msg = if filename_good?(basename)
53
- return if matching_definition?(file_path)
54
52
 
55
- no_definition_message(basename, file_path)
56
- else
57
- return if bad_filename_allowed?
53
+ if filename_good?(basename)
54
+ msg = perform_class_and_module_naming_checks(file_path, basename)
55
+ else
56
+ msg = other_message(basename) unless bad_filename_allowed?
57
+ end
58
58
 
59
- other_message(basename)
60
- end
59
+ yield source_range(processed_source.buffer, 1, 0), msg if msg
60
+ end
61
61
 
62
- yield source_range(processed_source.buffer, 1, 0), msg
62
+ def perform_class_and_module_naming_checks(file_path, basename)
63
+ return unless expect_matching_definition?
64
+
65
+ if check_definition_path_hierarchy? &&
66
+ !matching_definition?(file_path)
67
+ msg = no_definition_message(basename, file_path)
68
+ elsif !matching_class?(basename)
69
+ msg = no_definition_message(basename, basename)
70
+ end
71
+ msg
63
72
  end
64
73
 
65
74
  def matching_definition?(file_path)
66
- return true unless expect_matching_definition?
67
-
68
75
  find_class_or_module(processed_source.ast, to_namespace(file_path))
69
76
  end
70
77
 
78
+ def matching_class?(file_name)
79
+ find_class_or_module(processed_source.ast, to_namespace(file_name))
80
+ end
81
+
71
82
  def bad_filename_allowed?
72
83
  ignore_executable_scripts? && processed_source.start_with?('#!')
73
84
  end
@@ -94,6 +105,10 @@ module RuboCop
94
105
  cop_config['ExpectMatchingDefinition']
95
106
  end
96
107
 
108
+ def check_definition_path_hierarchy?
109
+ cop_config['CheckDefinitionPathHierarchy']
110
+ end
111
+
97
112
  def regex
98
113
  cop_config['Regex']
99
114
  end
@@ -104,14 +119,13 @@ module RuboCop
104
119
 
105
120
  def filename_good?(basename)
106
121
  basename = basename.sub(/^\./, '')
107
- basename = basename.sub(/\.[^\.]+$/, '')
122
+ basename = basename.sub(/\.[^.]+$/, '')
108
123
  # special handling for Action Pack Variants file names like
109
124
  # some_file.xlsx+mobile.axlsx
110
125
  basename = basename.sub('+', '_')
111
- basename =~ (regex || SNAKE_CASE)
126
+ basename.match?(regex || SNAKE_CASE)
112
127
  end
113
128
 
114
- # rubocop:disable Metrics/CyclomaticComplexity
115
129
  def find_class_or_module(node, namespace)
116
130
  return nil unless node
117
131
 
@@ -130,7 +144,6 @@ module RuboCop
130
144
 
131
145
  nil
132
146
  end
133
- # rubocop:enable Metrics/CyclomaticComplexity
134
147
 
135
148
  def match_namespace(node, namespace, expected)
136
149
  match_partial = partial_matcher!(expected)
@@ -153,9 +166,7 @@ module RuboCop
153
166
 
154
167
  namespace, name = *namespace
155
168
 
156
- if name == expected.last || match_acronym?(expected.last, name)
157
- expected.pop
158
- end
169
+ expected.pop if name == expected.last || match_acronym?(expected.last, name)
159
170
  end
160
171
 
161
172
  false
@@ -42,7 +42,7 @@ module RuboCop
42
42
  return false unless /\w/.match?(delimiters)
43
43
 
44
44
  forbidden_delimiters.none? do |forbidden_delimiter|
45
- delimiters =~ Regexp.new(forbidden_delimiter)
45
+ Regexp.new(forbidden_delimiter).match?(delimiters)
46
46
  end
47
47
  end
48
48
 
@@ -35,15 +35,11 @@ module RuboCop
35
35
 
36
36
  MSG = 'Use %<style>s for method names.'
37
37
 
38
- def_node_matcher :attr?, <<~PATTERN
39
- (send nil? ${:attr_reader :attr_writer :attr_accessor :attr} $...)
40
- PATTERN
41
-
42
38
  def_node_matcher :sym_name, '(sym $_name)'
43
39
  def_node_matcher :str_name, '(str $_name)'
44
40
 
45
41
  def on_send(node)
46
- return unless (attrs = attr?(node))
42
+ return unless (attrs = node.attribute_accessor?)
47
43
 
48
44
  attrs.last.each do |name_item|
49
45
  name = attr_name(name_item)
@@ -67,7 +67,7 @@ module RuboCop
67
67
  private
68
68
 
69
69
  def allowed_method_name?(method_name, prefix)
70
- !method_name.match(/^#{prefix}[^0-9]/) ||
70
+ !method_name.match?(/^#{prefix}[^0-9]/) ||
71
71
  method_name == expected_name(method_name, prefix) ||
72
72
  method_name.end_with?('=') ||
73
73
  allowed_methods.include?(method_name)
@@ -22,34 +22,42 @@ module RuboCop
22
22
 
23
23
  # Registry that tracks all cops by their badge and department.
24
24
  class Registry
25
+ include Enumerable
26
+
27
+ attr_reader :options
28
+
25
29
  def initialize(cops = [], options = {})
26
30
  @registry = {}
27
31
  @departments = {}
28
32
  @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] }
29
33
 
30
- cops.each { |cop| enlist(cop) }
34
+ @enrollment_queue = cops
31
35
  @options = options
32
36
  end
33
37
 
34
38
  def enlist(cop)
35
- @registry[cop.badge] = cop
36
- @departments[cop.department] ||= []
37
- @departments[cop.department] << cop
38
- @cops_by_cop_name[cop.cop_name] << cop
39
+ @enrollment_queue << cop
40
+ end
41
+
42
+ def dismiss(cop)
43
+ raise "Cop #{cop} could not be dismissed" unless @enrollment_queue.delete(cop)
39
44
  end
40
45
 
41
46
  # @return [Array<Symbol>] list of departments for current cops.
42
47
  def departments
48
+ clear_enrollment_queue
43
49
  @departments.keys
44
50
  end
45
51
 
46
52
  # @return [Registry] Cops for that specific department.
47
53
  def with_department(department)
54
+ clear_enrollment_queue
48
55
  with(@departments.fetch(department, []))
49
56
  end
50
57
 
51
58
  # @return [Registry] Cops not for a specific department.
52
59
  def without_department(department)
60
+ clear_enrollment_queue
53
61
  without_department = @departments.dup
54
62
  without_department.delete(department)
55
63
 
@@ -94,9 +102,7 @@ module RuboCop
94
102
  # @return [String] Qualified cop name
95
103
  def qualified_cop_name(name, path, shall_warn = true)
96
104
  badge = Badge.parse(name)
97
- if shall_warn && department_missing?(badge, name)
98
- print_warning(name, path)
99
- end
105
+ print_warning(name, path) if shall_warn && department_missing?(badge, name)
100
106
  return name if registered?(badge)
101
107
 
102
108
  potential_badges = qualify_badge(badge)
@@ -121,6 +127,7 @@ module RuboCop
121
127
  end
122
128
 
123
129
  def unqualified_cop_names
130
+ clear_enrollment_queue
124
131
  @unqualified_cop_names ||=
125
132
  Set.new(@cops_by_cop_name.keys.map { |qn| File.basename(qn) }) <<
126
133
  'RedundantCopDisableDirective'
@@ -128,18 +135,21 @@ module RuboCop
128
135
 
129
136
  # @return [Hash{String => Array<Class>}]
130
137
  def to_h
138
+ clear_enrollment_queue
131
139
  @cops_by_cop_name
132
140
  end
133
141
 
134
142
  def cops
143
+ clear_enrollment_queue
135
144
  @registry.values
136
145
  end
137
146
 
138
147
  def length
148
+ clear_enrollment_queue
139
149
  @registry.size
140
150
  end
141
151
 
142
- def enabled(config, only, only_safe = false)
152
+ def enabled(config, only = [], only_safe = false)
143
153
  select do |cop|
144
154
  only.include?(cop.cop_name) || enabled?(cop, config, only_safe)
145
155
  end
@@ -174,6 +184,7 @@ module RuboCop
174
184
  end
175
185
 
176
186
  def sort!
187
+ clear_enrollment_queue
177
188
  @registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }]
178
189
 
179
190
  self
@@ -190,16 +201,57 @@ module RuboCop
190
201
  # @param [String] cop_name
191
202
  # @return [Class, nil]
192
203
  def find_by_cop_name(cop_name)
193
- @cops_by_cop_name[cop_name].first
204
+ to_h[cop_name].first
205
+ end
206
+
207
+ @global = new
208
+
209
+ class << self
210
+ attr_reader :global
211
+ end
212
+
213
+ def self.all
214
+ global.without_department(:Test).cops
215
+ end
216
+
217
+ def self.qualified_cop_name(name, origin)
218
+ global.qualified_cop_name(name, origin)
219
+ end
220
+
221
+ # Changes momentarily the global registry
222
+ # Intended for testing purposes
223
+ def self.with_temporary_global(temp_global = global.dup)
224
+ previous = @global
225
+ @global = temp_global
226
+ yield
227
+ ensure
228
+ @global = previous
194
229
  end
195
230
 
196
231
  private
197
232
 
233
+ def initialize_copy(reg)
234
+ initialize(reg.cops, reg.options)
235
+ end
236
+
237
+ def clear_enrollment_queue
238
+ return if @enrollment_queue.empty?
239
+
240
+ @enrollment_queue.each do |cop|
241
+ @registry[cop.badge] = cop
242
+ @departments[cop.department] ||= []
243
+ @departments[cop.department] << cop
244
+ @cops_by_cop_name[cop.cop_name] << cop
245
+ end
246
+ @enrollment_queue = []
247
+ end
248
+
198
249
  def with(cops)
199
250
  self.class.new(cops)
200
251
  end
201
252
 
202
253
  def qualify_badge(badge)
254
+ clear_enrollment_queue
203
255
  @departments
204
256
  .map { |department, _| badge.with_department(department) }
205
257
  .select { |potential_badge| registered?(potential_badge) }
@@ -216,6 +268,7 @@ module RuboCop
216
268
  end
217
269
 
218
270
  def registered?(badge)
271
+ clear_enrollment_queue
219
272
  @registry.key?(badge)
220
273
  end
221
274
  end
@@ -31,9 +31,7 @@ module RuboCop
31
31
  # @api private
32
32
  def initialize(name_or_code)
33
33
  name = Severity.name_from_code(name_or_code)
34
- unless NAMES.include?(name)
35
- raise ArgumentError, "Unknown severity: #{name}"
36
- end
34
+ raise ArgumentError, "Unknown severity: #{name}" unless NAMES.include?(name)
37
35
 
38
36
  @name = name.freeze
39
37
  freeze
@@ -7,7 +7,7 @@ module RuboCop
7
7
  # `||` instead. It can be configured to check only in conditions or in
8
8
  # all contexts.
9
9
  #
10
- # @example EnforcedStyle: always (default)
10
+ # @example EnforcedStyle: always
11
11
  # # bad
12
12
  # foo.save and return
13
13
  #
@@ -22,7 +22,7 @@ module RuboCop
22
22
  # if foo && bar
23
23
  # end
24
24
  #
25
- # @example EnforcedStyle: conditionals
25
+ # @example EnforcedStyle: conditionals (default)
26
26
  # # bad
27
27
  # if foo and bar
28
28
  # end