rubocop 0.49.1 → 0.50.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (292) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/bin/rubocop +1 -1
  4. data/config/default.yml +160 -99
  5. data/config/disabled.yml +4 -5
  6. data/config/enabled.yml +149 -47
  7. data/lib/rubocop.rb +41 -14
  8. data/lib/rubocop/ast/builder.rb +4 -1
  9. data/lib/rubocop/ast/node.rb +36 -42
  10. data/lib/rubocop/ast/node/args_node.rb +1 -13
  11. data/lib/rubocop/ast/node/array_node.rb +9 -0
  12. data/lib/rubocop/ast/node/block_node.rb +9 -0
  13. data/lib/rubocop/ast/node/def_node.rb +71 -0
  14. data/lib/rubocop/ast/node/for_node.rb +8 -0
  15. data/lib/rubocop/ast/node/if_node.rb +10 -2
  16. data/lib/rubocop/ast/node/mixin/collection_node.rb +15 -0
  17. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +174 -0
  18. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +89 -0
  19. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +18 -31
  20. data/lib/rubocop/ast/node/regexp_node.rb +35 -0
  21. data/lib/rubocop/ast/node/send_node.rb +1 -154
  22. data/lib/rubocop/ast/node/super_node.rb +3 -24
  23. data/lib/rubocop/ast/node/yield_node.rb +21 -0
  24. data/lib/rubocop/ast/traversal.rb +6 -6
  25. data/lib/rubocop/cli.rb +7 -3
  26. data/lib/rubocop/config.rb +45 -8
  27. data/lib/rubocop/config_loader.rb +7 -5
  28. data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -3
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +64 -0
  30. data/lib/rubocop/cop/bundler/ordered_gems.rb +12 -12
  31. data/lib/rubocop/cop/commissioner.rb +8 -2
  32. data/lib/rubocop/cop/cop.rb +3 -1
  33. data/lib/rubocop/cop/generator.rb +94 -21
  34. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  35. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +14 -3
  36. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +43 -0
  37. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +46 -0
  38. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +49 -0
  39. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
  40. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +2 -3
  41. data/lib/rubocop/cop/layout/align_array.rb +2 -2
  42. data/lib/rubocop/cop/layout/align_hash.rb +2 -2
  43. data/lib/rubocop/cop/layout/align_parameters.rb +5 -11
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
  45. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  46. data/lib/rubocop/cop/layout/dot_position.rb +9 -0
  47. data/lib/rubocop/cop/layout/else_alignment.rb +30 -13
  48. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +4 -0
  49. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +20 -4
  50. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +3 -3
  51. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +3 -3
  52. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +3 -3
  53. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +5 -2
  54. data/lib/rubocop/cop/layout/indent_heredoc.rb +19 -24
  55. data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -2
  56. data/lib/rubocop/cop/layout/indentation_width.rb +12 -8
  57. data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -18
  58. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +20 -17
  59. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +3 -3
  60. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -0
  61. data/lib/rubocop/cop/layout/space_after_colon.rb +7 -0
  62. data/lib/rubocop/cop/layout/space_after_comma.rb +10 -0
  63. data/lib/rubocop/cop/layout/space_after_method_name.rb +5 -3
  64. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  65. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +13 -4
  66. data/lib/rubocop/cop/layout/space_around_keyword.rb +9 -5
  67. data/lib/rubocop/cop/layout/space_before_block_braces.rb +54 -5
  68. data/lib/rubocop/cop/layout/space_before_comment.rb +7 -0
  69. data/lib/rubocop/cop/layout/space_before_semicolon.rb +7 -0
  70. data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +1 -1
  71. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
  72. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
  73. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +8 -4
  74. data/lib/rubocop/cop/layout/tab.rb +1 -1
  75. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -2
  76. data/lib/rubocop/cop/lint/assignment_in_condition.rb +15 -1
  77. data/lib/rubocop/cop/lint/block_alignment.rb +15 -6
  78. data/lib/rubocop/cop/lint/boolean_symbol.rb +38 -0
  79. data/lib/rubocop/cop/lint/condition_position.rb +5 -1
  80. data/lib/rubocop/cop/lint/debugger.rb +16 -9
  81. data/lib/rubocop/cop/lint/def_end_alignment.rb +4 -4
  82. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +3 -3
  83. data/lib/rubocop/cop/lint/duplicate_methods.rb +73 -5
  84. data/lib/rubocop/cop/lint/duplicated_key.rb +1 -1
  85. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  86. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  87. data/lib/rubocop/cop/lint/empty_expression.rb +1 -1
  88. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  89. data/lib/rubocop/cop/lint/empty_when.rb +1 -1
  90. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  91. data/lib/rubocop/cop/lint/float_out_of_range.rb +5 -5
  92. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +35 -40
  93. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
  94. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +18 -13
  95. data/lib/rubocop/cop/lint/inherit_exception.rb +8 -7
  96. data/lib/rubocop/cop/lint/interpolation_check.rb +36 -0
  97. data/lib/rubocop/cop/lint/literal_in_condition.rb +3 -3
  98. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  99. data/lib/rubocop/cop/lint/multiple_compare.rb +1 -1
  100. data/lib/rubocop/cop/lint/nested_method_definition.rb +5 -7
  101. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
  102. data/lib/rubocop/cop/lint/percent_string_array.rb +3 -12
  103. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  104. data/lib/rubocop/cop/lint/rand_one.rb +7 -1
  105. data/lib/rubocop/cop/lint/redundant_with_index.rb +77 -0
  106. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  107. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  108. data/lib/rubocop/cop/lint/rescue_type.rb +13 -6
  109. data/lib/rubocop/cop/lint/rescue_without_error_class.rb +38 -0
  110. data/lib/rubocop/cop/lint/return_in_void_context.rb +63 -0
  111. data/lib/rubocop/cop/lint/script_permission.rb +6 -0
  112. data/lib/rubocop/cop/lint/syntax.rb +17 -20
  113. data/lib/rubocop/cop/lint/unified_integer.rb +3 -2
  114. data/lib/rubocop/cop/lint/unneeded_disable.rb +1 -1
  115. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  116. data/lib/rubocop/cop/lint/unreachable_code.rb +53 -8
  117. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +74 -0
  118. data/lib/rubocop/cop/lint/uri_regexp.rb +73 -0
  119. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -8
  120. data/lib/rubocop/cop/lint/useless_setter_call.rb +10 -11
  121. data/lib/rubocop/cop/lint/void.rb +29 -23
  122. data/lib/rubocop/cop/metrics/line_length.rb +2 -2
  123. data/lib/rubocop/cop/metrics/method_length.rb +8 -3
  124. data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
  125. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +1 -1
  126. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -2
  127. data/lib/rubocop/cop/mixin/first_element_line_break.rb +12 -3
  128. data/lib/rubocop/cop/mixin/heredoc.rb +28 -0
  129. data/lib/rubocop/cop/mixin/method_complexity.rb +9 -6
  130. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +68 -31
  131. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +18 -0
  132. data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -1
  133. data/lib/rubocop/cop/mixin/percent_array.rb +52 -0
  134. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  135. data/lib/rubocop/cop/{style → naming}/accessor_method_name.rb +11 -12
  136. data/lib/rubocop/cop/{style → naming}/ascii_identifiers.rb +1 -1
  137. data/lib/rubocop/cop/{style/op_method.rb → naming/binary_operator_parameter_name.rb} +2 -2
  138. data/lib/rubocop/cop/{style → naming}/class_and_module_camel_case.rb +1 -1
  139. data/lib/rubocop/cop/{style → naming}/constant_name.rb +1 -1
  140. data/lib/rubocop/cop/{style → naming}/file_name.rb +8 -4
  141. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +68 -0
  142. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +58 -0
  143. data/lib/rubocop/cop/{style → naming}/method_name.rb +1 -1
  144. data/lib/rubocop/cop/{style → naming}/predicate_name.rb +6 -7
  145. data/lib/rubocop/cop/{style → naming}/variable_name.rb +11 -15
  146. data/lib/rubocop/cop/{style → naming}/variable_number.rb +1 -1
  147. data/lib/rubocop/cop/performance/caller.rb +39 -11
  148. data/lib/rubocop/cop/performance/casecmp.rb +4 -4
  149. data/lib/rubocop/cop/performance/compare_with_block.rb +4 -4
  150. data/lib/rubocop/cop/performance/double_start_end_with.rb +4 -4
  151. data/lib/rubocop/cop/performance/end_with.rb +3 -3
  152. data/lib/rubocop/cop/performance/fixed_size.rb +1 -1
  153. data/lib/rubocop/cop/performance/hash_each_methods.rb +66 -25
  154. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
  155. data/lib/rubocop/cop/performance/range_include.rb +2 -2
  156. data/lib/rubocop/cop/performance/redundant_block_call.rb +6 -6
  157. data/lib/rubocop/cop/performance/redundant_match.rb +5 -5
  158. data/lib/rubocop/cop/performance/redundant_merge.rb +39 -23
  159. data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -2
  160. data/lib/rubocop/cop/performance/regexp_match.rb +13 -5
  161. data/lib/rubocop/cop/performance/size.rb +1 -1
  162. data/lib/rubocop/cop/performance/start_with.rb +3 -3
  163. data/lib/rubocop/cop/performance/times_map.rb +23 -12
  164. data/lib/rubocop/cop/performance/unfreeze_string.rb +50 -0
  165. data/lib/rubocop/cop/performance/uri_default_parser.rb +47 -0
  166. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
  167. data/lib/rubocop/cop/rails/delegate.rb +36 -7
  168. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  169. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -2
  170. data/lib/rubocop/cop/rails/file_path.rb +3 -4
  171. data/lib/rubocop/cop/rails/find_each.rb +1 -1
  172. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +48 -0
  173. data/lib/rubocop/cop/rails/http_positional_arguments.rb +5 -5
  174. data/lib/rubocop/cop/rails/not_null_column.rb +1 -1
  175. data/lib/rubocop/cop/rails/pluralization_grammar.rb +2 -2
  176. data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
  177. data/lib/rubocop/cop/rails/request_referer.rb +2 -2
  178. data/lib/rubocop/cop/rails/reversible_migration.rb +12 -12
  179. data/lib/rubocop/cop/rails/save_bang.rb +8 -6
  180. data/lib/rubocop/cop/rails/scope_args.rb +1 -1
  181. data/lib/rubocop/cop/security/eval.rb +2 -2
  182. data/lib/rubocop/cop/security/json_load.rb +2 -2
  183. data/lib/rubocop/cop/security/marshal_load.rb +2 -2
  184. data/lib/rubocop/cop/security/yaml_load.rb +2 -2
  185. data/lib/rubocop/cop/style/alias.rb +44 -20
  186. data/lib/rubocop/cop/style/and_or.rb +48 -34
  187. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +1 -1
  188. data/lib/rubocop/cop/style/block_comments.rb +3 -1
  189. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  190. data/lib/rubocop/cop/style/command_literal.rb +20 -9
  191. data/lib/rubocop/cop/style/conditional_assignment.rb +30 -28
  192. data/lib/rubocop/cop/style/copyright.rb +10 -10
  193. data/lib/rubocop/cop/style/def_with_parentheses.rb +6 -5
  194. data/lib/rubocop/cop/style/dir.rb +52 -0
  195. data/lib/rubocop/cop/style/documentation_method.rb +2 -6
  196. data/lib/rubocop/cop/style/empty_case_condition.rb +1 -1
  197. data/lib/rubocop/cop/style/empty_else.rb +3 -2
  198. data/lib/rubocop/cop/style/empty_literal.rb +1 -2
  199. data/lib/rubocop/cop/style/empty_method.rb +27 -17
  200. data/lib/rubocop/cop/style/flip_flop.rb +2 -2
  201. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  202. data/lib/rubocop/cop/style/guard_clause.rb +4 -2
  203. data/lib/rubocop/cop/style/hash_syntax.rb +10 -10
  204. data/lib/rubocop/cop/style/identical_conditional_branches.rb +5 -1
  205. data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -1
  206. data/lib/rubocop/cop/style/implicit_runtime_error.rb +4 -3
  207. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  208. data/lib/rubocop/cop/style/inverse_methods.rb +20 -8
  209. data/lib/rubocop/cop/style/lambda.rb +19 -9
  210. data/lib/rubocop/cop/style/lambda_call.rb +22 -1
  211. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +4 -20
  212. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -4
  213. data/lib/rubocop/cop/style/method_def_parentheses.rb +18 -26
  214. data/lib/rubocop/cop/style/method_missing.rb +5 -18
  215. data/lib/rubocop/cop/style/min_max.rb +67 -0
  216. data/lib/rubocop/cop/style/missing_else.rb +16 -3
  217. data/lib/rubocop/cop/style/mixin_grouping.rb +2 -2
  218. data/lib/rubocop/cop/style/module_function.rb +8 -4
  219. data/lib/rubocop/cop/style/multiline_if_modifier.rb +5 -1
  220. data/lib/rubocop/cop/style/multiline_memoization.rb +25 -3
  221. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  222. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  223. data/lib/rubocop/cop/style/mutable_constant.rb +2 -6
  224. data/lib/rubocop/cop/style/negated_if.rb +8 -4
  225. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +8 -8
  226. data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
  227. data/lib/rubocop/cop/style/non_nil_check.rb +14 -14
  228. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +6 -2
  229. data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
  230. data/lib/rubocop/cop/style/numeric_predicate.rb +8 -4
  231. data/lib/rubocop/cop/style/one_line_conditional.rb +8 -3
  232. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  233. data/lib/rubocop/cop/style/optional_arguments.rb +1 -2
  234. data/lib/rubocop/cop/style/or_assignment.rb +88 -0
  235. data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
  236. data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -11
  237. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  238. data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
  239. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  240. data/lib/rubocop/cop/style/proc.rb +1 -1
  241. data/lib/rubocop/cop/style/raise_args.rb +16 -17
  242. data/lib/rubocop/cop/style/redundant_begin.rb +6 -5
  243. data/lib/rubocop/cop/style/redundant_conditional.rb +95 -0
  244. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  245. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -11
  246. data/lib/rubocop/cop/style/redundant_return.rb +23 -11
  247. data/lib/rubocop/cop/style/redundant_self.rb +18 -9
  248. data/lib/rubocop/cop/style/regexp_literal.rb +12 -4
  249. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  250. data/lib/rubocop/cop/style/return_nil.rb +98 -0
  251. data/lib/rubocop/cop/style/safe_navigation.rb +80 -43
  252. data/lib/rubocop/cop/style/single_line_block_params.rb +14 -13
  253. data/lib/rubocop/cop/style/single_line_methods.rb +9 -13
  254. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  255. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +17 -39
  256. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +22 -1
  257. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  258. data/lib/rubocop/cop/style/symbol_array.rb +5 -25
  259. data/lib/rubocop/cop/style/symbol_literal.rb +1 -1
  260. data/lib/rubocop/cop/style/symbol_proc.rb +3 -18
  261. data/lib/rubocop/cop/style/ternary_parentheses.rb +14 -10
  262. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +28 -9
  263. data/lib/rubocop/cop/style/trivial_accessors.rb +39 -56
  264. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  265. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
  266. data/lib/rubocop/cop/style/unneeded_interpolation.rb +1 -1
  267. data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
  268. data/lib/rubocop/cop/style/variable_interpolation.rb +8 -3
  269. data/lib/rubocop/cop/style/word_array.rb +7 -24
  270. data/lib/rubocop/cop/style/yoda_condition.rb +49 -14
  271. data/lib/rubocop/cop/style/zero_length_predicate.rb +25 -18
  272. data/lib/rubocop/cop/team.rb +16 -8
  273. data/lib/rubocop/cop/util.rb +11 -0
  274. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  275. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  276. data/lib/rubocop/formatter/offense_count_formatter.rb +2 -0
  277. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  278. data/lib/rubocop/formatter/tap_formatter.rb +71 -0
  279. data/lib/rubocop/formatter/worst_offenders_formatter.rb +2 -0
  280. data/lib/rubocop/node_pattern.rb +44 -26
  281. data/lib/rubocop/options.rb +1 -0
  282. data/lib/rubocop/processed_source.rb +3 -1
  283. data/lib/rubocop/remote_config.rb +5 -1
  284. data/lib/rubocop/result_cache.rb +1 -0
  285. data/lib/rubocop/rspec/cop_helper.rb +10 -10
  286. data/lib/rubocop/rspec/expect_offense.rb +6 -8
  287. data/lib/rubocop/rspec/shared_examples.rb +8 -8
  288. data/lib/rubocop/string_util.rb +2 -0
  289. data/lib/rubocop/version.rb +1 -1
  290. metadata +51 -18
  291. data/lib/rubocop/cop/mixin/access_modifier_node.rb +0 -41
  292. data/lib/rubocop/cop/mixin/on_method_def.rb +0 -44
@@ -3,60 +3,72 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # This cop checks for uses of *and* and *or*.
6
+ # This cop checks for uses of `and` and `or`, and suggests using `&&` and
7
+ # `|| instead`. It can be configured to check only in conditions, or in
8
+ # all contexts.
9
+ #
10
+ # @example
11
+ #
12
+ # # EnforcedStyle: always (default)
13
+ #
14
+ # # good
15
+ # foo.save && return
16
+ # if foo && bar
17
+ #
18
+ # # bad
19
+ # foo.save and return
20
+ # if foo and bar
21
+ #
22
+ # @example
23
+ #
24
+ # # EnforcedStyle: conditionals
25
+ #
26
+ # # good
27
+ # foo.save && return
28
+ # foo.save and return
29
+ # if foo && bar
30
+ #
31
+ # # bad
32
+ # if foo and bar
7
33
  class AndOr < Cop
8
34
  include ConfigurableEnforcedStyle
9
35
 
10
36
  MSG = 'Use `%s` instead of `%s`.'.freeze
11
37
 
12
- OPS = { 'and' => '&&', 'or' => '||' }.freeze
13
-
14
38
  def on_and(node)
15
- process_logical_op(node) if style == :always
16
- end
17
-
18
- def on_or(node)
19
- process_logical_op(node) if style == :always
39
+ process_logical_operator(node) if style == :always
20
40
  end
41
+ alias on_or on_and
21
42
 
22
43
  def on_if(node)
23
44
  on_conditionals(node) if style == :conditionals
24
45
  end
25
-
26
- def on_while(node)
27
- on_conditionals(node) if style == :conditionals
28
- end
29
-
30
- def on_while_post(node)
31
- on_conditionals(node) if style == :conditionals
32
- end
33
-
34
- def on_until(node)
35
- on_conditionals(node) if style == :conditionals
36
- end
37
-
38
- def on_until_post(node)
39
- on_conditionals(node) if style == :conditionals
40
- end
46
+ alias on_while on_if
47
+ alias on_while_post on_if
48
+ alias on_until on_if
49
+ alias on_until_post on_if
41
50
 
42
51
  private
43
52
 
44
53
  def on_conditionals(node)
45
54
  node.condition.each_node(*LOGICAL_OPERATOR_NODES) do |logical_node|
46
- process_logical_op(logical_node)
55
+ process_logical_operator(logical_node)
47
56
  end
48
57
  end
49
58
 
50
- def process_logical_op(node)
59
+ def process_logical_operator(node)
51
60
  return if node.logical_operator?
52
61
 
53
- add_offense(node, :operator,
54
- format(MSG, node.alternate_operator, node.operator))
62
+ add_offense(node, :operator)
63
+ end
64
+
65
+ def message(node)
66
+ format(MSG, node.alternate_operator, node.operator)
55
67
  end
56
68
 
57
69
  def autocorrect(node)
58
70
  lambda do |corrector|
59
- [*node].each do |expr|
71
+ node.each_child_node do |expr|
60
72
  if expr.send_type?
61
73
  correct_send(expr, corrector)
62
74
  elsif expr.return_type?
@@ -73,15 +85,17 @@ module RuboCop
73
85
  def correct_send(node, corrector)
74
86
  return correct_not(node, node.receiver, corrector) if node.method?(:!)
75
87
  return correct_setter(node, corrector) if node.setter_method?
88
+ return correct_other(node, corrector) if node.comparison_method?
89
+
76
90
  return unless correctable_send?(node)
77
91
 
78
- corrector.replace(whitespace_before_arg(node), '('.freeze)
79
- corrector.insert_after(node.last_argument.source_range, ')'.freeze)
92
+ corrector.replace(whitespace_before_arg(node), '(')
93
+ corrector.insert_after(node.last_argument.source_range, ')')
80
94
  end
81
95
 
82
96
  def correct_setter(node, corrector)
83
- corrector.insert_before(node.receiver.source_range, '('.freeze)
84
- corrector.insert_after(node.last_argument.source_range, ')'.freeze)
97
+ corrector.insert_before(node.receiver.source_range, '(')
98
+ corrector.insert_after(node.last_argument.source_range, ')')
85
99
  end
86
100
 
87
101
  # ! is a special case:
@@ -108,7 +122,7 @@ module RuboCop
108
122
  end
109
123
 
110
124
  def correctable_send?(node)
111
- !node.parenthesized? && !node.method?(:[]) && node.arguments?
125
+ !node.parenthesized? && node.arguments? && !node.method?(:[])
112
126
  end
113
127
 
114
128
  def whitespace_before_arg(node)
@@ -13,7 +13,7 @@ module RuboCop
13
13
  # f = File.open('file')
14
14
  #
15
15
  # # good
16
- # f = File.open('file') do
16
+ # File.open('file') do |f|
17
17
  # ...
18
18
  # end
19
19
  class AutoResourceCleanup < Cop
@@ -11,7 +11,9 @@ module RuboCop
11
11
 
12
12
  def investigate(processed_source)
13
13
  processed_source.comments.each do |comment|
14
- add_offense(comment, :expression) if comment.document?
14
+ next unless comment.document?
15
+
16
+ add_offense(comment)
15
17
  end
16
18
  end
17
19
 
@@ -111,6 +111,7 @@ module RuboCop
111
111
  range.source_buffer.source[range.begin_pos + length, 1] =~ /\s/
112
112
  end
113
113
 
114
+ # rubocop:disable Metrics/CyclomaticComplexity
114
115
  def get_blocks(node, &block)
115
116
  case node.type
116
117
  when :block
@@ -126,8 +127,8 @@ module RuboCop
126
127
  when :pair
127
128
  node.each_child_node { |child| get_blocks(child, &block) }
128
129
  end
129
- nil
130
130
  end
131
+ # rubocop:enable Metrics/CyclimaticComplexity
131
132
 
132
133
  def proper_block_style?(node)
133
134
  return true if ignored_method?(node.method_name)
@@ -47,26 +47,37 @@ module RuboCop
47
47
  def check_backtick_literal(node)
48
48
  return if allowed_backtick_literal?(node)
49
49
 
50
- add_offense(node, :expression, MSG_USE_PERCENT_X)
50
+ add_offense(node)
51
51
  end
52
52
 
53
53
  def check_percent_x_literal(node)
54
54
  return if allowed_percent_x_literal?(node)
55
55
 
56
- add_offense(node, :expression, MSG_USE_BACKTICKS)
56
+ add_offense(node)
57
+ end
58
+
59
+ def message(node)
60
+ backtick_literal?(node) ? MSG_USE_PERCENT_X : MSG_USE_BACKTICKS
57
61
  end
58
62
 
59
63
  def allowed_backtick_literal?(node)
60
- style == :backticks && !contains_disallowed_backtick?(node) ||
61
- style == :mixed && node.single_line? &&
62
- !contains_disallowed_backtick?(node)
64
+ case style
65
+ when :backticks
66
+ !contains_disallowed_backtick?(node)
67
+ when :mixed
68
+ node.single_line? && !contains_disallowed_backtick?(node)
69
+ end
63
70
  end
64
71
 
65
72
  def allowed_percent_x_literal?(node)
66
- style == :backticks && contains_disallowed_backtick?(node) ||
67
- style == :percent_x ||
68
- style == :mixed && node.multiline? ||
69
- style == :mixed && contains_disallowed_backtick?(node)
73
+ case style
74
+ when :backticks
75
+ contains_disallowed_backtick?(node)
76
+ when :mixed
77
+ node.multiline? || contains_disallowed_backtick?(node)
78
+ when :percent_x
79
+ true
80
+ end
70
81
  end
71
82
 
72
83
  def contains_disallowed_backtick?(node)
@@ -6,6 +6,8 @@ module RuboCop
6
6
  # Helper module to provide common methods to classes needed for the
7
7
  # ConditionalAssignment Cop.
8
8
  module ConditionalAssignmentHelper
9
+ extend NodePattern::Macros
10
+
9
11
  EQUAL = '='.freeze
10
12
  END_ALIGNMENT = 'Lint/EndAlignment'.freeze
11
13
  ALIGN_WITH = 'EnforcedStyleAlignWith'.freeze
@@ -31,6 +33,7 @@ module RuboCop
31
33
  branch.begin_type? ? [*branch].last : branch
32
34
  end
33
35
 
36
+ # rubocop:disable Metrics/AbcSize
34
37
  def lhs(node)
35
38
  case node.type
36
39
  when :send
@@ -47,6 +50,7 @@ module RuboCop
47
50
  node.source
48
51
  end
49
52
  end
53
+ # rubocop:enable Metrics/AbcSize
50
54
 
51
55
  def indent(cop, source)
52
56
  conf = cop.config.for_cop(END_ALIGNMENT)
@@ -219,12 +223,12 @@ module RuboCop
219
223
 
220
224
  # The shovel operator `<<` does not have its own type. It is a `send`
221
225
  # type.
222
- def_node_matcher :assignment_type?, <<-END
226
+ def_node_matcher :assignment_type?, <<-PATTERN
223
227
  {
224
228
  #{ASSIGNMENT_TYPES.join(' ')}
225
229
  (send _recv {:[]= :<< :=~ :!~ :<=> #end_with_eq?} ...)
226
230
  }
227
- END
231
+ PATTERN
228
232
 
229
233
  ASSIGNMENT_TYPES.each do |type|
230
234
  define_method "on_#{type}" do |node|
@@ -267,21 +271,27 @@ module RuboCop
267
271
  private
268
272
 
269
273
  def check_assignment_to_condition(node)
270
- return unless style == :assign_inside_condition
271
- return unless assignment_rhs_exist?(node)
274
+ return unless candidate_node?(node)
275
+
272
276
  ignore_node(node)
273
277
 
274
278
  assignment = assignment_node(node)
275
- return unless condition?(assignment)
276
- return if allowed_ternary?(assignment)
279
+ return unless candidate_condition?(assignment)
277
280
 
278
281
  _condition, *branches, else_branch = *assignment
279
- return unless else_branch # empty else
282
+
283
+ return unless else_branch
280
284
  return if allowed_single_line?([*branches, else_branch])
281
285
 
282
286
  add_offense(node, :expression, ASSIGN_TO_CONDITION_MSG)
283
287
  end
284
288
 
289
+ def candidate_node?(node)
290
+ style == :assign_inside_condition && assignment_rhs_exist?(node)
291
+ end
292
+
293
+ def_node_matcher :candidate_condition?, '[{if case} !#allowed_ternary?]'
294
+
285
295
  def allowed_ternary?(assignment)
286
296
  assignment.if_type? && assignment.ternary? && !include_ternary?
287
297
  end
@@ -356,7 +366,7 @@ module RuboCop
356
366
  return if allowed_single_line?(branches)
357
367
  return if correction_exceeds_line_limit?(node, branches)
358
368
 
359
- add_offense(node, :expression)
369
+ add_offense(node)
360
370
  end
361
371
 
362
372
  def allowed_statements?(branches)
@@ -380,36 +390,20 @@ module RuboCop
380
390
 
381
391
  assignment = lhs(tail(branches[0]))
382
392
 
383
- longest_rhs_exceeds_line_limit?(branches, assignment) ||
384
- longest_line_exceeds_line_limit?(node, assignment)
385
- end
386
-
387
- def longest_rhs_exceeds_line_limit?(branches, assignment)
388
- longest_rhs_full_length(branches, assignment) > max_line_length
393
+ longest_line_exceeds_line_limit?(node, assignment)
389
394
  end
390
395
 
391
396
  def longest_line_exceeds_line_limit?(node, assignment)
392
397
  longest_line(node, assignment).length > max_line_length
393
398
  end
394
399
 
395
- def longest_rhs_full_length(branches, assignment)
396
- longest_rhs(branches) + indentation_width + assignment.length
397
- end
398
-
399
400
  def longest_line(node, assignment)
400
- assignment_regex = /#{Regexp.escape(assignment).gsub(' ', '\s*')}/
401
+ assignment_regex = /\s*#{Regexp.escape(assignment).gsub('\ ', '\s*')}/
401
402
  lines = node.source.lines.map do |line|
402
403
  line.chomp.sub(assignment_regex, '')
403
404
  end
404
405
  longest_line = lines.max_by(&:length)
405
- longest_line + assignment
406
- end
407
-
408
- def longest_rhs(branches)
409
- line_lengths = branches.flat_map do |branch|
410
- branch.children.last.source.split("\n").map(&:length)
411
- end
412
- line_lengths.max
406
+ assignment + longest_line
413
407
  end
414
408
 
415
409
  def line_length_cop_enabled?
@@ -475,7 +469,15 @@ module RuboCop
475
469
 
476
470
  def replace_branch_assignment(corrector, branch)
477
471
  _variable, *_operator, assignment = *branch
478
- corrector.replace(branch.source_range, assignment.source)
472
+ source = assignment.source
473
+
474
+ replacement = if assignment.array_type? && !assignment.bracketed?
475
+ "[#{source}]"
476
+ else
477
+ source
478
+ end
479
+
480
+ corrector.replace(branch.source_range, replacement)
479
481
  end
480
482
 
481
483
  def correct_branches(corrector, branches)
@@ -16,14 +16,21 @@ module RuboCop
16
16
  # an offense is reported.
17
17
  #
18
18
  class Copyright < Cop
19
+ MSG = 'Include a copyright notice matching /%s/ before ' \
20
+ 'any code.'.freeze
19
21
  AUTOCORRECT_EMPTY_WARNING = 'An AutocorrectNotice must be defined in' \
20
22
  'your RuboCop config'.freeze
21
23
 
22
- def message
23
- "Include a copyright notice matching /#{notice}/" \
24
- 'before any code.'
24
+ def investigate(processed_source)
25
+ return if notice.empty?
26
+ return if notice_found?(processed_source)
27
+ range = source_range(processed_source.buffer, 1, 0)
28
+ add_offense(insert_notice_before(processed_source),
29
+ range, MSG % notice)
25
30
  end
26
31
 
32
+ private
33
+
27
34
  def notice
28
35
  cop_config['Notice']
29
36
  end
@@ -32,13 +39,6 @@ module RuboCop
32
39
  cop_config['AutocorrectNotice']
33
40
  end
34
41
 
35
- def investigate(processed_source)
36
- return if notice.empty?
37
- return if notice_found?(processed_source)
38
- range = source_range(processed_source.buffer, 1, 0)
39
- add_offense(insert_notice_before(processed_source), range, message)
40
- end
41
-
42
42
  def insert_notice_before(processed_source)
43
43
  token_index = 0
44
44
  token_index += 1 if shebang_token?(processed_source, token_index)
@@ -7,17 +7,18 @@ module RuboCop
7
7
  # that does not take any arguments. Both instance and
8
8
  # class/singleton methods are checked.
9
9
  class DefWithParentheses < Cop
10
- include OnMethodDef
11
-
12
10
  MSG = "Omit the parentheses in defs when the method doesn't accept " \
13
11
  'any arguments.'.freeze
14
12
 
15
- def on_method_def(node, _method_name, args, _body)
13
+ def on_def(node)
16
14
  return if node.single_line?
17
- return unless args.children.empty? && args.loc.begin
15
+ return unless !node.arguments? && node.arguments.loc.begin
18
16
 
19
- add_offense(args, :begin)
17
+ add_offense(node.arguments, :begin)
20
18
  end
19
+ alias on_defs on_def
20
+
21
+ private
21
22
 
22
23
  def autocorrect(node)
23
24
  lambda do |corrector|
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # This cop checks for places where the `#__dir__` method can replace more
7
+ # complex constructs to retrieve a canonicalized absolute path to the
8
+ # current file.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # path = File.expand_path(File.dirname(__FILE__))
13
+ #
14
+ # # bad
15
+ # path = File.dirname(File.realpath(__FILE__))
16
+ #
17
+ # # good
18
+ # path = __dir__
19
+ class Dir < Cop
20
+ extend TargetRubyVersion
21
+
22
+ MSG = 'Use `__dir__` to get an absolute path to the current ' \
23
+ "file's directory.".freeze
24
+
25
+ def_node_matcher :dir_replacement?, <<-PATTERN
26
+ {(send (const nil :File) :expand_path (send (const nil :File) :dirname #file_keyword?))
27
+ (send (const nil :File) :dirname (send (const nil :File) :realpath #file_keyword?))}
28
+ PATTERN
29
+
30
+ minimum_target_ruby_version 2.0
31
+
32
+ def on_send(node)
33
+ dir_replacement?(node) do
34
+ add_offense(node)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def autocorrect(node)
41
+ lambda do |corrector|
42
+ corrector.replace(node.source_range, '__dir__')
43
+ end
44
+ end
45
+
46
+ def file_keyword?(node)
47
+ node.str_type? && node.source_range.is?('__FILE__')
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end