rubocop 0.58.2 → 0.59.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.
Files changed (211) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +22 -7
  4. data/config/disabled.yml +33 -4
  5. data/config/enabled.yml +4 -11
  6. data/lib/rubocop.rb +5 -0
  7. data/lib/rubocop/ast/builder.rb +1 -0
  8. data/lib/rubocop/ast/node.rb +11 -33
  9. data/lib/rubocop/ast/node/block_node.rb +8 -1
  10. data/lib/rubocop/ast/node/defined_node.rb +13 -0
  11. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +16 -5
  12. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +21 -0
  13. data/lib/rubocop/ast/node/send_node.rb +3 -12
  14. data/lib/rubocop/ast/traversal.rb +10 -0
  15. data/lib/rubocop/cli.rb +4 -1
  16. data/lib/rubocop/config.rb +21 -5
  17. data/lib/rubocop/config_loader.rb +2 -0
  18. data/lib/rubocop/config_loader_resolver.rb +3 -1
  19. data/lib/rubocop/cop/autocorrect_logic.rb +1 -0
  20. data/lib/rubocop/cop/bundler/gem_comment.rb +64 -0
  21. data/lib/rubocop/cop/bundler/ordered_gems.rb +2 -0
  22. data/lib/rubocop/cop/commissioner.rb +2 -0
  23. data/lib/rubocop/cop/cop.rb +3 -0
  24. data/lib/rubocop/cop/corrector.rb +2 -0
  25. data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -0
  26. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -0
  27. data/lib/rubocop/cop/correctors/space_corrector.rb +2 -0
  28. data/lib/rubocop/cop/force.rb +1 -0
  29. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  30. data/lib/rubocop/cop/generator.rb +1 -0
  31. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +1 -0
  32. data/lib/rubocop/cop/layout/class_structure.rb +4 -0
  33. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +5 -4
  34. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +35 -0
  35. data/lib/rubocop/cop/layout/else_alignment.rb +1 -0
  36. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -0
  37. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +1 -0
  38. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +5 -2
  39. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -0
  40. data/lib/rubocop/cop/layout/end_of_line.rb +1 -0
  41. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -0
  42. data/lib/rubocop/cop/layout/indent_array.rb +1 -0
  43. data/lib/rubocop/cop/layout/indent_heredoc.rb +3 -0
  44. data/lib/rubocop/cop/layout/indentation_width.rb +2 -0
  45. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -0
  46. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -1
  47. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +34 -11
  48. data/lib/rubocop/cop/layout/space_after_method_name.rb +1 -0
  49. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  50. data/lib/rubocop/cop/layout/space_around_keyword.rb +3 -1
  51. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -0
  52. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -0
  53. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +16 -8
  54. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +2 -0
  55. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +1 -1
  56. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +2 -0
  57. data/lib/rubocop/cop/layout/tab.rb +1 -0
  58. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -0
  59. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -0
  60. data/lib/rubocop/cop/lint/duplicate_methods.rb +9 -1
  61. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  62. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  63. data/lib/rubocop/cop/lint/interpolation_check.rb +2 -0
  64. data/lib/rubocop/cop/lint/literal_as_condition.rb +3 -6
  65. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -0
  66. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -0
  67. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -0
  68. data/lib/rubocop/cop/lint/rescue_type.rb +1 -0
  69. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +2 -2
  70. data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +2 -0
  71. data/lib/rubocop/cop/lint/script_permission.rb +1 -0
  72. data/lib/rubocop/cop/lint/shadowed_argument.rb +3 -0
  73. data/lib/rubocop/cop/lint/shadowed_exception.rb +2 -0
  74. data/lib/rubocop/cop/lint/unneeded_cop_disable_directive.rb +1 -0
  75. data/lib/rubocop/cop/lint/unneeded_cop_enable_directive.rb +1 -0
  76. data/lib/rubocop/cop/lint/unneeded_require_statement.rb +1 -0
  77. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
  78. data/lib/rubocop/cop/lint/unreachable_code.rb +2 -0
  79. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -0
  80. data/lib/rubocop/cop/lint/useless_setter_call.rb +3 -0
  81. data/lib/rubocop/cop/lint/void.rb +1 -0
  82. data/lib/rubocop/cop/message_annotator.rb +1 -0
  83. data/lib/rubocop/cop/metrics/block_length.rb +1 -0
  84. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -0
  85. data/lib/rubocop/cop/metrics/line_length.rb +6 -1
  86. data/lib/rubocop/cop/metrics/method_length.rb +1 -0
  87. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -0
  88. data/lib/rubocop/cop/mixin/classish_length.rb +1 -0
  89. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -0
  90. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
  91. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +12 -6
  92. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  93. data/lib/rubocop/cop/mixin/ignored_methods.rb +19 -0
  94. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +25 -1
  95. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +5 -3
  96. data/lib/rubocop/cop/mixin/percent_literal.rb +2 -0
  97. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +2 -0
  98. data/lib/rubocop/cop/mixin/safe_assignment.rb +2 -1
  99. data/lib/rubocop/cop/mixin/statement_modifier.rb +6 -1
  100. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -0
  101. data/lib/rubocop/cop/mixin/surrounding_space.rb +4 -0
  102. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -0
  103. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +2 -0
  104. data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -0
  105. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  106. data/lib/rubocop/cop/naming/file_name.rb +4 -1
  107. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -0
  108. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  109. data/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb +1 -0
  110. data/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb +1 -0
  111. data/lib/rubocop/cop/naming/variable_name.rb +1 -0
  112. data/lib/rubocop/cop/performance/case_when_splat.rb +11 -7
  113. data/lib/rubocop/cop/performance/casecmp.rb +33 -42
  114. data/lib/rubocop/cop/performance/chain_array_allocation.rb +77 -0
  115. data/lib/rubocop/cop/performance/compare_with_block.rb +3 -0
  116. data/lib/rubocop/cop/performance/regexp_match.rb +1 -0
  117. data/lib/rubocop/cop/performance/sample.rb +2 -0
  118. data/lib/rubocop/cop/performance/size.rb +8 -2
  119. data/lib/rubocop/cop/performance/string_replacement.rb +1 -0
  120. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -0
  121. data/lib/rubocop/cop/rails/bulk_change_table.rb +9 -2
  122. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +1 -0
  123. data/lib/rubocop/cop/rails/delegate.rb +7 -2
  124. data/lib/rubocop/cop/rails/dynamic_find_by.rb +1 -0
  125. data/lib/rubocop/cop/rails/find_each.rb +7 -2
  126. data/lib/rubocop/cop/rails/http_positional_arguments.rb +1 -1
  127. data/lib/rubocop/cop/rails/http_status.rb +2 -0
  128. data/lib/rubocop/cop/rails/inverse_of.rb +4 -0
  129. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +1 -0
  130. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -0
  131. data/lib/rubocop/cop/rails/reversible_migration.rb +1 -0
  132. data/lib/rubocop/cop/rails/save_bang.rb +189 -38
  133. data/lib/rubocop/cop/rails/time_zone.rb +1 -0
  134. data/lib/rubocop/cop/security/eval.rb +1 -0
  135. data/lib/rubocop/cop/security/json_load.rb +2 -2
  136. data/lib/rubocop/cop/security/open.rb +6 -3
  137. data/lib/rubocop/cop/severity.rb +1 -0
  138. data/lib/rubocop/cop/style/and_or.rb +3 -3
  139. data/lib/rubocop/cop/style/ascii_comments.rb +1 -0
  140. data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
  141. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +2 -3
  142. data/lib/rubocop/cop/style/class_and_module_children.rb +3 -0
  143. data/lib/rubocop/cop/style/class_vars.rb +1 -1
  144. data/lib/rubocop/cop/style/colon_method_definition.rb +1 -0
  145. data/lib/rubocop/cop/style/commented_keyword.rb +2 -0
  146. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  147. data/lib/rubocop/cop/style/copyright.rb +7 -2
  148. data/lib/rubocop/cop/style/date_time.rb +40 -7
  149. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  150. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -0
  151. data/lib/rubocop/cop/style/empty_else.rb +2 -0
  152. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -0
  153. data/lib/rubocop/cop/style/eval_with_location.rb +2 -0
  154. data/lib/rubocop/cop/style/for.rb +56 -10
  155. data/lib/rubocop/cop/style/format_string_token.rb +1 -1
  156. data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -0
  157. data/lib/rubocop/cop/style/inverse_methods.rb +1 -0
  158. data/lib/rubocop/cop/style/lambda.rb +1 -0
  159. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +3 -5
  160. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +3 -5
  161. data/lib/rubocop/cop/style/method_def_parentheses.rb +2 -2
  162. data/lib/rubocop/cop/style/missing_else.rb +1 -0
  163. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -0
  164. data/lib/rubocop/cop/style/multiline_method_signature.rb +65 -0
  165. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -0
  166. data/lib/rubocop/cop/style/nil_comparison.rb +45 -5
  167. data/lib/rubocop/cop/style/not.rb +1 -1
  168. data/lib/rubocop/cop/style/numeric_predicate.rb +5 -0
  169. data/lib/rubocop/cop/style/one_line_conditional.rb +1 -1
  170. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  171. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  172. data/lib/rubocop/cop/style/random_with_offset.rb +1 -0
  173. data/lib/rubocop/cop/style/redundant_begin.rb +13 -0
  174. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -0
  175. data/lib/rubocop/cop/style/redundant_parentheses.rb +6 -1
  176. data/lib/rubocop/cop/style/redundant_return.rb +1 -0
  177. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -0
  178. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -0
  179. data/lib/rubocop/cop/style/safe_navigation.rb +4 -0
  180. data/lib/rubocop/cop/style/semicolon.rb +4 -0
  181. data/lib/rubocop/cop/style/signal_exception.rb +1 -0
  182. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -0
  183. data/lib/rubocop/cop/style/symbol_proc.rb +1 -8
  184. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +1 -0
  185. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
  186. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -0
  187. data/lib/rubocop/cop/style/unneeded_condition.rb +13 -2
  188. data/lib/rubocop/cop/style/unneeded_percent_q.rb +2 -0
  189. data/lib/rubocop/cop/style/word_array.rb +13 -1
  190. data/lib/rubocop/cop/team.rb +1 -0
  191. data/lib/rubocop/cop/variable_force.rb +5 -0
  192. data/lib/rubocop/cop/variable_force/assignment.rb +4 -0
  193. data/lib/rubocop/cop/variable_force/branch.rb +4 -0
  194. data/lib/rubocop/cop/variable_force/branchable.rb +2 -0
  195. data/lib/rubocop/cop/variable_force/scope.rb +6 -0
  196. data/lib/rubocop/cop/variable_force/variable_table.rb +1 -0
  197. data/lib/rubocop/file_finder.rb +2 -0
  198. data/lib/rubocop/formatter/disabled_config_formatter.rb +4 -4
  199. data/lib/rubocop/formatter/file_list_formatter.rb +1 -0
  200. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -0
  201. data/lib/rubocop/options.rb +16 -0
  202. data/lib/rubocop/path_util.rb +16 -1
  203. data/lib/rubocop/processed_source.rb +4 -0
  204. data/lib/rubocop/remote_config.rb +6 -1
  205. data/lib/rubocop/result_cache.rb +1 -0
  206. data/lib/rubocop/rspec/cop_helper.rb +3 -5
  207. data/lib/rubocop/rspec/shared_examples.rb +1 -9
  208. data/lib/rubocop/runner.rb +4 -0
  209. data/lib/rubocop/target_finder.rb +2 -0
  210. data/lib/rubocop/version.rb +1 -1
  211. metadata +7 -2
@@ -69,7 +69,7 @@ module RuboCop
69
69
  private
70
70
 
71
71
  def includes_format_methods?(node)
72
- node.each_ancestor.any? do |ancestor|
72
+ node.each_ancestor(:send).any? do |ancestor|
73
73
  FORMAT_STRING_METHODS.include?(ancestor.method_name)
74
74
  end
75
75
  end
@@ -21,6 +21,7 @@ module RuboCop
21
21
  def on_normal_if_unless(node)
22
22
  beginning = node.loc.begin
23
23
  return unless beginning && beginning.is?(';')
24
+
24
25
  add_offense(node)
25
26
  end
26
27
  end
@@ -55,6 +55,7 @@ module RuboCop
55
55
 
56
56
  def on_send(node)
57
57
  return if part_of_ignored_node?(node)
58
+
58
59
  inverse_candidate?(node) do |_method_call, lhs, method, rhs|
59
60
  return unless inverse_methods.key?(method)
60
61
  return if possible_class_hierarchy_check?(lhs, rhs, method)
@@ -129,6 +129,7 @@ module RuboCop
129
129
  corrector.replace(block_method.source_range, 'lambda')
130
130
  corrector.remove(args.source_range) if args.source_range
131
131
  return if args.children.empty?
132
+
132
133
  arg_str = " |#{lambda_arg_string(args)}|"
133
134
  corrector.insert_after(node.loc.begin, arg_str)
134
135
  end
@@ -40,6 +40,8 @@ module RuboCop
40
40
  # bar :baz
41
41
  # end
42
42
  class MethodCallWithArgsParentheses < Cop
43
+ include IgnoredMethods
44
+
43
45
  MSG = 'Use parentheses for method calls with arguments.'.freeze
44
46
 
45
47
  def on_send(node)
@@ -66,11 +68,7 @@ module RuboCop
66
68
  def ignored_method?(node)
67
69
  node.operator_method? || node.setter_method? ||
68
70
  ignore_macros? && node.macro? ||
69
- ignored_list.include?(node.method_name)
70
- end
71
-
72
- def ignored_list
73
- cop_config['IgnoredMethods'].map(&:to_sym)
71
+ super(node.method_name)
74
72
  end
75
73
 
76
74
  def ignore_macros?
@@ -12,6 +12,8 @@ module RuboCop
12
12
  # # good
13
13
  # object.some_method
14
14
  class MethodCallWithoutArgsParentheses < Cop
15
+ include IgnoredMethods
16
+
15
17
  MSG = 'Do not use parentheses for method calls with ' \
16
18
  'no arguments.'.freeze
17
19
 
@@ -36,11 +38,7 @@ module RuboCop
36
38
  private
37
39
 
38
40
  def ineligible_node?(node)
39
- node.camel_case_method? || node.implicit_call? || node.keyword_not?
40
- end
41
-
42
- def ignored_method?(method)
43
- cop_config['IgnoredMethods'].to_a.map(&:to_sym).include?(method)
41
+ node.camel_case_method? || node.implicit_call? || node.prefix_not?
44
42
  end
45
43
 
46
44
  def same_name_assignment?(node)
@@ -60,8 +60,8 @@ module RuboCop
60
60
  #
61
61
  # @example EnforcedStyle: require_no_parentheses_except_multiline
62
62
  # # The `require_no_parentheses_except_multiline` style prefers no
63
- # # parantheses when method definition arguments fit on single line,
64
- # # but prefers parantheses when arguments span multiple lines.
63
+ # # parentheses when method definition arguments fit on single line,
64
+ # # but prefers parentheses when arguments span multiple lines.
65
65
  #
66
66
  # # bad
67
67
  # def bar(num1, num2)
@@ -167,6 +167,7 @@ module RuboCop
167
167
 
168
168
  def empty_else_style
169
169
  return unless empty_else_config.key?('EnforcedStyle')
170
+
170
171
  empty_else_config['EnforcedStyle'].to_sym
171
172
  end
172
173
 
@@ -58,6 +58,7 @@ module RuboCop
58
58
 
59
59
  def bad_rhs?(rhs)
60
60
  return false unless rhs.multiline?
61
+
61
62
  if style == :keyword
62
63
  rhs.begin_type?
63
64
  else
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # This cop checks for method signatures that span multiple lines.
7
+ #
8
+ # @example
9
+ #
10
+ # # good
11
+ #
12
+ # def foo(bar, baz)
13
+ # end
14
+ #
15
+ # # bad
16
+ #
17
+ # def foo(bar,
18
+ # baz)
19
+ # end
20
+ #
21
+ class MultilineMethodSignature < Cop
22
+ MSG = 'Avoid multi-line method signatures.'.freeze
23
+
24
+ def on_def(node)
25
+ return if opening_line(node) == closing_line(node)
26
+ return if correction_exceeds_max_line_length?(node)
27
+
28
+ add_offense(node)
29
+ end
30
+ alias on_defs on_def
31
+
32
+ private
33
+
34
+ def opening_line(node)
35
+ node.first_line
36
+ end
37
+
38
+ def closing_line(node)
39
+ if node.arguments?
40
+ node.arguments.last_line
41
+ else
42
+ node.first_line
43
+ end
44
+ end
45
+
46
+ def correction_exceeds_max_line_length?(node)
47
+ indentation_width(node) + definition_width(node) > max_line_length
48
+ end
49
+
50
+ def indentation_width(node)
51
+ processed_source.line_indentation(node.loc.expression.line)
52
+ end
53
+
54
+ def definition_width(node)
55
+ node.loc.expression.source.squeeze.length -
56
+ node.loc.end.source.length
57
+ end
58
+
59
+ def max_line_length
60
+ config.for_cop('Metrics/LineLength')['Max'] || 80
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -37,6 +37,7 @@ module RuboCop
37
37
 
38
38
  def nested_variable_comparison?(node)
39
39
  return false unless nested_comparison?(node)
40
+
40
41
  variables_in_node(node).count == 1
41
42
  end
42
43
 
@@ -3,9 +3,12 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # This cop checks for comparison of something with nil using ==.
6
+ # This cop checks for comparison of something with nil using `==` and
7
+ # `nil?`.
7
8
  #
8
- # @example
9
+ # Supported styles are: predicate, comparison.
10
+ #
11
+ # @example EnforcedStyle: predicate (default)
9
12
  #
10
13
  # # bad
11
14
  # if x == nil
@@ -14,21 +17,58 @@ module RuboCop
14
17
  # # good
15
18
  # if x.nil?
16
19
  # end
20
+ #
21
+ # @example EnforcedStyle: comparison
22
+ #
23
+ # # bad
24
+ # if x.nil?
25
+ # end
26
+ #
27
+ # # good
28
+ # if x == nil
29
+ # end
30
+ #
17
31
  class NilComparison < Cop
18
- MSG = 'Prefer the use of the `nil?` predicate.'.freeze
32
+ include ConfigurableEnforcedStyle
33
+
34
+ PREDICATE_MSG = 'Prefer the use of the `nil?` predicate.'.freeze
35
+ EXPLICIT_MSG = 'Prefer the use of the `==` comparison.'.freeze
19
36
 
20
37
  def_node_matcher :nil_comparison?, '(send _ {:== :===} nil)'
38
+ def_node_matcher :nil_check?, '(send _ :nil?)'
21
39
 
22
40
  def on_send(node)
23
- nil_comparison?(node) do
41
+ style_check?(node) do
24
42
  add_offense(node, location: :selector)
25
43
  end
26
44
  end
27
45
 
28
46
  def autocorrect(node)
29
- new_code = node.source.sub(/\s*={2,3}\s*nil/, '.nil?')
47
+ new_code = if prefer_comparison?
48
+ node.source.sub('.nil?', ' == nil')
49
+ else
50
+ node.source.sub(/\s*={2,3}\s*nil/, '.nil?')
51
+ end
30
52
  ->(corrector) { corrector.replace(node.source_range, new_code) }
31
53
  end
54
+
55
+ private
56
+
57
+ def message(_node)
58
+ prefer_comparison? ? EXPLICIT_MSG : PREDICATE_MSG
59
+ end
60
+
61
+ def style_check?(node, &block)
62
+ if prefer_comparison?
63
+ nil_check?(node, &block)
64
+ else
65
+ nil_comparison?(node, &block)
66
+ end
67
+ end
68
+
69
+ def prefer_comparison?
70
+ style == :comparison
71
+ end
32
72
  end
33
73
  end
34
74
  end
@@ -28,7 +28,7 @@ module RuboCop
28
28
  }.freeze
29
29
 
30
30
  def on_send(node)
31
- return unless node.keyword_not?
31
+ return unless node.prefix_not?
32
32
 
33
33
  add_offense(node, location: :selector)
34
34
  end
@@ -43,6 +43,7 @@ module RuboCop
43
43
  # bar.baz > 0
44
44
  class NumericPredicate < Cop
45
45
  include ConfigurableEnforcedStyle
46
+ include IgnoredMethods
46
47
 
47
48
  MSG = 'Use `%<prefer>s` instead of `%<current>s`.'.freeze
48
49
 
@@ -53,6 +54,10 @@ module RuboCop
53
54
  }.freeze
54
55
 
55
56
  def on_send(node)
57
+ return if node.each_ancestor(:send, :block).any? do |ancestor|
58
+ ignored_method?(ancestor.method_name)
59
+ end
60
+
56
61
  numeric, replacement = check(node)
57
62
 
58
63
  return unless numeric
@@ -87,7 +87,7 @@ module RuboCop
87
87
 
88
88
  def keyword_with_changed_precedence?(node)
89
89
  return false unless node.keyword?
90
- return true if node.keyword_not?
90
+ return true if node.prefix_not?
91
91
 
92
92
  !node.parenthesized_call?
93
93
  end
@@ -46,11 +46,13 @@ module RuboCop
46
46
 
47
47
  def on_if(node)
48
48
  return unless unless_assignment?(node)
49
+
49
50
  add_offense(node)
50
51
  end
51
52
 
52
53
  def on_lvasgn(node)
53
54
  return unless ternary_assignment?(node)
55
+
54
56
  add_offense(node)
55
57
  end
56
58
 
@@ -30,7 +30,7 @@ module RuboCop
30
30
  include ConfigurableEnforcedStyle
31
31
 
32
32
  LOWER_CASE_Q_MSG = 'Do not use `%Q` unless interpolation is ' \
33
- 'needed. Use `%q`.'.freeze
33
+ 'needed. Use `%q`.'.freeze
34
34
  UPPER_CASE_Q_MSG = 'Use `%Q` instead of `%q`.'.freeze
35
35
 
36
36
  def on_str(node)
@@ -59,6 +59,7 @@ module RuboCop
59
59
  return unless integer_op_rand?(node) ||
60
60
  rand_op_integer?(node) ||
61
61
  rand_modified?(node)
62
+
62
63
  add_offense(node)
63
64
  end
64
65
 
@@ -44,6 +44,16 @@ module RuboCop
44
44
  # rescue => ex
45
45
  # anything
46
46
  # end
47
+ #
48
+ # # good
49
+ # # Stabby lambdas don't support implicit `begin` in `do-end` blocks.
50
+ # -> do
51
+ # begin
52
+ # foo
53
+ # rescue Bar
54
+ # baz
55
+ # end
56
+ # end
47
57
  class RedundantBegin < Cop
48
58
  MSG = 'Redundant `begin` block detected.'.freeze
49
59
 
@@ -54,7 +64,10 @@ module RuboCop
54
64
 
55
65
  def on_block(node)
56
66
  return if target_ruby_version < 2.5
67
+
68
+ return if node.send_node.stabby_lambda?
57
69
  return if node.braces?
70
+
58
71
  check(node)
59
72
  end
60
73
 
@@ -63,6 +63,7 @@ module RuboCop
63
63
 
64
64
  def offense?(node)
65
65
  return if node.modifier_form?
66
+
66
67
  redundant_condition?(node) || redundant_condition_inverted?(node)
67
68
  end
68
69
 
@@ -28,6 +28,7 @@ module RuboCop
28
28
 
29
29
  def on_begin(node)
30
30
  return if !parentheses?(node) || parens_allowed?(node)
31
+
31
32
  check(node)
32
33
  end
33
34
 
@@ -63,8 +64,10 @@ module RuboCop
63
64
 
64
65
  def allowed_multiple_expression?(node)
65
66
  return false if node.children.one?
67
+
66
68
  ancestor = node.ancestors.first
67
69
  return false unless ancestor
70
+
68
71
  !ancestor.begin_type? && !ancestor.def_type? && !ancestor.block_type?
69
72
  end
70
73
 
@@ -89,6 +92,7 @@ module RuboCop
89
92
  end
90
93
  return offense(begin_node, 'a variable') if node.variable?
91
94
  return offense(begin_node, 'a constant') if node.const_type?
95
+
92
96
  check_send(begin_node, node) if node.send_type?
93
97
  end
94
98
 
@@ -157,6 +161,7 @@ module RuboCop
157
161
 
158
162
  siblings = begin_node.parent && begin_node.parent.children
159
163
  return false if siblings.nil?
164
+
160
165
  next_sibling = siblings[begin_node.sibling_index + 1]
161
166
  base_value = node.children.first
162
167
 
@@ -178,7 +183,7 @@ module RuboCop
178
183
 
179
184
  def method_call_with_redundant_parentheses?(node)
180
185
  return false unless node.send_type?
181
- return false if node.keyword_not?
186
+ return false if node.prefix_not?
182
187
  return false if range_end?(node)
183
188
 
184
189
  send_node, args = method_node_and_args(node)
@@ -118,6 +118,7 @@ module RuboCop
118
118
 
119
119
  def check_when_node(node)
120
120
  return unless node
121
+
121
122
  _cond, body = *node
122
123
  check_branch(body) if body
123
124
  end
@@ -23,6 +23,7 @@ module RuboCop
23
23
 
24
24
  def on_resbody(node)
25
25
  return unless rescue_modifier?(node)
26
+
26
27
  add_offense(node.parent)
27
28
  end
28
29
 
@@ -90,6 +90,7 @@ module RuboCop
90
90
 
91
91
  def on_resbody(node)
92
92
  return if rescue_modifier?(node)
93
+
93
94
  case style
94
95
  when :implicit
95
96
  rescue_standard_error?(node) do |error|
@@ -88,6 +88,7 @@ module RuboCop
88
88
 
89
89
  def on_if(node)
90
90
  return if allowed_if_condition?(node)
91
+
91
92
  check_node(node)
92
93
  end
93
94
 
@@ -97,6 +98,7 @@ module RuboCop
97
98
 
98
99
  def check_node(node)
99
100
  return if target_ruby_version < 2.3
101
+
100
102
  checked_variable, receiver, method_chain, method = extract_parts(node)
101
103
  return unless receiver == checked_variable
102
104
  # method is already a method call so this is actually checking for a
@@ -176,12 +178,14 @@ module RuboCop
176
178
  end
177
179
 
178
180
  return receiver if receiver == checked_variable
181
+
179
182
  find_matching_receiver_invocation(receiver, checked_variable)
180
183
  end
181
184
 
182
185
  def chain_size(method_chain, method)
183
186
  method.each_ancestor(:send).inject(0) do |total, ancestor|
184
187
  break total + 1 if ancestor == method_chain
188
+
185
189
  total + 1
186
190
  end
187
191
  end