rubocop 0.50.0 → 0.51.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (318) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -6
  3. data/config/default.yml +20 -11
  4. data/config/enabled.yml +44 -7
  5. data/lib/rubocop.rb +526 -514
  6. data/lib/rubocop/ast/node.rb +9 -9
  7. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +2 -2
  8. data/lib/rubocop/ast/node/send_node.rb +4 -0
  9. data/lib/rubocop/cached_data.rb +1 -6
  10. data/lib/rubocop/cli.rb +7 -6
  11. data/lib/rubocop/config.rb +58 -22
  12. data/lib/rubocop/config_loader.rb +37 -21
  13. data/lib/rubocop/config_loader_resolver.rb +3 -2
  14. data/lib/rubocop/cop/bundler/duplicated_gem.rb +5 -3
  15. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +4 -2
  16. data/lib/rubocop/cop/bundler/ordered_gems.rb +2 -53
  17. data/lib/rubocop/cop/commissioner.rb +1 -4
  18. data/lib/rubocop/cop/cop.rb +50 -17
  19. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +96 -0
  20. data/lib/rubocop/cop/generator.rb +8 -3
  21. data/lib/rubocop/cop/internal_affairs.rb +6 -5
  22. data/lib/rubocop/cop/internal_affairs/deprecated_positional_arguments.rb +81 -0
  23. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
  24. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +21 -11
  25. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +27 -14
  26. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +38 -16
  27. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -2
  28. data/lib/rubocop/cop/layout/block_end_newline.rb +1 -1
  29. data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
  30. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -1
  31. data/lib/rubocop/cop/layout/comment_indentation.rb +4 -2
  32. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  33. data/lib/rubocop/cop/layout/else_alignment.rb +5 -2
  34. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +1 -1
  35. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
  36. data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
  37. data/lib/rubocop/cop/layout/end_of_line.rb +1 -1
  38. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  39. data/lib/rubocop/cop/layout/indent_array.rb +1 -1
  40. data/lib/rubocop/cop/layout/indent_hash.rb +1 -1
  41. data/lib/rubocop/cop/layout/indent_heredoc.rb +2 -2
  42. data/lib/rubocop/cop/layout/indentation_width.rb +7 -5
  43. data/lib/rubocop/cop/layout/initial_indentation.rb +3 -2
  44. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +2 -2
  45. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  46. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -1
  47. data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
  48. data/lib/rubocop/cop/layout/space_after_method_name.rb +1 -1
  49. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -2
  50. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +1 -1
  51. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  52. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  53. data/lib/rubocop/cop/layout/space_before_block_braces.rb +4 -4
  54. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  55. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  56. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +6 -2
  57. data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +1 -1
  58. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
  59. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +5 -2
  60. data/lib/rubocop/cop/layout/space_inside_parens.rb +9 -0
  61. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  62. data/lib/rubocop/cop/layout/tab.rb +5 -2
  63. data/lib/rubocop/cop/layout/trailing_blank_lines.rb +4 -2
  64. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  65. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
  66. data/lib/rubocop/cop/lint/block_alignment.rb +1 -1
  67. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
  68. data/lib/rubocop/cop/lint/circular_argument_reference.rb +3 -14
  69. data/lib/rubocop/cop/lint/debugger.rb +7 -7
  70. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
  71. data/lib/rubocop/cop/lint/duplicate_methods.rb +5 -3
  72. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  73. data/lib/rubocop/cop/lint/empty_expression.rb +1 -1
  74. data/lib/rubocop/cop/lint/empty_when.rb +1 -1
  75. data/lib/rubocop/cop/lint/end_in_method.rb +1 -1
  76. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  77. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  78. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +4 -3
  79. data/lib/rubocop/cop/lint/interpolation_check.rb +2 -1
  80. data/lib/rubocop/cop/lint/{literal_in_condition.rb → literal_as_condition.rb} +19 -5
  81. data/lib/rubocop/cop/lint/loop.rb +1 -1
  82. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  83. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +2 -2
  84. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +3 -2
  85. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  86. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  87. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
  88. data/lib/rubocop/cop/lint/redundant_with_object.rb +81 -0
  89. data/lib/rubocop/cop/lint/regexp_as_condition.rb +29 -0
  90. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  91. data/lib/rubocop/cop/lint/rescue_type.rb +5 -3
  92. data/lib/rubocop/cop/lint/rescue_without_error_class.rb +6 -3
  93. data/lib/rubocop/cop/lint/return_in_void_context.rb +24 -13
  94. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
  95. data/lib/rubocop/cop/lint/script_permission.rb +1 -1
  96. data/lib/rubocop/cop/lint/shadowed_exception.rb +37 -10
  97. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  98. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +1 -1
  99. data/lib/rubocop/cop/lint/syntax.rb +8 -2
  100. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -1
  101. data/lib/rubocop/cop/lint/unified_integer.rb +2 -2
  102. data/lib/rubocop/cop/lint/unneeded_disable.rb +11 -4
  103. data/lib/rubocop/cop/lint/unneeded_require_statement.rb +50 -0
  104. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +2 -2
  105. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  106. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -1
  107. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -2
  108. data/lib/rubocop/cop/lint/uri_regexp.rb +3 -3
  109. data/lib/rubocop/cop/lint/useless_access_modifier.rb +9 -9
  110. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  111. data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
  112. data/lib/rubocop/cop/lint/useless_setter_call.rb +5 -1
  113. data/lib/rubocop/cop/lint/void.rb +10 -5
  114. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  115. data/lib/rubocop/cop/metrics/line_length.rb +4 -1
  116. data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -2
  117. data/lib/rubocop/cop/mixin/array_hash_indentation.rb +3 -2
  118. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +1 -1
  119. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  120. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
  121. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  122. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +7 -2
  123. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  124. data/lib/rubocop/cop/mixin/enforce_superclass.rb +1 -1
  125. data/lib/rubocop/cop/mixin/heredoc.rb +1 -1
  126. data/lib/rubocop/cop/mixin/method_complexity.rb +3 -4
  127. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  128. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +10 -4
  129. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +63 -0
  130. data/lib/rubocop/cop/mixin/parser_diagnostic.rb +4 -1
  131. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -1
  132. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -1
  133. data/lib/rubocop/cop/mixin/space_inside.rb +1 -1
  134. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  135. data/lib/rubocop/cop/mixin/trailing_comma.rb +5 -5
  136. data/lib/rubocop/cop/mixin/unused_argument.rb +43 -2
  137. data/lib/rubocop/cop/naming/accessor_method_name.rb +8 -7
  138. data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
  139. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  140. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  141. data/lib/rubocop/cop/naming/constant_name.rb +1 -1
  142. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  143. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
  144. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +3 -2
  145. data/lib/rubocop/cop/naming/predicate_name.rb +36 -5
  146. data/lib/rubocop/cop/performance/caller.rb +2 -2
  147. data/lib/rubocop/cop/performance/case_when_splat.rb +1 -1
  148. data/lib/rubocop/cop/performance/casecmp.rb +1 -1
  149. data/lib/rubocop/cop/performance/compare_with_block.rb +6 -2
  150. data/lib/rubocop/cop/performance/count.rb +3 -1
  151. data/lib/rubocop/cop/performance/detect.rb +4 -4
  152. data/lib/rubocop/cop/performance/double_start_end_with.rb +6 -9
  153. data/lib/rubocop/cop/performance/end_with.rb +1 -1
  154. data/lib/rubocop/cop/performance/flat_map.rb +3 -1
  155. data/lib/rubocop/cop/performance/hash_each_methods.rb +13 -10
  156. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +3 -1
  157. data/lib/rubocop/cop/performance/range_include.rb +1 -1
  158. data/lib/rubocop/cop/performance/redundant_block_call.rb +1 -1
  159. data/lib/rubocop/cop/performance/redundant_match.rb +6 -5
  160. data/lib/rubocop/cop/performance/redundant_merge.rb +1 -1
  161. data/lib/rubocop/cop/performance/redundant_sort_by.rb +4 -1
  162. data/lib/rubocop/cop/performance/regexp_match.rb +4 -4
  163. data/lib/rubocop/cop/performance/reverse_each.rb +1 -1
  164. data/lib/rubocop/cop/performance/sample.rb +1 -1
  165. data/lib/rubocop/cop/performance/size.rb +1 -1
  166. data/lib/rubocop/cop/performance/start_with.rb +1 -1
  167. data/lib/rubocop/cop/performance/string_replacement.rb +2 -2
  168. data/lib/rubocop/cop/performance/times_map.rb +3 -3
  169. data/lib/rubocop/cop/performance/unfreeze_string.rb +2 -2
  170. data/lib/rubocop/cop/performance/uri_default_parser.rb +2 -2
  171. data/lib/rubocop/cop/rails/action_filter.rb +1 -1
  172. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -2
  173. data/lib/rubocop/cop/rails/application_job.rb +1 -1
  174. data/lib/rubocop/cop/rails/application_record.rb +1 -1
  175. data/lib/rubocop/cop/rails/blank.rb +20 -17
  176. data/lib/rubocop/cop/rails/date.rb +7 -6
  177. data/lib/rubocop/cop/rails/delegate.rb +1 -1
  178. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  179. data/lib/rubocop/cop/rails/dynamic_find_by.rb +2 -2
  180. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -2
  181. data/lib/rubocop/cop/rails/exit.rb +1 -1
  182. data/lib/rubocop/cop/rails/file_path.rb +6 -8
  183. data/lib/rubocop/cop/rails/find_by.rb +2 -1
  184. data/lib/rubocop/cop/rails/find_each.rb +1 -1
  185. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  186. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +35 -11
  187. data/lib/rubocop/cop/rails/http_positional_arguments.rb +3 -2
  188. data/lib/rubocop/cop/rails/not_null_column.rb +5 -5
  189. data/lib/rubocop/cop/rails/output.rb +2 -2
  190. data/lib/rubocop/cop/rails/output_safety.rb +2 -1
  191. data/lib/rubocop/cop/rails/present.rb +14 -17
  192. data/lib/rubocop/cop/rails/read_write_attribute.rb +3 -3
  193. data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
  194. data/lib/rubocop/cop/rails/request_referer.rb +2 -2
  195. data/lib/rubocop/cop/rails/reversible_migration.rb +21 -19
  196. data/lib/rubocop/cop/rails/safe_navigation.rb +2 -2
  197. data/lib/rubocop/cop/rails/save_bang.rb +12 -12
  198. data/lib/rubocop/cop/rails/scope_args.rb +1 -1
  199. data/lib/rubocop/cop/rails/skips_model_validations.rb +2 -2
  200. data/lib/rubocop/cop/rails/time_zone.rb +3 -2
  201. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -1
  202. data/lib/rubocop/cop/rails/unknown_env.rb +63 -0
  203. data/lib/rubocop/cop/rails/validation.rb +1 -1
  204. data/lib/rubocop/cop/registry.rb +2 -1
  205. data/lib/rubocop/cop/security/eval.rb +2 -2
  206. data/lib/rubocop/cop/security/json_load.rb +2 -2
  207. data/lib/rubocop/cop/security/marshal_load.rb +3 -3
  208. data/lib/rubocop/cop/security/yaml_load.rb +2 -2
  209. data/lib/rubocop/cop/style/alias.rb +3 -3
  210. data/lib/rubocop/cop/style/and_or.rb +1 -1
  211. data/lib/rubocop/cop/style/array_join.rb +1 -1
  212. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  213. data/lib/rubocop/cop/style/attr.rb +1 -1
  214. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +2 -2
  215. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  216. data/lib/rubocop/cop/style/begin_block.rb +1 -1
  217. data/lib/rubocop/cop/style/block_delimiters.rb +1 -1
  218. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +22 -19
  219. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  220. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
  221. data/lib/rubocop/cop/style/class_check.rb +1 -1
  222. data/lib/rubocop/cop/style/class_methods.rb +2 -1
  223. data/lib/rubocop/cop/style/class_vars.rb +1 -1
  224. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  225. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  226. data/lib/rubocop/cop/style/comment_annotation.rb +5 -2
  227. data/lib/rubocop/cop/style/commented_keyword.rb +81 -0
  228. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  229. data/lib/rubocop/cop/style/copyright.rb +1 -1
  230. data/lib/rubocop/cop/style/date_time.rb +44 -0
  231. data/lib/rubocop/cop/style/def_with_parentheses.rb +1 -1
  232. data/lib/rubocop/cop/style/dir.rb +2 -6
  233. data/lib/rubocop/cop/style/documentation.rb +1 -1
  234. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  235. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  236. data/lib/rubocop/cop/style/each_with_object.rb +2 -1
  237. data/lib/rubocop/cop/style/empty_case_condition.rb +1 -1
  238. data/lib/rubocop/cop/style/empty_else.rb +2 -2
  239. data/lib/rubocop/cop/style/empty_literal.rb +9 -9
  240. data/lib/rubocop/cop/style/encoding.rb +7 -51
  241. data/lib/rubocop/cop/style/end_block.rb +1 -1
  242. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  243. data/lib/rubocop/cop/style/for.rb +6 -2
  244. data/lib/rubocop/cop/style/format_string.rb +4 -3
  245. data/lib/rubocop/cop/style/format_string_token.rb +2 -1
  246. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -3
  247. data/lib/rubocop/cop/style/global_vars.rb +1 -1
  248. data/lib/rubocop/cop/style/guard_clause.rb +2 -2
  249. data/lib/rubocop/cop/style/hash_syntax.rb +2 -3
  250. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  251. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -1
  252. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +2 -1
  253. data/lib/rubocop/cop/style/implicit_runtime_error.rb +2 -2
  254. data/lib/rubocop/cop/style/infinite_loop.rb +2 -2
  255. data/lib/rubocop/cop/style/inverse_methods.rb +4 -6
  256. data/lib/rubocop/cop/style/lambda.rb +3 -3
  257. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  258. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +1 -1
  259. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  260. data/lib/rubocop/cop/style/method_def_parentheses.rb +4 -2
  261. data/lib/rubocop/cop/style/min_max.rb +2 -1
  262. data/lib/rubocop/cop/style/mixin_usage.rb +73 -0
  263. data/lib/rubocop/cop/style/module_function.rb +2 -2
  264. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  265. data/lib/rubocop/cop/style/multiline_if_then.rb +2 -1
  266. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  267. data/lib/rubocop/cop/style/nested_modifier.rb +1 -1
  268. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +3 -1
  269. data/lib/rubocop/cop/style/next.rb +2 -1
  270. data/lib/rubocop/cop/style/nil_comparison.rb +2 -2
  271. data/lib/rubocop/cop/style/non_nil_check.rb +2 -2
  272. data/lib/rubocop/cop/style/not.rb +1 -1
  273. data/lib/rubocop/cop/style/numeric_predicate.rb +2 -2
  274. data/lib/rubocop/cop/style/option_hash.rb +0 -11
  275. data/lib/rubocop/cop/style/or_assignment.rb +1 -1
  276. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
  277. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  278. data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
  279. data/lib/rubocop/cop/style/proc.rb +1 -1
  280. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  281. data/lib/rubocop/cop/style/redundant_exception.rb +4 -4
  282. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  283. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  284. data/lib/rubocop/cop/style/regexp_literal.rb +2 -2
  285. data/lib/rubocop/cop/style/return_nil.rb +2 -7
  286. data/lib/rubocop/cop/style/safe_navigation.rb +27 -30
  287. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  288. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  289. data/lib/rubocop/cop/style/send.rb +1 -1
  290. data/lib/rubocop/cop/style/signal_exception.rb +4 -3
  291. data/lib/rubocop/cop/style/stderr_puts.rb +52 -0
  292. data/lib/rubocop/cop/style/string_literals.rb +4 -3
  293. data/lib/rubocop/cop/style/string_methods.rb +1 -1
  294. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -3
  295. data/lib/rubocop/cop/style/symbol_array.rb +1 -5
  296. data/lib/rubocop/cop/style/symbol_proc.rb +5 -5
  297. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -2
  298. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +46 -23
  299. data/lib/rubocop/cop/style/trivial_accessors.rb +3 -1
  300. data/lib/rubocop/cop/style/variable_interpolation.rb +0 -2
  301. data/lib/rubocop/cop/style/when_then.rb +1 -1
  302. data/lib/rubocop/cop/style/while_until_do.rb +2 -1
  303. data/lib/rubocop/cop/style/while_until_modifier.rb +2 -1
  304. data/lib/rubocop/cop/style/word_array.rb +2 -2
  305. data/lib/rubocop/cop/style/zero_length_predicate.rb +4 -4
  306. data/lib/rubocop/cop/util.rb +1 -12
  307. data/lib/rubocop/formatter/html_formatter.rb +1 -1
  308. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -2
  309. data/lib/rubocop/node_pattern.rb +1 -1
  310. data/lib/rubocop/options.rb +4 -0
  311. data/lib/rubocop/processed_source.rb +2 -8
  312. data/lib/rubocop/rake_task.rb +16 -23
  313. data/lib/rubocop/remote_config.rb +8 -0
  314. data/lib/rubocop/rspec/shared_contexts.rb +0 -8
  315. data/lib/rubocop/rspec/support.rb +5 -5
  316. data/lib/rubocop/version.rb +1 -1
  317. metadata +40 -30
  318. data/lib/rubocop/cop/lint/invalid_character_literal.rb +0 -41
@@ -26,6 +26,7 @@ module RuboCop
26
26
  # gem 'rspec'
27
27
  class OrderedGems < Cop
28
28
  include ConfigurableEnforcedStyle
29
+ include OrderedGemNode
29
30
 
30
31
  MSG = 'Gems should be sorted in an alphabetical order within their '\
31
32
  'section of the Gemfile. '\
@@ -46,66 +47,14 @@ module RuboCop
46
47
 
47
48
  private
48
49
 
49
- def case_insensitive_out_of_order?(string_a, string_b)
50
- string_a.downcase < string_b.downcase
51
- end
52
-
53
- def consecutive_lines(previous, current)
54
- first_line = get_source_range(current).first_line
55
- previous.source_range.last_line == first_line - 1
56
- end
57
-
58
- def register_offense(previous, current)
59
- add_offense(current, :expression,
60
- format(MSG, gem_name(current), gem_name(previous)))
61
- end
62
-
63
- def gem_name(declaration_node)
64
- declaration_node.first_argument.str_content
65
- end
66
-
67
- def autocorrect(node)
68
- previous = previous_declaration(node)
69
-
70
- current_range = declaration_with_comment(node)
71
- previous_range = declaration_with_comment(previous)
72
-
73
- lambda do |corrector|
74
- swap_range(corrector, current_range, previous_range)
75
- end
76
- end
77
-
78
- def declaration_with_comment(node)
79
- buffer = processed_source.buffer
80
- begin_pos = get_source_range(node).begin_pos
81
- end_line = buffer.line_for_position(node.loc.expression.end_pos)
82
- end_pos = buffer.line_range(end_line).end_pos
83
- Parser::Source::Range.new(buffer, begin_pos, end_pos)
84
- end
85
-
86
- def swap_range(corrector, range1, range2)
87
- src1 = range1.source
88
- src2 = range2.source
89
- corrector.replace(range1, src2)
90
- corrector.replace(range2, src1)
91
- end
92
-
93
50
  def previous_declaration(node)
94
51
  declarations = gem_declarations(processed_source.ast)
95
52
  node_index = declarations.find_index(node)
96
53
  declarations.to_a[node_index - 1]
97
54
  end
98
55
 
99
- def get_source_range(node)
100
- unless cop_config['TreatCommentsAsGroupSeparators']
101
- first_comment = processed_source.ast_with_comments[node].first
102
- return first_comment.loc.expression unless first_comment.nil?
103
- end
104
- node.source_range
105
- end
106
-
107
56
  def_node_search :gem_declarations, <<-PATTERN
108
- (:send nil :gem ...)
57
+ (:send nil? :gem ...)
109
58
  PATTERN
110
59
  end
111
60
  end
@@ -105,11 +105,9 @@ module RuboCop
105
105
  # Allow blind rescues here, since we're absorbing and packaging or
106
106
  # re-raising exceptions that can be raised from within the individual
107
107
  # cops' `#investigate` methods.
108
- #
109
- # rubocop:disable Lint/RescueWithoutErrorClass
110
108
  def with_cop_error_handling(cop, node = nil)
111
109
  yield
112
- rescue => e
110
+ rescue StandardError => e
113
111
  raise e if @options[:raise_error]
114
112
  if node
115
113
  line = node.loc.line
@@ -118,7 +116,6 @@ module RuboCop
118
116
  error = CopError.new(e, line, column)
119
117
  @errors[cop] << error
120
118
  end
121
- # rubocop:enable Lint/RescueWithoutErrorClass
122
119
  end
123
120
  end
124
121
  end
@@ -105,30 +105,23 @@ module RuboCop
105
105
  end
106
106
 
107
107
  def cop_config
108
- @cop_config ||= @config.for_cop(self)
108
+ # Use department configuration as basis, but let individual cop
109
+ # configuration override.
110
+ @cop_config ||= @config.for_cop(self.class.department.to_s)
111
+ .merge(@config.for_cop(self))
109
112
  end
110
113
 
111
114
  def message(_node = nil)
112
115
  self.class::MSG
113
116
  end
114
117
 
115
- # rubocop:disable Metrics/CyclomaticComplexity
116
- def add_offense(node, loc = :expression, message = nil, severity = nil)
117
- location = find_location(node, loc)
118
-
119
- return if duplicate_location?(location)
120
-
121
- severity = custom_severity || severity || default_severity
122
-
123
- message ||= message(node)
124
- message = annotate(message)
125
-
126
- status = enabled_line?(location.line) ? correct(node) : :disabled
127
-
128
- @offenses << Offense.new(severity, location, message, name, status)
129
- yield if block_given? && status != :disabled
118
+ def add_offense(node, *args, **kwargs, &block)
119
+ if args.any?
120
+ add_offense_deprecated(node, *args, &block)
121
+ else
122
+ add_offense_common(node, **kwargs, &block)
123
+ end
130
124
  end
131
- # rubocop:enable Metrics/CyclomaticComplexity
132
125
 
133
126
  def find_location(node, loc)
134
127
  # Location can be provided as a symbol, e.g.: `:keyword`
@@ -229,6 +222,46 @@ module RuboCop
229
222
  warn(Rainbow(message).red)
230
223
  end
231
224
  end
225
+
226
+ def add_offense_deprecated(node, loc = :expression, message = nil,
227
+ severity = nil, &block)
228
+
229
+ caller = caller_locations(2..2).first
230
+ path = "#{caller.path}:#{caller.lineno}"
231
+ warn_message = <<-RUBY.strip_indent
232
+ #{path}
233
+ Warning: The usage of positional location, message, and severity
234
+ parameters to Cop#add_offense is deprecated.
235
+ Please use keyword arguments instead.
236
+
237
+ The positional arguments version of Cop#add_offense will be removed in
238
+ RuboCop 0.52
239
+ RUBY
240
+
241
+ warn(Rainbow(warn_message).red)
242
+
243
+ add_offense_common(node, location: loc, message: message,
244
+ severity: severity, &block)
245
+ end
246
+
247
+ # rubocop:disable Metrics/CyclomaticComplexity
248
+ def add_offense_common(node, location: :expression, message: nil,
249
+ severity: nil)
250
+ loc = find_location(node, location)
251
+
252
+ return if duplicate_location?(loc)
253
+
254
+ severity = custom_severity || severity || default_severity
255
+
256
+ message ||= message(node)
257
+ message = annotate(message)
258
+
259
+ status = enabled_line?(loc.line) ? correct(node) : :disabled
260
+
261
+ @offenses << Offense.new(severity, loc, message, name, status)
262
+ yield if block_given? && status != :disabled
263
+ end
264
+ # rubocop:enable Metrics/CyclomaticComplexity
232
265
  end
233
266
  end
234
267
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Gemspec
6
+ # Dependencies in the gemspec should be alphabetically sorted.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # spec.add_dependency 'rubocop'
11
+ # spec.add_dependency 'rspec'
12
+ #
13
+ # # good
14
+ # spec.add_dependency 'rspec'
15
+ # spec.add_dependency 'rubocop'
16
+ #
17
+ # # good
18
+ # spec.add_dependency 'rubocop'
19
+ #
20
+ # spec.add_dependency 'rspec'
21
+ #
22
+ # # bad
23
+ # spec.add_development_dependency 'rubocop'
24
+ # spec.add_development_dependency 'rspec'
25
+ #
26
+ # # good
27
+ # spec.add_development_dependency 'rspec'
28
+ # spec.add_development_dependency 'rubocop'
29
+ #
30
+ # # good
31
+ # spec.add_development_dependency 'rubocop'
32
+ #
33
+ # spec.add_development_dependency 'rspec'
34
+ #
35
+ # # bad
36
+ # spec.add_runtime_dependency 'rubocop'
37
+ # spec.add_runtime_dependency 'rspec'
38
+ #
39
+ # # good
40
+ # spec.add_runtime_dependency 'rspec'
41
+ # spec.add_runtime_dependency 'rubocop'
42
+ #
43
+ # # good
44
+ # spec.add_runtime_dependency 'rubocop'
45
+ #
46
+ # spec.add_runtime_dependency 'rspec'
47
+ #
48
+ # # good only if TreatCommentsAsGroupSeparators is true
49
+ # # For code quality
50
+ # spec.add_dependency 'rubocop'
51
+ # # For tests
52
+ # spec.add_dependency 'rspec'
53
+ class OrderedDependencies < Cop
54
+ include ConfigurableEnforcedStyle
55
+ include OrderedGemNode
56
+
57
+ MSG = 'Dependencies should be sorted in an alphabetical order within '\
58
+ 'their section of the gemspec. '\
59
+ 'Dependency `%s` should appear before `%s`.'.freeze
60
+
61
+ def investigate(processed_source)
62
+ return if processed_source.ast.nil?
63
+ dependency_declarations(processed_source.ast)
64
+ .each_cons(2) do |previous, current|
65
+ next unless consecutive_lines(previous, current)
66
+ next unless case_insensitive_out_of_order?(
67
+ gem_name(current),
68
+ gem_name(previous)
69
+ )
70
+ unless get_dependency_name(previous) == get_dependency_name(current)
71
+ next
72
+ end
73
+
74
+ register_offense(previous, current)
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def previous_declaration(node)
81
+ declarations = dependency_declarations(processed_source.ast)
82
+ node_index = declarations.find_index(node)
83
+ declarations.to_a[node_index - 1]
84
+ end
85
+
86
+ def get_dependency_name(node)
87
+ node.method_name
88
+ end
89
+
90
+ def_node_search :dependency_declarations, <<-PATTERN
91
+ (send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} ...)
92
+ PATTERN
93
+ end
94
+ end
95
+ end
96
+ end
@@ -101,6 +101,8 @@ module RuboCop
101
101
  Files created:
102
102
  - #{source_path}
103
103
  - #{spec_path}
104
+ File modified:
105
+ - `require_relative '#{require_path}'` added into lib/rubocop.rb
104
106
 
105
107
  Do 3 steps:
106
108
  1. Add an entry to the "New features" section in CHANGELOG.md,
@@ -115,7 +117,10 @@ module RuboCop
115
117
  attr_reader :badge
116
118
 
117
119
  def write_unless_file_exists(path, contents)
118
- raise "#{path} already exists!" if File.exist?(path)
120
+ if File.exist?(path)
121
+ warn "rake new_cop: #{path} already exists!"
122
+ exit!
123
+ end
119
124
 
120
125
  dir = File.dirname(path)
121
126
  FileUtils.mkdir_p(dir) unless File.exist?(dir)
@@ -170,7 +175,7 @@ module RuboCop
170
175
  # It looks for other directives that require files in the same (cop)
171
176
  # namespace and injects the provided one in alpha
172
177
  class RequireFileInjector
173
- REQUIRE_PATH = /require ['"](.+)['"]/
178
+ REQUIRE_PATH = /require_relative ['"](.+)['"]/
174
179
 
175
180
  def initialize(require_path)
176
181
  @require_path = require_path
@@ -228,7 +233,7 @@ module RuboCop
228
233
  end
229
234
 
230
235
  def injectable_require_directive
231
- "require '#{require_path}'\n"
236
+ "require_relative '#{require_path}'\n"
232
237
  end
233
238
  end
234
239
  end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rubocop/cop/internal_affairs/node_type_predicate'
4
- require 'rubocop/cop/internal_affairs/offense_location_keyword'
5
- require 'rubocop/cop/internal_affairs/redundant_message_argument'
6
- require 'rubocop/cop/internal_affairs/redundant_location_argument'
7
- require 'rubocop/cop/internal_affairs/useless_message_assertion'
3
+ require_relative 'internal_affairs/node_type_predicate'
4
+ require_relative 'internal_affairs/offense_location_keyword'
5
+ require_relative 'internal_affairs/redundant_message_argument'
6
+ require_relative 'internal_affairs/redundant_location_argument'
7
+ require_relative 'internal_affairs/useless_message_assertion'
8
+ require_relative 'internal_affairs/deprecated_positional_arguments'
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Enforces use of keyword arguments for `#add_offense`.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # add_offense(node, :selector, 'message')
12
+ # add_offense(node, :selector, message: 'message')
13
+ #
14
+ # # good
15
+ # add_offense(node, location: :selector, message: 'message')
16
+ #
17
+ class DeprecatedPositionalArguments < Cop
18
+ MSG = 'Use of positional arguments on `#add_offense` is ' \
19
+ 'deprecated.'.freeze
20
+
21
+ ARGUMENTS = %i[location message severity].freeze
22
+
23
+ def on_send(node)
24
+ return unless node.method_name == :add_offense
25
+
26
+ positional_arguments(node) do |arguments|
27
+ location = range_between(
28
+ arguments.first.loc.expression.begin_pos,
29
+ arguments.last.loc.expression.end_pos
30
+ )
31
+
32
+ add_offense(node, location: location)
33
+ end
34
+ end
35
+
36
+ def autocorrect(node)
37
+ positional_arguments(node) do |arguments|
38
+ # Can't autocorrect splat.
39
+ next if arguments.any?(&:splat_type?)
40
+
41
+ lambda do |corrector|
42
+ arguments.zip(ARGUMENTS).each do |(arg, keyword)|
43
+ corrector.replace(arg.source_range, "#{keyword}: #{arg.source}")
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def positional_arguments(node)
52
+ arguments = extract_arguments(node)
53
+
54
+ positional_args =
55
+ case arguments
56
+ when :empty?.to_proc then []
57
+ # Keyword arguments only
58
+ when ->(args) { args.one? && args.first.hash_type? } then []
59
+ # Mixed style
60
+ when ->(args) { args.last.hash_type? } then arguments.drop_last(1)
61
+ # Positional arguments only
62
+ else arguments
63
+ end
64
+
65
+ yield positional_args if positional_args.any?
66
+ end
67
+
68
+ def extract_arguments(node)
69
+ node.arguments.butfirst.take_while do |arg|
70
+ # Filter out block argument
71
+ next false if arg.block_pass_type?
72
+ # Filter out kwsplat
73
+ next false if arg.hash_type? && arg.each_child_node(:kwsplat).any?
74
+ # Keep all others
75
+ true
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -24,7 +24,7 @@ module RuboCop
24
24
  node_type_check(node) do |_receiver, node_type|
25
25
  return unless Parser::Meta::NODE_TYPES.include?(node_type)
26
26
 
27
- add_offense(node, :expression, format(MSG, node_type))
27
+ add_offense(node, message: format(MSG, node_type))
28
28
  end
29
29
  end
30
30
 
@@ -9,31 +9,41 @@ module RuboCop
9
9
  # @example
10
10
  #
11
11
  # # bad
12
- # add_offense(node, node.loc.selector)
12
+ # add_offense(node, location: node.loc.selector)
13
13
  #
14
14
  # # good
15
- # add_offense(node, :selector)
15
+ # add_offense(node, location: :selector)
16
16
  class OffenseLocationKeyword < Cop
17
17
  MSG = 'Use `:%s` as the location argument to `#add_offense`.'.freeze
18
18
 
19
19
  def on_send(node)
20
- offense_location(node) do |location_argument, keyword|
21
- add_offense(node, location_argument.loc.expression,
22
- format(MSG, keyword))
20
+ node_type_check(node) do |node_arg, kwargs|
21
+ find_offending_argument(node_arg, kwargs) do |location, keyword|
22
+ add_offense(location, message: format(MSG, keyword))
23
+ end
23
24
  end
24
25
  end
25
26
 
26
27
  private
27
28
 
28
- def_node_matcher :offense_location, <<-PATTERN
29
- (send nil :add_offense _offender
30
- $(send (send _offender :loc) $_) ...)
29
+ def_node_matcher :node_type_check, <<-PATTERN
30
+ (send nil? :add_offense $_node $hash)
31
+ PATTERN
32
+
33
+ def_node_matcher :offending_location_argument, <<-PATTERN
34
+ (pair (sym :location) $(send (send $_node :loc) $_keyword))
31
35
  PATTERN
32
36
 
33
37
  def autocorrect(node)
34
- lambda do |corrector|
35
- offense_location(node) do |location_argument, keyword|
36
- corrector.replace(location_argument.loc.expression, ":#{keyword}")
38
+ (*, keyword) = offending_location_argument(node.parent)
39
+
40
+ ->(corrector) { corrector.replace(node.source_range, ":#{keyword}") }
41
+ end
42
+
43
+ def find_offending_argument(searched_node, kwargs)
44
+ kwargs.pairs.each do |pair|
45
+ offending_location_argument(pair) do |location, node, keyword|
46
+ yield(location, keyword) if searched_node == node
37
47
  end
38
48
  end
39
49
  end