rubocop 0.47.1 → 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -16
  3. data/config/default.yml +203 -115
  4. data/config/disabled.yml +0 -5
  5. data/config/enabled.yml +92 -8
  6. data/lib/rubocop.rb +24 -1
  7. data/lib/rubocop/ast/builder.rb +7 -2
  8. data/lib/rubocop/ast/node.rb +23 -20
  9. data/lib/rubocop/ast/node/and_node.rb +37 -0
  10. data/lib/rubocop/ast/node/array_node.rb +4 -1
  11. data/lib/rubocop/ast/node/case_node.rb +1 -0
  12. data/lib/rubocop/ast/node/ensure_node.rb +25 -0
  13. data/lib/rubocop/ast/node/hash_node.rb +10 -3
  14. data/lib/rubocop/ast/node/if_node.rb +2 -0
  15. data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +23 -0
  16. data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +35 -0
  17. data/lib/rubocop/ast/node/or_node.rb +37 -0
  18. data/lib/rubocop/ast/node/resbody_node.rb +25 -0
  19. data/lib/rubocop/ast/node/send_node.rb +190 -0
  20. data/lib/rubocop/ast/node/when_node.rb +1 -1
  21. data/lib/rubocop/ast/traversal.rb +15 -15
  22. data/lib/rubocop/comment_config.rb +1 -1
  23. data/lib/rubocop/config.rb +39 -15
  24. data/lib/rubocop/config_loader.rb +34 -13
  25. data/lib/rubocop/cop/bundler/ordered_gems.rb +23 -4
  26. data/lib/rubocop/cop/commissioner.rb +4 -0
  27. data/lib/rubocop/cop/cop.rb +5 -0
  28. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +53 -0
  29. data/lib/rubocop/cop/lint/debugger.rb +8 -1
  30. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -1
  31. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +2 -4
  32. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
  33. data/lib/rubocop/cop/lint/each_with_object_argument.rb +3 -1
  34. data/lib/rubocop/cop/lint/empty_ensure.rb +6 -2
  35. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  36. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +20 -18
  37. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -3
  38. data/lib/rubocop/cop/lint/literal_in_condition.rb +1 -1
  39. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  40. data/lib/rubocop/cop/lint/multiple_compare.rb +5 -3
  41. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +26 -18
  42. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +7 -8
  43. data/lib/rubocop/cop/lint/require_parentheses.rb +7 -13
  44. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +7 -3
  45. data/lib/rubocop/cop/lint/shadowed_exception.rb +2 -6
  46. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +7 -8
  47. data/lib/rubocop/cop/lint/unneeded_disable.rb +35 -11
  48. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  49. data/lib/rubocop/cop/lint/unreachable_code.rb +5 -2
  50. data/lib/rubocop/cop/lint/unused_block_argument.rb +6 -6
  51. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -1
  52. data/lib/rubocop/cop/lint/useless_comparison.rb +5 -4
  53. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  54. data/lib/rubocop/cop/message_annotator.rb +7 -3
  55. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  56. data/lib/rubocop/cop/metrics/block_nesting.rb +4 -4
  57. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
  58. data/lib/rubocop/cop/metrics/line_length.rb +2 -9
  59. data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -3
  60. data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
  61. data/lib/rubocop/cop/mixin/access_modifier_node.rb +1 -1
  62. data/lib/rubocop/cop/mixin/array_hash_indentation.rb +2 -2
  63. data/lib/rubocop/cop/mixin/check_assignment.rb +6 -6
  64. data/lib/rubocop/cop/mixin/duplication.rb +1 -1
  65. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  66. data/lib/rubocop/cop/mixin/ignored_pattern.rb +27 -0
  67. data/lib/rubocop/cop/mixin/method_preference.rb +2 -0
  68. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +17 -29
  69. data/lib/rubocop/cop/mixin/on_method_def.rb +3 -3
  70. data/lib/rubocop/cop/mixin/percent_literal.rb +27 -0
  71. data/lib/rubocop/cop/mixin/rescue_node.rb +21 -0
  72. data/lib/rubocop/cop/mixin/safe_mode.rb +1 -1
  73. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  74. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -1
  75. data/lib/rubocop/cop/mixin/target_rails_version.rb +16 -0
  76. data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
  77. data/lib/rubocop/cop/offense.rb +3 -3
  78. data/lib/rubocop/cop/performance/casecmp.rb +1 -1
  79. data/lib/rubocop/cop/performance/detect.rb +2 -1
  80. data/lib/rubocop/cop/performance/double_start_end_with.rb +35 -1
  81. data/lib/rubocop/cop/performance/end_with.rb +3 -1
  82. data/lib/rubocop/cop/performance/flat_map.rb +6 -6
  83. data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
  84. data/lib/rubocop/cop/performance/range_include.rb +3 -1
  85. data/lib/rubocop/cop/performance/redundant_match.rb +6 -5
  86. data/lib/rubocop/cop/performance/regexp_match.rb +10 -3
  87. data/lib/rubocop/cop/performance/reverse_each.rb +2 -1
  88. data/lib/rubocop/cop/performance/size.rb +6 -11
  89. data/lib/rubocop/cop/performance/start_with.rb +3 -1
  90. data/lib/rubocop/cop/performance/string_replacement.rb +13 -18
  91. data/lib/rubocop/cop/performance/times_map.rb +4 -4
  92. data/lib/rubocop/cop/rails/action_filter.rb +42 -42
  93. data/lib/rubocop/cop/rails/active_support_aliases.rb +68 -0
  94. data/lib/rubocop/cop/rails/blank.rb +131 -0
  95. data/lib/rubocop/cop/rails/date.rb +25 -28
  96. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +5 -7
  97. data/lib/rubocop/cop/rails/dynamic_find_by.rb +7 -3
  98. data/lib/rubocop/cop/rails/exit.rb +9 -9
  99. data/lib/rubocop/cop/rails/file_path.rb +5 -14
  100. data/lib/rubocop/cop/rails/find_by.rb +8 -10
  101. data/lib/rubocop/cop/rails/find_each.rb +6 -9
  102. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -0
  103. data/lib/rubocop/cop/rails/http_positional_arguments.rb +15 -7
  104. data/lib/rubocop/cop/rails/output.rb +3 -5
  105. data/lib/rubocop/cop/rails/output_safety.rb +4 -8
  106. data/lib/rubocop/cop/rails/pluralization_grammar.rb +25 -24
  107. data/lib/rubocop/cop/rails/present.rb +137 -0
  108. data/lib/rubocop/cop/rails/read_write_attribute.rb +9 -18
  109. data/lib/rubocop/cop/rails/relative_date_constant.rb +53 -0
  110. data/lib/rubocop/cop/rails/request_referer.rb +7 -4
  111. data/lib/rubocop/cop/rails/reversible_migration.rb +1 -2
  112. data/lib/rubocop/cop/rails/safe_navigation.rb +2 -1
  113. data/lib/rubocop/cop/rails/save_bang.rb +10 -10
  114. data/lib/rubocop/cop/rails/skips_model_validations.rb +23 -6
  115. data/lib/rubocop/cop/rails/time_zone.rb +20 -18
  116. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -2
  117. data/lib/rubocop/cop/rails/validation.rb +8 -11
  118. data/lib/rubocop/cop/registry.rb +3 -3
  119. data/lib/rubocop/cop/security/json_load.rb +1 -1
  120. data/lib/rubocop/cop/security/marshal_load.rb +5 -1
  121. data/lib/rubocop/cop/security/yaml_load.rb +3 -3
  122. data/lib/rubocop/cop/severity.rb +1 -1
  123. data/lib/rubocop/cop/style/alias.rb +5 -5
  124. data/lib/rubocop/cop/style/align_hash.rb +1 -1
  125. data/lib/rubocop/cop/style/align_parameters.rb +5 -5
  126. data/lib/rubocop/cop/style/and_or.rb +16 -31
  127. data/lib/rubocop/cop/style/attr.rb +14 -8
  128. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +8 -11
  129. data/lib/rubocop/cop/style/block_delimiters.rb +11 -13
  130. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +19 -23
  131. data/lib/rubocop/cop/style/case_indentation.rb +2 -0
  132. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  133. data/lib/rubocop/cop/style/class_check.rb +5 -7
  134. data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +5 -4
  135. data/lib/rubocop/cop/style/collection_methods.rb +8 -8
  136. data/lib/rubocop/cop/style/colon_method_call.rb +2 -9
  137. data/lib/rubocop/cop/style/conditional_assignment.rb +38 -45
  138. data/lib/rubocop/cop/style/constant_name.rb +1 -1
  139. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  140. data/lib/rubocop/cop/style/dot_position.rb +3 -7
  141. data/lib/rubocop/cop/style/double_negation.rb +2 -1
  142. data/lib/rubocop/cop/style/each_with_object.rb +1 -1
  143. data/lib/rubocop/cop/style/empty_else.rb +2 -2
  144. data/lib/rubocop/cop/style/empty_line_after_magic_comment.rb +63 -0
  145. data/lib/rubocop/cop/style/empty_line_between_defs.rb +74 -4
  146. data/lib/rubocop/cop/style/empty_lines_around_begin_body.rb +42 -0
  147. data/lib/rubocop/cop/style/empty_lines_around_exception_handling_keywords.rb +127 -0
  148. data/lib/rubocop/cop/style/empty_literal.rb +17 -9
  149. data/lib/rubocop/cop/style/end_of_line.rb +25 -3
  150. data/lib/rubocop/cop/style/file_name.rb +1 -1
  151. data/lib/rubocop/cop/style/first_method_argument_line_break.rb +1 -1
  152. data/lib/rubocop/cop/style/first_parameter_indentation.rb +17 -19
  153. data/lib/rubocop/cop/style/for.rb +2 -4
  154. data/lib/rubocop/cop/style/format_string.rb +5 -4
  155. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  156. data/lib/rubocop/cop/style/identical_conditional_branches.rb +27 -1
  157. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
  158. data/lib/rubocop/cop/style/indent_assignment.rb +2 -2
  159. data/lib/rubocop/cop/style/indent_hash.rb +2 -1
  160. data/lib/rubocop/cop/style/indent_heredoc.rb +173 -0
  161. data/lib/rubocop/cop/style/indentation_width.rb +61 -29
  162. data/lib/rubocop/cop/style/inverse_methods.rb +130 -0
  163. data/lib/rubocop/cop/style/lambda_call.rb +15 -11
  164. data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -4
  165. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +26 -14
  166. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -16
  167. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
  168. data/lib/rubocop/cop/style/missing_else.rb +4 -3
  169. data/lib/rubocop/cop/style/mixin_grouping.rb +97 -0
  170. data/lib/rubocop/cop/style/multiline_memoization.rb +38 -5
  171. data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +2 -3
  172. data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +38 -19
  173. data/lib/rubocop/cop/style/mutable_constant.rb +5 -1
  174. data/lib/rubocop/cop/style/negated_if.rb +73 -1
  175. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +21 -19
  176. data/lib/rubocop/cop/style/next.rb +5 -5
  177. data/lib/rubocop/cop/style/non_nil_check.rb +7 -10
  178. data/lib/rubocop/cop/style/not.rb +3 -4
  179. data/lib/rubocop/cop/style/numeric_literals.rb +25 -3
  180. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  181. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
  182. data/lib/rubocop/cop/style/op_method.rb +2 -2
  183. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -3
  184. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +52 -6
  185. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  186. data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -9
  187. data/lib/rubocop/cop/style/raise_args.rb +28 -24
  188. data/lib/rubocop/cop/style/redundant_freeze.rb +5 -7
  189. data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -3
  190. data/lib/rubocop/cop/style/redundant_self.rb +17 -35
  191. data/lib/rubocop/cop/style/rescue_modifier.rb +2 -14
  192. data/lib/rubocop/cop/style/self_assignment.rb +3 -3
  193. data/lib/rubocop/cop/style/send.rb +4 -5
  194. data/lib/rubocop/cop/style/space_after_not.rb +7 -8
  195. data/lib/rubocop/cop/style/space_around_keyword.rb +8 -9
  196. data/lib/rubocop/cop/style/space_around_operators.rb +19 -15
  197. data/lib/rubocop/cop/style/space_before_first_arg.rb +17 -14
  198. data/lib/rubocop/cop/style/space_inside_brackets.rb +1 -1
  199. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +3 -3
  200. data/lib/rubocop/cop/style/space_inside_parens.rb +1 -1
  201. data/lib/rubocop/cop/style/special_global_vars.rb +14 -14
  202. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -1
  203. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  204. data/lib/rubocop/cop/style/string_methods.rb +10 -5
  205. data/lib/rubocop/cop/style/struct_inheritance.rb +4 -15
  206. data/lib/rubocop/cop/style/symbol_array.rb +31 -35
  207. data/lib/rubocop/cop/style/symbol_proc.rb +2 -2
  208. data/lib/rubocop/cop/style/ternary_parentheses.rb +41 -13
  209. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +6 -9
  210. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -1
  211. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  212. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -2
  213. data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
  214. data/lib/rubocop/cop/style/word_array.rb +12 -34
  215. data/lib/rubocop/cop/style/zero_length_predicate.rb +11 -4
  216. data/lib/rubocop/cop/team.rb +4 -1
  217. data/lib/rubocop/cop/util.rb +33 -26
  218. data/lib/rubocop/cop/variable_force.rb +13 -13
  219. data/lib/rubocop/cop/variable_force/assignment.rb +1 -8
  220. data/lib/rubocop/cop/variable_force/branch.rb +318 -0
  221. data/lib/rubocop/cop/variable_force/branchable.rb +21 -0
  222. data/lib/rubocop/cop/variable_force/reference.rb +1 -3
  223. data/lib/rubocop/cop/variable_force/scope.rb +36 -20
  224. data/lib/rubocop/cop/variable_force/variable.rb +9 -8
  225. data/lib/rubocop/formatter/colorizable.rb +10 -10
  226. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  227. data/lib/rubocop/formatter/html_formatter.rb +2 -1
  228. data/lib/rubocop/formatter/simple_text_formatter.rb +4 -2
  229. data/lib/rubocop/magic_comment.rb +20 -6
  230. data/lib/rubocop/options.rb +1 -1
  231. data/lib/rubocop/platform.rb +11 -0
  232. data/lib/rubocop/processed_source.rb +1 -1
  233. data/lib/rubocop/remote_config.rb +18 -6
  234. data/lib/rubocop/result_cache.rb +8 -8
  235. data/lib/rubocop/rspec/cop_helper.rb +2 -0
  236. data/lib/rubocop/rspec/shared_contexts.rb +20 -0
  237. data/lib/rubocop/rspec/shared_examples.rb +1 -1
  238. data/lib/rubocop/runner.rb +2 -2
  239. data/lib/rubocop/target_finder.rb +64 -6
  240. data/lib/rubocop/version.rb +2 -4
  241. metadata +27 -4
  242. data/lib/rubocop/cop/variable_force/locatable.rb +0 -200
@@ -19,12 +19,12 @@ module RuboCop
19
19
  MSG = 'Prefer using `YAML.safe_load` over `YAML.load`.'.freeze
20
20
 
21
21
  def_node_matcher :yaml_load, <<-END
22
- (send (const nil :YAML) :load ...)
22
+ (send (const {nil cbase} :YAML) :load ...)
23
23
  END
24
24
 
25
25
  def on_send(node)
26
- yaml_load(node) do |method|
27
- add_offense(node, :selector, format(MSG, method))
26
+ yaml_load(node) do
27
+ add_offense(node, :selector)
28
28
  end
29
29
  end
30
30
 
@@ -7,7 +7,7 @@ module RuboCop
7
7
  include Comparable
8
8
 
9
9
  # @api private
10
- NAMES = [:refactor, :convention, :warning, :error, :fatal].freeze
10
+ NAMES = %i(refactor convention warning error fatal).freeze
11
11
 
12
12
  # @api private
13
13
  CODE_TABLE = { R: :refactor, C: :convention,
@@ -15,7 +15,7 @@ module RuboCop
15
15
  MSG_SYMBOL_ARGS = 'Use `alias %s` instead of `alias %s`.'.freeze
16
16
 
17
17
  def on_send(node)
18
- return unless node.method_name == :alias_method && node.receiver.nil?
18
+ return unless node.command?(:alias_method)
19
19
  return if style == :prefer_alias_method
20
20
  return if scope_type(node) == :dynamic
21
21
 
@@ -86,14 +86,14 @@ module RuboCop
86
86
  end
87
87
 
88
88
  def bareword?(sym_node)
89
- sym_node.source[0] != ':'
89
+ !sym_node.source.start_with?(':')
90
90
  end
91
91
 
92
- def correct_alias_method_to_alias(node)
92
+ def correct_alias_method_to_alias(send_node)
93
93
  lambda do |corrector|
94
- new, old = *node.method_args
94
+ new, old = *send_node.arguments
95
95
  replacement = "alias #{new.children.first} #{old.children.first}"
96
- corrector.replace(node.source_range, replacement)
96
+ corrector.replace(send_node.source_range, replacement)
97
97
  end
98
98
  end
99
99
 
@@ -106,7 +106,7 @@ module RuboCop
106
106
  def on_send(node)
107
107
  return if double_splat?(node)
108
108
 
109
- last_argument = node.children.last
109
+ last_argument = node.last_argument
110
110
 
111
111
  return unless last_argument.hash_type? &&
112
112
  ignore_hash_argument?(last_argument)
@@ -16,23 +16,23 @@ module RuboCop
16
16
  'following the first line of a multi-line method %s.'.freeze
17
17
 
18
18
  def on_send(node)
19
- _receiver, method, *args = *node
19
+ return if node.arguments.size < 2 || node.method?(:[]=)
20
20
 
21
- return if method == :[]=
22
- return if args.size < 2
23
-
24
- check_alignment(args, base_column(node, args))
21
+ check_alignment(node.arguments, base_column(node, node.arguments))
25
22
  end
26
23
 
27
24
  def on_method_def(node, _method_name, args, _body)
28
25
  args = args.children
26
+
29
27
  return if args.size < 2
28
+
30
29
  check_alignment(args, base_column(node, args))
31
30
  end
32
31
 
33
32
  def message(node)
34
33
  type = node && node.parent.send_type? ? 'call' : 'definition'
35
34
  msg = fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_PARAMS_MSG
35
+
36
36
  format(msg, type)
37
37
  end
38
38
 
@@ -42,26 +42,21 @@ module RuboCop
42
42
  private
43
43
 
44
44
  def on_conditionals(node)
45
- condition_node, = *node
46
-
47
- condition_node.each_node(*LOGICAL_OPERATOR_NODES) do |logical_node|
45
+ node.condition.each_node(*LOGICAL_OPERATOR_NODES) do |logical_node|
48
46
  process_logical_op(logical_node)
49
47
  end
50
48
  end
51
49
 
52
50
  def process_logical_op(node)
53
- op = node.loc.operator.source
54
- op_type = node.type.to_s
55
- return unless op == op_type
51
+ return if node.logical_operator?
56
52
 
57
- add_offense(node, :operator, format(MSG, OPS[op], op))
53
+ add_offense(node, :operator,
54
+ format(MSG, node.alternate_operator, node.operator))
58
55
  end
59
56
 
60
57
  def autocorrect(node)
61
- expr1, expr2 = *node
62
- replacement = (node.and_type? ? '&&' : '||')
63
58
  lambda do |corrector|
64
- [expr1, expr2].each do |expr|
59
+ [*node].each do |expr|
65
60
  if expr.send_type?
66
61
  correct_send(expr, corrector)
67
62
  elsif expr.return_type?
@@ -70,24 +65,23 @@ module RuboCop
70
65
  correct_other(expr, corrector)
71
66
  end
72
67
  end
73
- corrector.replace(node.loc.operator, replacement)
68
+
69
+ corrector.replace(node.loc.operator, node.alternate_operator)
74
70
  end
75
71
  end
76
72
 
77
73
  def correct_send(node, corrector)
78
- receiver, method_name, *args = *node
79
- return correct_not(node, receiver, corrector) if method_name == :!
80
- return correct_setter(node, corrector) if setter_method?(method_name)
74
+ return correct_not(node, node.receiver, corrector) if node.method?(:!)
75
+ return correct_setter(node, corrector) if node.setter_method?
81
76
  return unless correctable_send?(node)
82
77
 
83
78
  corrector.replace(whitespace_before_arg(node), '('.freeze)
84
- corrector.insert_after(args.last.source_range, ')'.freeze)
79
+ corrector.insert_after(node.last_argument.source_range, ')'.freeze)
85
80
  end
86
81
 
87
82
  def correct_setter(node, corrector)
88
- receiver, _method_name, *args = *node
89
- corrector.insert_before(receiver.source_range, '('.freeze)
90
- corrector.insert_after(args.last.source_range, ')'.freeze)
83
+ corrector.insert_before(node.receiver.source_range, '('.freeze)
84
+ corrector.insert_after(node.last_argument.source_range, ')'.freeze)
91
85
  end
92
86
 
93
87
  # ! is a special case:
@@ -95,7 +89,7 @@ module RuboCop
95
89
  # recurse down a level and add parens to 'obj.method arg'
96
90
  # however, 'not x' also parses as (send x :!)
97
91
  def correct_not(node, receiver, corrector)
98
- if node.loc.selector.source == '!'
92
+ if node.keyword_bang?
99
93
  return unless receiver.send_type?
100
94
 
101
95
  correct_send(receiver, corrector)
@@ -107,23 +101,14 @@ module RuboCop
107
101
  end
108
102
 
109
103
  def correct_other(node, corrector)
110
- return unless node.source_range.begin.source != '('
104
+ return if node.source_range.begin.is?('(')
105
+
111
106
  corrector.insert_before(node.source_range, '(')
112
107
  corrector.insert_after(node.source_range, ')')
113
108
  end
114
109
 
115
- def setter_method?(method_name)
116
- method_name.to_s.end_with?('=')
117
- end
118
-
119
110
  def correctable_send?(node)
120
- _receiver, method_name, *args = *node
121
- # don't clobber if we already have a starting paren
122
- return false unless !node.loc.begin || node.loc.begin.source != '('
123
- # don't touch anything unless we are sure it is a method call.
124
- return false unless args.last && method_name.to_s =~ /[a-z]/
125
-
126
- true
111
+ !node.parenthesized? && node.arguments?
127
112
  end
128
113
 
129
114
  def whitespace_before_arg(node)
@@ -5,16 +5,19 @@ module RuboCop
5
5
  module Style
6
6
  # This cop checks for uses of Module#attr.
7
7
  class Attr < Cop
8
+ MSG = 'Do not use `attr`. Use `%s` instead.'.freeze
9
+
8
10
  def on_send(node)
9
- return unless node.command?(:attr)
10
- _receiver, _method_name, *args = *node
11
- msg = "Do not use `attr`. Use `#{replacement_method(node)}` instead."
11
+ return unless node.command?(:attr) && node.arguments?
12
12
 
13
- add_offense(node, :selector, msg) if args.any?
13
+ add_offense(node, :selector)
14
14
  end
15
15
 
16
+ private
17
+
16
18
  def autocorrect(node)
17
- _receiver, _method_name, attr_name, setter = *node
19
+ attr_name, setter = *node.arguments
20
+
18
21
  node_expr = node.source_range
19
22
  attr_expr = attr_name.source_range
20
23
 
@@ -24,14 +27,17 @@ module RuboCop
24
27
 
25
28
  lambda do |corrector|
26
29
  corrector.replace(node.loc.selector, replacement_method(node))
27
- corrector.replace(remove, '') if remove
30
+ corrector.remove(remove) if remove
28
31
  end
29
32
  end
30
33
 
31
- private
34
+ def message(node)
35
+ format(MSG, replacement_method(node))
36
+ end
32
37
 
33
38
  def replacement_method(node)
34
- _receiver, _method_name, _attr_name, setter = *node
39
+ setter = node.last_argument
40
+
35
41
  if setter && (setter.true_type? || setter.false_type?)
36
42
  setter.true_type? ? 'attr_accessor' : 'attr_reader'
37
43
  else
@@ -19,23 +19,20 @@ module RuboCop
19
19
  class AutoResourceCleanup < Cop
20
20
  MSG = 'Use the block version of `%s.%s`.'.freeze
21
21
 
22
- TARGET_METHODS = [
23
- [:File, :open]
24
- ].freeze
22
+ TARGET_METHODS = {
23
+ File: :open
24
+ }.freeze
25
25
 
26
26
  def on_send(node)
27
- receiver_node, method_name, *arg_nodes = *node
28
-
29
- TARGET_METHODS.each do |(target_class, target_method)|
27
+ TARGET_METHODS.each do |target_class, target_method|
30
28
  target_receiver = s(:const, nil, target_class)
31
29
 
32
- next if receiver_node != target_receiver
33
- next if method_name != target_method
30
+ next if node.receiver != target_receiver
31
+ next if node.method_name != target_method
34
32
  next if node.parent && node.parent.block_type?
35
- next if !arg_nodes.empty? && arg_nodes.last.block_pass_type?
33
+ next if node.block_argument?
36
34
 
37
- add_offense(node,
38
- :expression,
35
+ add_offense(node, :expression,
39
36
  format(MSG, target_class, target_method))
40
37
  end
41
38
  end
@@ -9,11 +9,10 @@ module RuboCop
9
9
  include ConfigurableEnforcedStyle
10
10
 
11
11
  def on_send(node)
12
- _receiver, method_name, *args = *node
13
- return if args.empty?
14
- return if parentheses?(node) || operator?(method_name)
12
+ return unless node.arguments?
13
+ return if node.parenthesized? || node.operator_method?
15
14
 
16
- args.each do |arg|
15
+ node.arguments.each do |arg|
17
16
  get_blocks(arg) do |block|
18
17
  # If there are no parentheses around the arguments, then braces
19
18
  # and do-end have different meaning due to how they bind, so we
@@ -116,21 +115,22 @@ module RuboCop
116
115
  when :block
117
116
  yield node
118
117
  when :send
119
- receiver, _method_name, *_args = *node
120
- get_blocks(receiver, &block) if receiver
118
+ get_blocks(node.receiver, &block) if node.receiver
121
119
  when :hash
122
120
  # A hash which is passed as method argument may have no braces
123
121
  # In that case, one of the K/V pairs could contain a block node
124
122
  # which could change in meaning if do...end replaced {...}
125
123
  return if node.loc.begin
126
- node.children.each { |child| get_blocks(child, &block) }
124
+ node.each_child_node { |child| get_blocks(child, &block) }
127
125
  when :pair
128
- node.children.each { |child| get_blocks(child, &block) }
126
+ node.each_child_node { |child| get_blocks(child, &block) }
129
127
  end
130
128
  nil
131
129
  end
132
130
 
133
131
  def proper_block_style?(node)
132
+ return true if ignored_method?(node.method_name)
133
+
134
134
  case style
135
135
  when :line_count_based then line_count_based_block_style?(node)
136
136
  when :semantic then semantic_block_style?(node)
@@ -146,8 +146,6 @@ module RuboCop
146
146
 
147
147
  def semantic_block_style?(node)
148
148
  method_name = node.method_name
149
- return true if ignored_method?(method_name)
150
-
151
149
  block_begin = node.loc.begin.source
152
150
 
153
151
  if block_begin == '{'
@@ -169,7 +167,7 @@ module RuboCop
169
167
  end
170
168
 
171
169
  def return_value_chaining?(node)
172
- node.parent && node.parent.send_type? && node.parent.loc.dot
170
+ node.parent && node.parent.send_type? && node.parent.dot?
173
171
  end
174
172
 
175
173
  def correction_would_break_code?(node)
@@ -178,8 +176,8 @@ module RuboCop
178
176
  # Converting `obj.method arg do |x| end` to use `{}` would cause
179
177
  # a syntax error.
180
178
  send = node.children.first
181
- _receiver, _method_name, *args = *send
182
- !args.empty? && !parentheses?(send)
179
+
180
+ send.arguments? && !send.parenthesized?
183
181
  end
184
182
 
185
183
  def ignored_method?(method_name)
@@ -11,26 +11,21 @@ module RuboCop
11
11
  MSG = '%s curly braces around a hash parameter.'.freeze
12
12
 
13
13
  def on_send(node)
14
- return if node.asgn_method_call?
14
+ return if node.asgn_method_call? || node.operator_method?
15
15
 
16
- _receiver, method_name, *args = *node
16
+ return unless node.arguments? && node.last_argument.hash_type? &&
17
+ !node.last_argument.empty?
17
18
 
18
- return if operator?(method_name)
19
- return if args.empty?
20
-
21
- return unless args.last.hash_type? && !args.last.pairs.empty?
22
-
23
- check(args.last, args)
19
+ check(node.last_argument, node.arguments)
24
20
  end
25
21
 
26
22
  private
27
23
 
28
24
  def check(arg, args)
29
25
  if style == :braces && !arg.braces?
30
- add_offense(arg.parent, arg.source_range, format(MSG, 'Missing'))
26
+ add_arg_offense(arg, :missing)
31
27
  elsif style == :no_braces && arg.braces?
32
- add_offense(arg.parent, arg.source_range,
33
- format(MSG, 'Redundant'))
28
+ add_arg_offense(arg, :redundant)
34
29
  elsif style == :context_dependent
35
30
  check_context_dependent(arg, args)
36
31
  end
@@ -38,28 +33,33 @@ module RuboCop
38
33
 
39
34
  def check_context_dependent(arg, args)
40
35
  braces_around_second_from_end = args.size > 1 && args[-2].hash_type?
36
+
41
37
  if arg.braces?
42
38
  unless braces_around_second_from_end
43
- add_offense(arg.parent, arg.source_range,
44
- format(MSG, 'Redundant'))
39
+ add_arg_offense(arg, :redundant)
45
40
  end
46
41
  elsif braces_around_second_from_end
47
- add_offense(arg.parent, arg.source_range, format(MSG, 'Missing'))
42
+ add_arg_offense(arg, :missing)
48
43
  end
49
44
  end
50
45
 
46
+ def add_arg_offense(arg, type)
47
+ add_offense(arg.parent, arg.source_range,
48
+ format(MSG, type.to_s.capitalize))
49
+ end
50
+
51
51
  # We let AutocorrectUnlessChangingAST#autocorrect work with the send
52
52
  # node, because that context is needed. When parsing the code to see if
53
53
  # the AST has changed, a braceless hash would not be parsed as a hash
54
54
  # otherwise.
55
55
  def autocorrect(send_node)
56
- _receiver, _method_name, *args = *send_node
57
- node = args.last
56
+ hash_node = send_node.last_argument
57
+
58
58
  lambda do |corrector|
59
- if node.braces?
60
- remove_braces_with_whitespace(corrector, node)
59
+ if hash_node.braces?
60
+ remove_braces_with_whitespace(corrector, hash_node)
61
61
  else
62
- add_braces(corrector, node)
62
+ add_braces(corrector, hash_node)
63
63
  end
64
64
  end
65
65
  end
@@ -98,10 +98,6 @@ module RuboCop
98
98
  corrector.insert_before(node.source_range, '{')
99
99
  corrector.insert_after(node.source_range, '}')
100
100
  end
101
-
102
- def braces?(arg)
103
- arg.loc.begin
104
- end
105
101
  end
106
102
  end
107
103
  end
@@ -14,6 +14,8 @@ module RuboCop
14
14
  MSG = 'Indent `when` %s `%s`.'.freeze
15
15
 
16
16
  def on_case(case_node)
17
+ return if case_node.single_line?
18
+
17
19
  case_node.each_when do |when_node|
18
20
  check_when(when_node)
19
21
  end
@@ -57,7 +57,7 @@ module RuboCop
57
57
  end
58
58
 
59
59
  def one_child?(body)
60
- body && [:module, :class].include?(body.type)
60
+ body && %i(module class).include?(body.type)
61
61
  end
62
62
 
63
63
  def compact_node_name?(node)