rubocop 0.46.0 → 0.47.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +77 -2
  4. data/config/default.yml +151 -74
  5. data/config/disabled.yml +9 -0
  6. data/config/enabled.yml +49 -9
  7. data/lib/rubocop.rb +36 -8
  8. data/lib/rubocop/ast/builder.rb +59 -0
  9. data/lib/rubocop/ast/node.rb +607 -0
  10. data/lib/rubocop/ast/node/array_node.rb +45 -0
  11. data/lib/rubocop/ast/node/case_node.rb +63 -0
  12. data/lib/rubocop/ast/node/for_node.rb +53 -0
  13. data/lib/rubocop/ast/node/hash_node.rb +102 -0
  14. data/lib/rubocop/ast/node/if_node.rb +136 -0
  15. data/lib/rubocop/ast/node/keyword_splat_node.rb +45 -0
  16. data/lib/rubocop/ast/node/mixin/conditional_node.rb +45 -0
  17. data/lib/rubocop/ast/node/mixin/hash_element_node.rb +125 -0
  18. data/lib/rubocop/ast/node/mixin/modifier_node.rb +17 -0
  19. data/lib/rubocop/ast/node/pair_node.rb +64 -0
  20. data/lib/rubocop/ast/node/until_node.rb +43 -0
  21. data/lib/rubocop/ast/node/when_node.rb +61 -0
  22. data/lib/rubocop/ast/node/while_node.rb +43 -0
  23. data/lib/rubocop/ast/sexp.rb +16 -0
  24. data/lib/rubocop/{ast_node → ast}/traversal.rb +1 -1
  25. data/lib/rubocop/cli.rb +18 -14
  26. data/lib/rubocop/comment_config.rb +1 -3
  27. data/lib/rubocop/config.rb +93 -35
  28. data/lib/rubocop/config_loader.rb +1 -1
  29. data/lib/rubocop/cop/badge.rb +73 -0
  30. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -2
  31. data/lib/rubocop/cop/bundler/ordered_gems.rb +43 -3
  32. data/lib/rubocop/cop/commissioner.rb +17 -6
  33. data/lib/rubocop/cop/cop.rb +25 -112
  34. data/lib/rubocop/cop/lint/ambiguous_operator.rb +9 -4
  35. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +7 -0
  36. data/lib/rubocop/cop/lint/assignment_in_condition.rb +18 -4
  37. data/lib/rubocop/cop/lint/block_alignment.rb +40 -9
  38. data/lib/rubocop/cop/lint/circular_argument_reference.rb +14 -0
  39. data/lib/rubocop/cop/lint/condition_position.rb +14 -16
  40. data/lib/rubocop/cop/lint/debugger.rb +28 -0
  41. data/lib/rubocop/cop/lint/def_end_alignment.rb +21 -1
  42. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +13 -1
  43. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +26 -22
  44. data/lib/rubocop/cop/lint/duplicate_methods.rb +15 -1
  45. data/lib/rubocop/cop/lint/duplicated_key.rb +16 -8
  46. data/lib/rubocop/cop/lint/each_with_object_argument.rb +9 -0
  47. data/lib/rubocop/cop/lint/else_layout.rb +26 -29
  48. data/lib/rubocop/cop/lint/empty_ensure.rb +38 -0
  49. data/lib/rubocop/cop/lint/empty_expression.rb +11 -1
  50. data/lib/rubocop/cop/lint/empty_interpolation.rb +8 -0
  51. data/lib/rubocop/cop/lint/empty_when.rb +14 -16
  52. data/lib/rubocop/cop/lint/end_alignment.rb +48 -28
  53. data/lib/rubocop/cop/lint/end_in_method.rb +23 -0
  54. data/lib/rubocop/cop/lint/ensure_return.rb +21 -0
  55. data/lib/rubocop/cop/lint/float_out_of_range.rb +5 -0
  56. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +29 -4
  57. data/lib/rubocop/cop/lint/handle_exceptions.rb +40 -0
  58. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +7 -2
  59. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +11 -2
  60. data/lib/rubocop/cop/lint/invalid_character_literal.rb +3 -0
  61. data/lib/rubocop/cop/lint/literal_in_condition.rb +34 -36
  62. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +8 -0
  63. data/lib/rubocop/cop/lint/loop.rb +36 -0
  64. data/lib/rubocop/cop/lint/multiple_compare.rb +46 -0
  65. data/lib/rubocop/cop/lint/nested_method_definition.rb +22 -0
  66. data/lib/rubocop/cop/lint/next_without_accumulator.rb +5 -0
  67. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -0
  68. data/lib/rubocop/cop/lint/percent_string_array.rb +27 -13
  69. data/lib/rubocop/cop/lint/percent_symbol_array.rb +14 -4
  70. data/lib/rubocop/cop/lint/rand_one.rb +7 -3
  71. data/lib/rubocop/cop/lint/require_parentheses.rb +20 -19
  72. data/lib/rubocop/cop/lint/rescue_exception.rb +20 -0
  73. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +66 -0
  74. data/lib/rubocop/cop/lint/shadowed_exception.rb +6 -1
  75. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +24 -0
  76. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +8 -0
  77. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +24 -0
  78. data/lib/rubocop/cop/lint/unified_integer.rb +5 -0
  79. data/lib/rubocop/cop/lint/unneeded_disable.rb +2 -2
  80. data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +5 -0
  81. data/lib/rubocop/cop/lint/unreachable_code.rb +17 -0
  82. data/lib/rubocop/cop/lint/unused_block_argument.rb +2 -0
  83. data/lib/rubocop/cop/lint/unused_method_argument.rb +10 -0
  84. data/lib/rubocop/cop/lint/useless_access_modifier.rb +28 -1
  85. data/lib/rubocop/cop/lint/useless_assignment.rb +18 -0
  86. data/lib/rubocop/cop/lint/useless_comparison.rb +3 -1
  87. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +16 -1
  88. data/lib/rubocop/cop/lint/useless_setter_call.rb +16 -4
  89. data/lib/rubocop/cop/lint/void.rb +52 -0
  90. data/lib/rubocop/cop/message_annotator.rb +102 -0
  91. data/lib/rubocop/cop/metrics/block_length.rb +6 -0
  92. data/lib/rubocop/cop/metrics/block_nesting.rb +17 -5
  93. data/lib/rubocop/cop/metrics/line_length.rb +11 -4
  94. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -2
  95. data/lib/rubocop/cop/mixin/array_syntax.rb +2 -11
  96. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +12 -5
  97. data/lib/rubocop/cop/mixin/configurable_formatting.rb +48 -0
  98. data/lib/rubocop/cop/mixin/configurable_max.rb +3 -3
  99. data/lib/rubocop/cop/mixin/configurable_naming.rb +5 -33
  100. data/lib/rubocop/cop/mixin/configurable_numbering.rb +6 -47
  101. data/lib/rubocop/cop/mixin/documentation_comment.rb +7 -1
  102. data/lib/rubocop/cop/mixin/duplication.rb +46 -0
  103. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +2 -2
  104. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +14 -11
  105. data/lib/rubocop/cop/mixin/hash_alignment.rb +114 -0
  106. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -3
  107. data/lib/rubocop/cop/mixin/negative_conditional.rb +21 -7
  108. data/lib/rubocop/cop/mixin/on_method_def.rb +14 -0
  109. data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +1 -24
  110. data/lib/rubocop/cop/mixin/statement_modifier.rb +8 -13
  111. data/lib/rubocop/cop/mixin/target_ruby_version.rb +16 -0
  112. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -3
  113. data/lib/rubocop/cop/offense.rb +1 -1
  114. data/lib/rubocop/cop/performance/case_when_splat.rb +56 -59
  115. data/lib/rubocop/cop/performance/detect.rb +2 -2
  116. data/lib/rubocop/cop/performance/flat_map.rb +3 -3
  117. data/lib/rubocop/cop/performance/redundant_merge.rb +3 -6
  118. data/lib/rubocop/cop/performance/regexp_match.rb +201 -0
  119. data/lib/rubocop/cop/rails/delegate.rb +2 -2
  120. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +10 -19
  121. data/lib/rubocop/cop/rails/enum_uniqueness.rb +12 -40
  122. data/lib/rubocop/cop/rails/file_path.rb +80 -0
  123. data/lib/rubocop/cop/rails/find_each.rb +5 -14
  124. data/lib/rubocop/cop/rails/http_positional_arguments.rb +30 -24
  125. data/lib/rubocop/cop/rails/not_null_column.rb +23 -0
  126. data/lib/rubocop/cop/rails/reversible_migration.rb +217 -0
  127. data/lib/rubocop/cop/rails/safe_navigation.rb +4 -2
  128. data/lib/rubocop/cop/rails/skips_model_validations.rb +46 -0
  129. data/lib/rubocop/cop/rails/time_zone.rb +1 -1
  130. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +7 -5
  131. data/lib/rubocop/cop/registry.rb +170 -0
  132. data/lib/rubocop/cop/{lint → security}/eval.rb +7 -1
  133. data/lib/rubocop/cop/security/marshal_load.rb +33 -0
  134. data/lib/rubocop/cop/security/yaml_load.rb +37 -0
  135. data/lib/rubocop/cop/style/align_hash.rb +138 -169
  136. data/lib/rubocop/cop/style/and_or.rb +1 -1
  137. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +10 -15
  138. data/lib/rubocop/cop/style/case_indentation.rb +36 -27
  139. data/lib/rubocop/cop/style/conditional_assignment.rb +64 -47
  140. data/lib/rubocop/cop/style/each_with_object.rb +4 -1
  141. data/lib/rubocop/cop/style/else_alignment.rb +14 -20
  142. data/lib/rubocop/cop/style/empty_case_condition.rb +16 -25
  143. data/lib/rubocop/cop/style/empty_else.rb +20 -22
  144. data/lib/rubocop/cop/style/empty_literal.rb +4 -4
  145. data/lib/rubocop/cop/style/empty_method.rb +12 -6
  146. data/lib/rubocop/cop/style/encoding.rb +1 -1
  147. data/lib/rubocop/cop/style/file_name.rb +24 -4
  148. data/lib/rubocop/cop/style/first_method_argument_line_break.rb +1 -1
  149. data/lib/rubocop/cop/style/format_string.rb +17 -48
  150. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +40 -11
  151. data/lib/rubocop/cop/style/guard_clause.rb +11 -17
  152. data/lib/rubocop/cop/style/hash_syntax.rb +24 -42
  153. data/lib/rubocop/cop/style/identical_conditional_branches.rb +40 -28
  154. data/lib/rubocop/cop/style/if_inside_else.rb +6 -9
  155. data/lib/rubocop/cop/style/if_unless_modifier.rb +16 -25
  156. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -9
  157. data/lib/rubocop/cop/style/indent_array.rb +1 -1
  158. data/lib/rubocop/cop/style/indentation_width.rb +29 -60
  159. data/lib/rubocop/cop/style/infinite_loop.rb +21 -22
  160. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +86 -0
  161. data/lib/rubocop/cop/style/{method_call_parentheses.rb → method_call_without_args_parentheses.rb} +8 -1
  162. data/lib/rubocop/cop/style/missing_else.rb +40 -14
  163. data/lib/rubocop/cop/style/multiline_if_modifier.rb +5 -15
  164. data/lib/rubocop/cop/style/multiline_if_then.rb +14 -8
  165. data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +3 -3
  166. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -5
  167. data/lib/rubocop/cop/style/mutable_constant.rb +3 -2
  168. data/lib/rubocop/cop/style/negated_if.rb +3 -19
  169. data/lib/rubocop/cop/style/negated_while.rb +2 -17
  170. data/lib/rubocop/cop/style/nested_modifier.rb +16 -43
  171. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -5
  172. data/lib/rubocop/cop/style/next.rb +23 -21
  173. data/lib/rubocop/cop/style/non_nil_check.rb +2 -3
  174. data/lib/rubocop/cop/style/not.rb +1 -3
  175. data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
  176. data/lib/rubocop/cop/style/one_line_conditional.rb +12 -22
  177. data/lib/rubocop/cop/style/option_hash.rb +4 -15
  178. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -3
  179. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -12
  180. data/lib/rubocop/cop/style/percent_q_literals.rb +15 -12
  181. data/lib/rubocop/cop/style/redundant_freeze.rb +3 -2
  182. data/lib/rubocop/cop/style/redundant_parentheses.rb +27 -4
  183. data/lib/rubocop/cop/style/redundant_return.rb +4 -8
  184. data/lib/rubocop/cop/style/safe_navigation.rb +13 -6
  185. data/lib/rubocop/cop/style/space_after_colon.rb +2 -4
  186. data/lib/rubocop/cop/style/space_around_block_parameters.rb +1 -1
  187. data/lib/rubocop/cop/style/space_around_operators.rb +15 -13
  188. data/lib/rubocop/cop/style/string_methods.rb +1 -3
  189. data/lib/rubocop/cop/style/symbol_array.rb +1 -5
  190. data/lib/rubocop/cop/style/ternary_parentheses.rb +5 -6
  191. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +2 -5
  192. data/lib/rubocop/cop/style/trailing_comma_in_literal.rb +1 -1
  193. data/lib/rubocop/cop/style/unless_else.rb +1 -5
  194. data/lib/rubocop/cop/style/when_then.rb +4 -2
  195. data/lib/rubocop/cop/style/while_until_do.rb +9 -13
  196. data/lib/rubocop/cop/style/while_until_modifier.rb +12 -11
  197. data/lib/rubocop/cop/style/word_array.rb +5 -9
  198. data/lib/rubocop/cop/team.rb +16 -15
  199. data/lib/rubocop/cop/util.rb +13 -3
  200. data/lib/rubocop/formatter/clang_style_formatter.rb +2 -2
  201. data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -1
  202. data/lib/rubocop/magic_comment.rb +196 -0
  203. data/lib/rubocop/options.rb +5 -4
  204. data/lib/rubocop/processed_source.rb +1 -1
  205. data/lib/rubocop/rspec/cop_helper.rb +9 -0
  206. data/lib/rubocop/rspec/shared_examples.rb +1 -1
  207. data/lib/rubocop/runner.rb +7 -2
  208. data/lib/rubocop/version.rb +1 -1
  209. metadata +41 -14
  210. data/lib/rubocop/ast_node.rb +0 -624
  211. data/lib/rubocop/ast_node/builder.rb +0 -30
  212. data/lib/rubocop/ast_node/sexp.rb +0 -13
  213. data/lib/rubocop/cop/mixin/hash_node.rb +0 -14
  214. data/lib/rubocop/cop/mixin/if_node.rb +0 -42
@@ -18,28 +18,13 @@ module RuboCop
18
18
  end
19
19
 
20
20
  def message(node)
21
- if node.while_type?
22
- format(MSG, 'until', 'while')
23
- else
24
- format(MSG, 'while', 'until')
25
- end
21
+ format(MSG, node.inverse_keyword, node.keyword)
26
22
  end
27
23
 
28
24
  private
29
25
 
30
26
  def autocorrect(node)
31
- lambda do |corrector|
32
- condition, _body, _rest = *node
33
- # Look inside parentheses around the condition, if any.
34
- condition, = *condition while condition.begin_type?
35
- # Unwrap the negated portion of the condition (a send node).
36
- pos_condition, _method, = *condition
37
- corrector.replace(
38
- node.loc.keyword,
39
- node.while_type? ? 'until' : 'while'
40
- )
41
- corrector.replace(condition.source_range, pos_condition.source)
42
- end
27
+ negative_conditional_corrector(node)
43
28
  end
44
29
  end
45
30
  end
@@ -14,8 +14,6 @@ module RuboCop
14
14
  # # good
15
15
  # something if b && a
16
16
  class NestedModifier < Cop
17
- include IfNode
18
-
19
17
  MSG = 'Avoid using nested modifiers.'.freeze
20
18
 
21
19
  def on_while(node)
@@ -32,54 +30,33 @@ module RuboCop
32
30
 
33
31
  def check(node)
34
32
  return if part_of_ignored_node?(node)
35
- return unless modifier?(node)
36
-
37
- ancestor = node.ancestors.first
38
- return unless ancestor &&
39
- [:if, :while, :until].include?(ancestor.type) &&
40
- modifier?(ancestor)
33
+ return unless modifier?(node) && modifier?(node.parent)
41
34
 
42
35
  add_offense(node, :keyword)
43
36
  ignore_node(node)
44
37
  end
45
38
 
46
39
  def modifier?(node)
47
- modifier_if?(node) || modifier_while_or_until?(node)
48
- end
49
-
50
- def modifier_while_or_until?(node)
51
- node.loc.respond_to?(:keyword) &&
52
- %w(while until).include?(node.loc.keyword.source) &&
53
- node.modifier_form?
40
+ node && MODIFIER_NODES.include?(node.type) && node.modifier_form?
54
41
  end
55
42
 
56
43
  def autocorrect(node)
57
- return unless node.if_type?
58
-
59
- ancestor = node.ancestors.first
60
- return unless ancestor.if_type?
44
+ return unless node.if_type? && node.parent.if_type?
61
45
 
62
- autocorrect_if_unless(ancestor, node)
63
- end
64
-
65
- def autocorrect_if_unless(outer_node, inner_node)
66
- outer_cond, = *outer_node
67
-
68
- range = range_between(inner_node.loc.keyword.begin_pos,
69
- outer_cond.source_range.end_pos)
46
+ range = range_between(node.loc.keyword.begin_pos,
47
+ node.parent.condition.source_range.end_pos)
70
48
 
71
49
  lambda do |corrector|
72
- corrector.replace(range, new_expression(outer_node, inner_node))
50
+ corrector.replace(range, new_expression(node.parent, node))
73
51
  end
74
52
  end
75
53
 
76
54
  def new_expression(outer_node, inner_node)
77
- outer_keyword = outer_node.loc.keyword.source
78
- operator = replacement_operator(outer_keyword)
55
+ operator = replacement_operator(outer_node.keyword)
79
56
  lh_operand = left_hand_operand(outer_node, operator)
80
- rh_operand = right_hand_operand(inner_node, outer_keyword)
57
+ rh_operand = right_hand_operand(inner_node, outer_node.keyword)
81
58
 
82
- "#{outer_keyword} #{lh_operand} #{operator} #{rh_operand}"
59
+ "#{outer_node.keyword} #{lh_operand} #{operator} #{rh_operand}"
83
60
  end
84
61
 
85
62
  def replacement_operator(keyword)
@@ -87,26 +64,22 @@ module RuboCop
87
64
  end
88
65
 
89
66
  def left_hand_operand(node, operator)
90
- cond, = *node
91
-
92
- expr = cond.source
93
- expr = "(#{expr})" if cond.or_type? && operator == '&&'.freeze
67
+ expr = node.condition.source
68
+ expr = "(#{expr})" if node.condition.or_type? &&
69
+ operator == '&&'.freeze
94
70
  expr
95
71
  end
96
72
 
97
73
  def right_hand_operand(node, left_hand_keyword)
98
- cond, = *node
99
- keyword = node.loc.keyword.source
100
-
101
- expr = cond.source
102
- expr = "(#{expr})" if requires_parens?(cond)
103
- expr = "!#{expr}" unless left_hand_keyword == keyword
74
+ expr = node.condition.source
75
+ expr = "(#{expr})" if requires_parens?(node.condition)
76
+ expr = "!#{expr}" unless left_hand_keyword == node.keyword
104
77
  expr
105
78
  end
106
79
 
107
80
  def requires_parens?(node)
108
81
  node.or_type? ||
109
- !(RuboCop::Node::COMPARISON_OPERATORS & node.children).empty?
82
+ !(RuboCop::AST::Node::COMPARISON_OPERATORS & node.children).empty?
110
83
  end
111
84
  end
112
85
  end
@@ -5,16 +5,14 @@ module RuboCop
5
5
  module Style
6
6
  # This cop checks for nested ternary op expressions.
7
7
  class NestedTernaryOperator < Cop
8
- include IfNode
9
-
10
8
  MSG = 'Ternary operators must not be nested. Prefer `if` or `else` ' \
11
9
  'constructs instead.'.freeze
12
10
 
13
11
  def on_if(node)
14
- return unless ternary?(node)
12
+ return unless node.ternary?
15
13
 
16
- node.each_descendant(:if) do |nested_if_node|
17
- add_offense(nested_if_node, :expression) if ternary?(nested_if_node)
14
+ node.each_descendant(:if).select(&:ternary?).each do |nested_ternary|
15
+ add_offense(nested_ternary, :expression)
18
16
  end
19
17
  end
20
18
  end
@@ -19,7 +19,6 @@ module RuboCop
19
19
  # puts a
20
20
  # end
21
21
  class Next < Cop
22
- include IfNode
23
22
  include ConfigurableEnforcedStyle
24
23
  include MinBodyLength
25
24
 
@@ -50,21 +49,14 @@ module RuboCop
50
49
  end
51
50
 
52
51
  def on_while(node)
53
- _, body = *node
54
- return unless body && ends_with_condition?(body)
55
-
56
- offense_node = offense_node(body)
57
- add_offense(offense_node, offense_location(offense_node), MSG)
58
- end
59
- alias on_until on_while
52
+ return unless node.body && ends_with_condition?(node.body)
60
53
 
61
- def on_for(node)
62
- _, _, body = *node
63
- return unless body && ends_with_condition?(body)
54
+ offending_node = offense_node(node.body)
64
55
 
65
- offense_node = offense_node(body)
66
- add_offense(offense_node, offense_location(offense_node), MSG)
56
+ add_offense(offending_node, offense_location(offending_node), MSG)
67
57
  end
58
+ alias on_until on_while
59
+ alias on_for on_while
68
60
 
69
61
  private
70
62
 
@@ -80,23 +72,33 @@ module RuboCop
80
72
  end
81
73
 
82
74
  def simple_if_without_break?(node)
83
- return false unless node
84
75
  return false unless if_without_else?(node)
85
- return false if style == :skip_modifier_ifs && modifier_if?(node)
86
- return false if !modifier_if?(node) && !min_body_length?(node)
76
+ return false if if_else_children?(node)
77
+ return false if allowed_modifier_if?(node)
87
78
 
88
79
  !exit_body_type?(node)
89
80
  end
90
81
 
82
+ def allowed_modifier_if?(node)
83
+ if node.modifier_form?
84
+ style == :skip_modifier_ifs
85
+ else
86
+ !min_body_length?(node)
87
+ end
88
+ end
89
+
90
+ def if_else_children?(node)
91
+ node.each_child_node(:if).any?(&:else?)
92
+ end
93
+
91
94
  def if_without_else?(node)
92
- node.if_type? && !ternary?(node) && !if_else?(node)
95
+ node && node.if_type? && !node.ternary? && !node.else?
93
96
  end
94
97
 
95
98
  def exit_body_type?(node)
96
- _conditional, if_body, _else_body = *node
97
- return false unless if_body
99
+ return false unless node.if_branch
98
100
 
99
- EXIT_TYPES.include?(if_body.type)
101
+ EXIT_TYPES.include?(node.if_branch.type)
100
102
  end
101
103
 
102
104
  def offense_node(body)
@@ -112,7 +114,7 @@ module RuboCop
112
114
 
113
115
  def autocorrect(node)
114
116
  lambda do |corrector|
115
- if modifier_if?(node)
117
+ if node.modifier_form?
116
118
  autocorrect_modifier(corrector, node)
117
119
  else
118
120
  autocorrect_block(corrector, node)
@@ -25,7 +25,6 @@ module RuboCop
25
25
  # end
26
26
  class NonNilCheck < Cop
27
27
  include OnMethodDef
28
- include IfNode
29
28
 
30
29
  def_node_matcher :not_equal_to_nil?, '(send _ :!= (:nil))'
31
30
  def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
@@ -47,8 +46,8 @@ module RuboCop
47
46
 
48
47
  def unless_and_nil_check?(send_node)
49
48
  parent = send_node.parent
50
- nil_check?(send_node) && unless_check?(parent) && !ternary?(parent) &&
51
- parent.loc.keyword.is?('unless')
49
+ nil_check?(send_node) && unless_check?(parent) && !parent.ternary? &&
50
+ parent.unless?
52
51
  end
53
52
 
54
53
  def message(node)
@@ -5,8 +5,6 @@ module RuboCop
5
5
  module Style
6
6
  # This cop checks for uses if the keyword *not* instead of !.
7
7
  class Not < Cop
8
- include IfNode
9
-
10
8
  MSG = 'Use `!` instead of `not`.'.freeze
11
9
 
12
10
  OPPOSITE_METHODS = {
@@ -45,7 +43,7 @@ module RuboCop
45
43
 
46
44
  def requires_parens?(child)
47
45
  child.and_type? || child.or_type? || child.binary_operation? ||
48
- ternary?(child)
46
+ child.if_type? && child.ternary?
49
47
  end
50
48
 
51
49
  def correct_opposite_method(range, child)
@@ -25,7 +25,7 @@ module RuboCop
25
25
 
26
26
  private
27
27
 
28
- def parameter_name
28
+ def max_parameter_name
29
29
  'MinDigits'
30
30
  end
31
31
 
@@ -39,7 +39,7 @@ module RuboCop
39
39
  case int
40
40
  when /^\d+$/
41
41
  add_offense(node, :expression) { self.max = int.size + 1 }
42
- when /\d{4}/, /_\d{1,2}_/
42
+ when /\d{4}/, /_\d{1,2}(_|$)/
43
43
  add_offense(node, :expression) do
44
44
  self.config_to_allow_offenses = { 'Enabled' => false }
45
45
  end
@@ -12,15 +12,9 @@ module RuboCop
12
12
  'over `%s/then/else/end` constructs.'.freeze
13
13
 
14
14
  def on_normal_if_unless(node)
15
- exp = node.source
16
- return if exp.include?("\n")
15
+ return unless node.single_line? && node.else_branch
17
16
 
18
- condition = exp.include?('if') ? 'if' : 'unless'
19
- return unless node.loc.respond_to?(:else) &&
20
- node.loc.else &&
21
- else_branch_present?(node, condition)
22
-
23
- add_offense(node, :expression, format(MSG, condition))
17
+ add_offense(node, :expression, format(MSG, node.keyword))
24
18
  end
25
19
 
26
20
  def autocorrect(node)
@@ -30,20 +24,23 @@ module RuboCop
30
24
  end
31
25
 
32
26
  def replacement(node)
33
- return ternary(node) unless node.parent
34
- return "(#{ternary(node)})" if [:and, :or].include?(node.parent.type)
27
+ return to_ternary(node) unless node.parent
28
+
29
+ if [:and, :or].include?(node.parent.type)
30
+ return "(#{to_ternary(node)})"
31
+ end
35
32
 
36
33
  if node.parent.send_type? && operator?(node.parent.method_name)
37
- return "(#{ternary(node)})"
34
+ return "(#{to_ternary(node)})"
38
35
  end
39
36
 
40
- ternary(node)
37
+ to_ternary(node)
41
38
  end
42
39
 
43
- def ternary(node)
40
+ def to_ternary(node)
44
41
  cond, body, else_clause = *node
45
- "#{expr_replacement(cond)} ? #{expr_replacement(body)} : " +
46
- expr_replacement(else_clause)
42
+ "#{expr_replacement(cond)} ? #{expr_replacement(body)} : " \
43
+ "#{expr_replacement(else_clause)}"
47
44
  end
48
45
 
49
46
  def expr_replacement(node)
@@ -72,13 +69,6 @@ module RuboCop
72
69
 
73
70
  !parenthesized_call?(node)
74
71
  end
75
-
76
- private
77
-
78
- def else_branch_present?(node, condition)
79
- _, if_branch, else_branch = *node
80
- condition == 'if' ? else_branch : if_branch
81
- end
82
72
  end
83
73
  end
84
74
  end
@@ -25,23 +25,12 @@ module RuboCop
25
25
  def on_args(node)
26
26
  *_but_last, last_arg = *node
27
27
 
28
- # asserting that there was an argument at all
29
- return unless last_arg
30
-
31
- # asserting last argument is an optional argument
32
- return unless last_arg.optarg_type?
28
+ return unless last_arg && last_arg.optarg_type?
33
29
 
34
30
  arg, default_value = *last_arg
35
31
 
36
- # asserting default value is a hash
37
- return unless default_value.hash_type?
38
-
39
- # asserting default value is empty hash
40
- *key_value_pairs = *default_value
41
- return unless key_value_pairs.empty?
42
-
43
- # Check for suspicious argument names
44
- return unless name_in_suspicious_param_names?(arg)
32
+ return unless default_value.hash_type? && default_value.pairs.empty?
33
+ return unless suspicious_name?(arg)
45
34
 
46
35
  add_offense(last_arg, :expression, MSG)
47
36
  end
@@ -59,7 +48,7 @@ module RuboCop
59
48
 
60
49
  private
61
50
 
62
- def name_in_suspicious_param_names?(arg_name)
51
+ def suspicious_name?(arg_name)
63
52
  cop_config.key?('SuspiciousParamNames') &&
64
53
  cop_config['SuspiciousParamNames'].include?(arg_name.to_s)
65
54
  end
@@ -23,8 +23,6 @@ module RuboCop
23
23
  # b = 2
24
24
  # c = 3
25
25
  class ParallelAssignment < Cop
26
- include IfNode
27
-
28
26
  MSG = 'Do not use parallel assignment.'.freeze
29
27
 
30
28
  def on_masgn(node)
@@ -164,7 +162,7 @@ module RuboCop
164
162
 
165
163
  def modifier_statement?(node)
166
164
  node &&
167
- ((node.if_type? && modifier_if?(node)) ||
165
+ ((node.if_type? && node.modifier_form?) ||
168
166
  ((node.while_type? || node.until_type?) && modifier_while?(node)))
169
167
  end
170
168
 
@@ -6,12 +6,12 @@ module RuboCop
6
6
  # This cop checks for the presence of superfluous parentheses around the
7
7
  # condition of if/unless/while/until.
8
8
  class ParenthesesAroundCondition < Cop
9
- include IfNode
10
9
  include SafeAssignment
11
10
  include Parentheses
12
11
 
13
12
  def on_if(node)
14
- return if ternary?(node)
13
+ return if node.ternary?
14
+
15
15
  process_control_op(node)
16
16
  end
17
17
 
@@ -26,31 +26,27 @@ module RuboCop
26
26
  private
27
27
 
28
28
  def process_control_op(node)
29
- cond, _body = *node
29
+ cond = node.condition
30
30
 
31
31
  return unless cond.begin_type?
32
- # handle `if () ...`
33
- return if empty_condition?(cond)
34
- # handle `if (something rescue something_else) ...`
32
+ return if cond.children.empty?
35
33
  return if modifier_op?(cond.children.first)
36
- # check if there's any whitespace between the keyword and the cond
37
34
  return if parens_required?(node.children.first)
38
- # allow safe assignment
39
35
  return if safe_assignment?(cond) && safe_assignment_allowed?
40
36
 
41
37
  add_offense(cond, :expression, message(node))
42
38
  end
43
39
 
44
40
  def modifier_op?(node)
45
- return false if ternary?(node)
41
+ return false if node.if_type? && node.ternary?
46
42
  return true if node.rescue_type?
47
43
 
48
- [:if, :while, :until].include?(node.type) &&
49
- node.loc.end.nil?
44
+ MODIFIER_NODES.include?(node.type) &&
45
+ node.modifier_form?
50
46
  end
51
47
 
52
48
  def message(node)
53
- kw = node.loc.keyword.source
49
+ kw = node.keyword
54
50
  article = kw == 'while' ? 'a' : 'an'
55
51
  "Don't use parentheses around the condition of #{article} `#{kw}`."
56
52
  end