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
@@ -8,6 +8,8 @@ module RuboCop
8
8
  class SpaceAroundOperators < Cop
9
9
  include PrecedingFollowingAlignment
10
10
 
11
+ IRREGULAR_METHODS = %i([] ! []=).freeze
12
+
11
13
  def on_pair(node)
12
14
  return unless node.hash_rocket?
13
15
 
@@ -25,32 +27,32 @@ module RuboCop
25
27
 
26
28
  def on_resbody(node)
27
29
  return unless node.loc.assoc
30
+
28
31
  _, variable, = *node
29
32
 
30
33
  check_operator(node.loc.assoc, variable.source_range)
31
34
  end
32
35
 
33
36
  def on_send(node)
34
- if node.loc.operator # aref assignment, attribute assignment
37
+ if node.setter_method?
35
38
  on_special_asgn(node)
36
- elsif !node.unary_operation? && !called_with_dot?(node)
37
- op = node.method_name
38
- if operator_with_regular_syntax?(op)
39
- _, _, right, = *node
40
- check_operator(node.loc.selector, right.source_range)
41
- end
39
+ elsif regular_operator?(node)
40
+ check_operator(node.loc.selector, node.first_argument.source_range)
42
41
  end
43
42
  end
44
43
 
45
44
  def on_binary(node)
46
- _, right, = *node
47
- return if right.nil?
48
- check_operator(node.loc.operator, right.source_range)
45
+ _, rhs, = *node
46
+
47
+ return unless rhs
48
+
49
+ check_operator(node.loc.operator, rhs.source_range)
49
50
  end
50
51
 
51
52
  def on_special_asgn(node)
52
- return unless node.loc.operator
53
53
  _, _, right, = *node
54
+
55
+ return unless right
54
56
  check_operator(node.loc.operator, right.source_range)
55
57
  end
56
58
 
@@ -69,12 +71,14 @@ module RuboCop
69
71
 
70
72
  private
71
73
 
72
- def called_with_dot?(node)
73
- node.loc.dot
74
+ def regular_operator?(send_node)
75
+ !send_node.unary_operation? && !send_node.dot? &&
76
+ operator_with_regular_syntax?(send_node)
74
77
  end
75
78
 
76
- def operator_with_regular_syntax?(op)
77
- ![:[], :!, :[]=].include?(op) && operator?(op)
79
+ def operator_with_regular_syntax?(send_node)
80
+ send_node.operator_method? &&
81
+ !IRREGULAR_METHODS.include?(send_node.method_name)
78
82
  end
79
83
 
80
84
  def check_operator(op, right_operand)
@@ -15,6 +15,10 @@ module RuboCop
15
15
  # something x
16
16
  # something y, z
17
17
  #
18
+ # @good
19
+ # something x
20
+ # something y, z
21
+ #
18
22
  class SpaceBeforeFirstArg < Cop
19
23
  include PrecedingFollowingAlignment
20
24
 
@@ -22,13 +26,14 @@ module RuboCop
22
26
  'the first argument.'.freeze
23
27
 
24
28
  def on_send(node)
25
- return unless regular_method_call_with_params?(node)
29
+ return unless regular_method_call_with_arguments?(node)
26
30
  return unless expect_params_after_method_name?(node)
27
31
 
28
- _receiver, _method_name, *args = *node
29
- arg1 = args.first.source_range
30
- arg1_with_space = range_with_surrounding_space(arg1, :left)
31
- space = range_between(arg1_with_space.begin_pos, arg1.begin_pos)
32
+ first_arg = node.first_argument.source_range
33
+ first_arg_with_space = range_with_surrounding_space(first_arg, :left)
34
+ space = range_between(first_arg_with_space.begin_pos,
35
+ first_arg.begin_pos)
36
+
32
37
  add_offense(space, space) if space.length > 1
33
38
  end
34
39
 
@@ -38,20 +43,18 @@ module RuboCop
38
43
 
39
44
  private
40
45
 
41
- def regular_method_call_with_params?(node)
42
- _receiver, method_name, *args = *node
43
-
44
- !(args.empty? || operator?(method_name) || node.asgn_method_call?)
46
+ def regular_method_call_with_arguments?(node)
47
+ node.arguments? && !node.operator_method? && !node.setter_method?
45
48
  end
46
49
 
47
50
  def expect_params_after_method_name?(node)
48
- return false if parentheses?(node)
51
+ return false if node.parenthesized?
49
52
 
50
- _receiver, _method_name, *args = *node
51
- arg1 = args.first.source_range
53
+ first_arg = node.first_argument
52
54
 
53
- arg1.line == node.loc.line &&
54
- !(allow_for_alignment? && aligned_with_something?(arg1))
55
+ same_line?(first_arg, node) &&
56
+ !(allow_for_alignment? &&
57
+ aligned_with_something?(first_arg.source_range))
55
58
  end
56
59
  end
57
60
  end
@@ -9,7 +9,7 @@ module RuboCop
9
9
 
10
10
  def specifics
11
11
  [
12
- [:tLBRACK, :tLBRACK2],
12
+ %i(tLBRACK tLBRACK2),
13
13
  :tRBRACK,
14
14
  'square brackets'
15
15
  ]
@@ -48,7 +48,7 @@ module RuboCop
48
48
  return if t1.pos.line < t2.pos.line
49
49
  return if t2.type == :tCOMMENT # Also indicates there's a line break.
50
50
 
51
- is_empty_braces = t1.text == '{' && t2.text == '}'
51
+ is_empty_braces = left_brace?(t1) && right_brace?(t2)
52
52
  expect_space = expect_space?(t1, t2)
53
53
 
54
54
  if offense?(t1, t2, expect_space)
@@ -59,8 +59,8 @@ module RuboCop
59
59
  end
60
60
 
61
61
  def expect_space?(t1, t2)
62
- is_same_braces = t1.text == t2.text
63
- is_empty_braces = t1.text == '{' && t2.text == '}'
62
+ is_same_braces = t1.type == t2.type
63
+ is_empty_braces = left_brace?(t1) && right_brace?(t2)
64
64
 
65
65
  if is_same_braces && style == :compact
66
66
  false
@@ -8,7 +8,7 @@ module RuboCop
8
8
  include SpaceInside
9
9
 
10
10
  def specifics
11
- [[:tLPAREN, :tLPAREN2], :tRPAREN, 'parentheses']
11
+ [%i(tLPAREN tLPAREN2), :tRPAREN, 'parentheses']
12
12
  end
13
13
  end
14
14
  end
@@ -19,19 +19,19 @@ module RuboCop
19
19
  :$0 => [:$PROGRAM_NAME],
20
20
  :$! => [:$ERROR_INFO],
21
21
  :$@ => [:$ERROR_POSITION],
22
- :$; => [:$FIELD_SEPARATOR, :$FS],
23
- :$, => [:$OUTPUT_FIELD_SEPARATOR, :$OFS],
24
- :$/ => [:$INPUT_RECORD_SEPARATOR, :$RS],
25
- :$\ => [:$OUTPUT_RECORD_SEPARATOR, :$ORS],
26
- :$. => [:$INPUT_LINE_NUMBER, :$NR],
22
+ :$; => %i($FIELD_SEPARATOR $FS),
23
+ :$, => %i($OUTPUT_FIELD_SEPARATOR $OFS),
24
+ :$/ => %i($INPUT_RECORD_SEPARATOR $RS),
25
+ :$\ => %i($OUTPUT_RECORD_SEPARATOR $ORS),
26
+ :$. => %i($INPUT_LINE_NUMBER $NR),
27
27
  :$_ => [:$LAST_READ_LINE],
28
28
  :$> => [:$DEFAULT_OUTPUT],
29
29
  :$< => [:$DEFAULT_INPUT],
30
- :$$ => [:$PROCESS_ID, :$PID],
30
+ :$$ => %i($PROCESS_ID $PID),
31
31
  :$? => [:$CHILD_STATUS],
32
32
  :$~ => [:$LAST_MATCH_INFO],
33
33
  :$= => [:$IGNORECASE],
34
- :$* => [:$ARGV, :ARGV],
34
+ :$* => %i($ARGV ARGV),
35
35
  :$& => [:$MATCH],
36
36
  :$` => [:$PREMATCH],
37
37
  :$' => [:$POSTMATCH],
@@ -51,12 +51,12 @@ module RuboCop
51
51
  PERL_VARS.each { |_, v| v.freeze }.freeze
52
52
 
53
53
  # Anything *not* in this set is provided by the English library.
54
- NON_ENGLISH_VARS = Set.new([
55
- :$LOAD_PATH,
56
- :$LOADED_FEATURES,
57
- :$PROGRAM_NAME,
58
- :ARGV
59
- ]).freeze
54
+ NON_ENGLISH_VARS = Set.new(%i(
55
+ $LOAD_PATH
56
+ $LOADED_FEATURES
57
+ $PROGRAM_NAME
58
+ ARGV
59
+ )).freeze
60
60
 
61
61
  def on_gvar(node)
62
62
  global_var, = *node
@@ -126,7 +126,7 @@ module RuboCop
126
126
  parent_type = node.parent && node.parent.type
127
127
  preferred_name = preferred_names(global_var).first
128
128
 
129
- unless [:dstr, :xstr, :regexp].include?(parent_type)
129
+ unless %i(dstr xstr regexp).include?(parent_type)
130
130
  return preferred_name.to_s
131
131
  end
132
132
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module RuboCop
3
4
  module Cop
4
5
  module Style
@@ -87,7 +88,7 @@ module RuboCop
87
88
 
88
89
  def lambda_node?(node)
89
90
  receiver, call = *node
90
- receiver.nil? && call == :lambda
91
+ !receiver && call == :lambda
91
92
  end
92
93
 
93
94
  def arrow_form?(node)
@@ -93,7 +93,7 @@ module RuboCop
93
93
 
94
94
  def accept_child_double_quotes?(nodes)
95
95
  nodes.any? do |n|
96
- n.dstr_type? || double_quotes_acceptable?(n.str_content)
96
+ n.dstr_type? || double_quotes_required?(n.source)
97
97
  end
98
98
  end
99
99
  end
@@ -11,16 +11,21 @@ module RuboCop
11
11
  MSG = 'Prefer `%s` over `%s`.'.freeze
12
12
 
13
13
  def on_send(node)
14
- _receiver, method_name, *_args = *node
15
- return unless preferred_methods[method_name]
16
- add_offense(node, :selector,
17
- format(MSG, preferred_method(method_name), method_name))
14
+ return unless preferred_method(node.method_name)
15
+
16
+ add_offense(node, :selector)
17
+ end
18
+
19
+ private
20
+
21
+ def message(node)
22
+ format(MSG, preferred_method(node.method_name), node.method_name)
18
23
  end
19
24
 
20
25
  def autocorrect(node)
21
26
  lambda do |corrector|
22
27
  corrector.replace(node.loc.selector,
23
- preferred_method(node.loc.selector.source))
28
+ preferred_method(node.method_name))
24
29
  end
25
30
  end
26
31
  end
@@ -22,21 +22,10 @@ module RuboCop
22
22
  add_offense(node, superclass.source_range, MSG)
23
23
  end
24
24
 
25
- private
26
-
27
- def struct_constructor?(node)
28
- return false unless node
29
-
30
- send_node = node.block_type? ? node.children.first : node
31
- return false unless send_node.send_type?
32
-
33
- receiver, method_name = *send_node
34
-
35
- receiver &&
36
- receiver.const_type? &&
37
- receiver.children.last == :Struct &&
38
- method_name == :new
39
- end
25
+ def_node_matcher :struct_constructor?, <<-PATTERN
26
+ {(send (const nil :Struct) :new ...)
27
+ (block (send (const nil :Struct) :new ...) ...)}
28
+ PATTERN
40
29
  end
41
30
  end
42
31
  end
@@ -9,9 +9,31 @@ module RuboCop
9
9
  # Alternatively, it checks for symbol arrays using the %i() syntax on
10
10
  # projects which do not want to use that syntax, perhaps because they
11
11
  # support a version of Ruby lower than 2.0.
12
+ #
13
+ # @example
14
+ # EnforcedStyle: percent (default)
15
+ #
16
+ # # good
17
+ # %i[foo bar baz]
18
+ #
19
+ # # bad
20
+ # [:foo, :bar, :baz]
21
+ #
22
+ # @example
23
+ # EnforcedStyle: brackets
24
+ #
25
+ # # good
26
+ # [:foo, :bar, :baz]
27
+ #
28
+ # # bad
29
+ # %i[foo bar baz]
12
30
  class SymbolArray < Cop
13
31
  include ConfigurableEnforcedStyle
14
32
  include ArraySyntax
33
+ include PercentLiteral
34
+ extend TargetRubyVersion
35
+
36
+ minimum_target_ruby_version 2.0
15
37
 
16
38
  PERCENT_MSG = 'Use `%i` or `%I` for an array of symbols.'.freeze
17
39
  ARRAY_MSG = 'Use `[]` for an array of symbols.'.freeze
@@ -24,19 +46,6 @@ module RuboCop
24
46
  end
25
47
  end
26
48
 
27
- def validate_config
28
- return unless style == :percent && target_ruby_version < 2.0
29
-
30
- raise ValidationError, 'The default `percent` style for the ' \
31
- '`Style/SymbolArray` cop is only compatible' \
32
- ' with Ruby 2.0 and up, but the target Ruby' \
33
- " version for your project is 1.9.\nPlease " \
34
- 'either disable this cop, configure it to ' \
35
- 'use `array` style, or adjust the ' \
36
- '`TargetRubyVersion` parameter in your ' \
37
- 'configuration.'
38
- end
39
-
40
49
  private
41
50
 
42
51
  def check_bracketed_array(node)
@@ -69,32 +78,19 @@ module RuboCop
69
78
  end
70
79
 
71
80
  def autocorrect(node)
72
- syms = node.children.map { |c| c.children[0].to_s }
73
- corrected = if style == :percent
74
- percent_replacement(syms)
75
- else
76
- bracket_replacement(syms)
77
- end
78
-
79
- lambda do |corrector|
80
- corrector.replace(node.source_range, corrected)
81
- end
82
- end
83
-
84
- def percent_replacement(syms)
85
- escape = syms.any? { |s| needs_escaping?(s) }
86
- syms = syms.map { |s| escape_string(s) } if escape
87
- syms = syms.map { |s| s.gsub(/\)/, '\\)') }
88
- if escape
89
- "%I(#{syms.join(' ')})"
81
+ if style == :percent
82
+ correct_percent(node, 'i')
90
83
  else
91
- "%i(#{syms.join(' ')})"
84
+ correct_bracketed(node)
92
85
  end
93
86
  end
94
87
 
95
- def bracket_replacement(syms)
96
- syms = syms.map { |s| to_symbol_literal(s) }
97
- "[#{syms.join(', ')}]"
88
+ def correct_bracketed(node)
89
+ syms = node.children.map { |c| to_symbol_literal(c.children[0].to_s) }
90
+
91
+ lambda do |corrector|
92
+ corrector.replace(node.source_range, "[#{syms.join(', ')}]")
93
+ end
98
94
  end
99
95
  end
100
96
  end
@@ -13,7 +13,7 @@ module RuboCop
13
13
  # something.map(&:upcase)
14
14
  class SymbolProc < Cop
15
15
  MSG = 'Pass `&:%s` as an argument to `%s` instead of a block.'.freeze
16
- SUPER_TYPES = [:super, :zsuper].freeze
16
+ SUPER_TYPES = %i(super zsuper).freeze
17
17
 
18
18
  def_node_matcher :proc_node?, '(send (const nil :Proc) :new)'
19
19
  def_node_matcher :symbol_proc?, <<-PATTERN
@@ -31,7 +31,7 @@ module RuboCop
31
31
  # configurable - https://github.com/bbatsov/rubocop/issues/1485
32
32
  # we should ignore lambdas & procs
33
33
  return if proc_node?(send_or_super)
34
- return if [:lambda, :proc].include?(block_method_name)
34
+ return if %i(lambda proc).include?(block_method_name)
35
35
  return if ignored_method?(block_method_name)
36
36
  return unless can_shorten?(block_args, block_body)
37
37
 
@@ -13,7 +13,7 @@ module RuboCop
13
13
  #
14
14
  # @bad
15
15
  # foo = (bar?) ? a : b
16
- # foo = (bar.baz) ? a : b
16
+ # foo = (bar.baz?) ? a : b
17
17
  # foo = (bar && baz) ? a : b
18
18
  #
19
19
  # @good
@@ -32,7 +32,7 @@ module RuboCop
32
32
  #
33
33
  # @good
34
34
  # foo = (bar?) ? a : b
35
- # foo = (bar.baz) ? a : b
35
+ # foo = (bar.baz?) ? a : b
36
36
  # foo = (bar && baz) ? a : b
37
37
  #
38
38
  # @example
@@ -46,11 +46,12 @@ module RuboCop
46
46
  #
47
47
  # @good
48
48
  # foo = bar? ? a : b
49
- # foo = bar.baz ? a : b
49
+ # foo = bar.baz? ? a : b
50
50
  # foo = (bar && baz) ? a : b
51
51
  class TernaryParentheses < Cop
52
52
  include SafeAssignment
53
53
  include ConfigurableEnforcedStyle
54
+ include SurroundingSpace
54
55
 
55
56
  MSG = '%s parentheses for ternary conditions.'.freeze
56
57
  MSG_COMPLEX = '%s parentheses for ternary expressions with' \
@@ -87,14 +88,10 @@ module RuboCop
87
88
  (safe_assignment?(condition) ||
88
89
  unsafe_autocorrect?(condition))
89
90
 
90
- lambda do |corrector|
91
- if parenthesized?(condition)
92
- corrector.remove(condition.loc.begin)
93
- corrector.remove(condition.loc.end)
94
- else
95
- corrector.insert_before(condition.source_range, '(')
96
- corrector.insert_after(condition.source_range, ')')
97
- end
91
+ if parenthesized?(condition)
92
+ correct_parenthesized(condition)
93
+ else
94
+ correct_unparenthesized(condition)
98
95
  end
99
96
  end
100
97
 
@@ -113,7 +110,8 @@ module RuboCop
113
110
  def non_complex_type?(condition)
114
111
  condition.variable? || condition.const_type? ||
115
112
  (condition.send_type? && !operator?(condition.method_name)) ||
116
- condition.defined_type?
113
+ condition.defined_type? || condition.yield_type? ||
114
+ square_brackets?(condition)
117
115
  end
118
116
 
119
117
  def message(node)
@@ -135,7 +133,7 @@ module RuboCop
135
133
  end
136
134
 
137
135
  def redundant_parentheses_enabled?
138
- @config.for_cop('RedundantParentheses')['Enabled']
136
+ @config.for_cop('Style/RedundantParentheses').fetch('Enabled')
139
137
  end
140
138
 
141
139
  def parenthesized?(node)
@@ -165,6 +163,36 @@ module RuboCop
165
163
  {(:defined? $...)
166
164
  (send {(send ...) nil} _ $(send nil _)...)}
167
165
  PATTERN
166
+
167
+ def_node_matcher :square_brackets?,
168
+ '(send {(send _recv _msg) str array hash} :[] ...)'
169
+
170
+ def correct_parenthesized(condition)
171
+ lambda do |corrector|
172
+ corrector.remove(condition.loc.begin)
173
+ corrector.remove(condition.loc.end)
174
+
175
+ # Ruby allows no space between the question mark and parentheses.
176
+ # If we remove the parentheses, we need to add a space or we'll
177
+ # generate invalid code.
178
+ unless whitespace_after?(condition)
179
+ corrector.insert_after(condition.loc.end, ' ')
180
+ end
181
+ end
182
+ end
183
+
184
+ def correct_unparenthesized(condition)
185
+ lambda do |corrector|
186
+ corrector.insert_before(condition.source_range, '(')
187
+ corrector.insert_after(condition.source_range, ')')
188
+ end
189
+ end
190
+
191
+ def whitespace_after?(node)
192
+ index = index_of_last_token(node)
193
+ last_token, next_token = processed_source.tokens[index, 2]
194
+ space_between?(last_token, next_token)
195
+ end
168
196
  end
169
197
  end
170
198
  end