rubocop 0.75.0 → 0.79.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +341 -316
  4. data/lib/rubocop.rb +48 -31
  5. data/lib/rubocop/ast/builder.rb +43 -41
  6. data/lib/rubocop/ast/node.rb +5 -1
  7. data/lib/rubocop/ast/node/block_node.rb +2 -0
  8. data/lib/rubocop/ast/node/def_node.rb +11 -0
  9. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  10. data/lib/rubocop/ast/node/return_node.rb +24 -0
  11. data/lib/rubocop/ast/traversal.rb +11 -3
  12. data/lib/rubocop/cli.rb +11 -227
  13. data/lib/rubocop/cli/command.rb +21 -0
  14. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  15. data/lib/rubocop/cli/command/base.rb +33 -0
  16. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  17. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  18. data/lib/rubocop/cli/command/show_cops.rb +80 -0
  19. data/lib/rubocop/cli/command/version.rb +17 -0
  20. data/lib/rubocop/cli/environment.rb +21 -0
  21. data/lib/rubocop/comment_config.rb +2 -2
  22. data/lib/rubocop/config.rb +8 -1
  23. data/lib/rubocop/config_loader.rb +20 -20
  24. data/lib/rubocop/config_loader_resolver.rb +2 -1
  25. data/lib/rubocop/config_obsoletion.rb +73 -11
  26. data/lib/rubocop/config_validator.rb +77 -110
  27. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  28. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  30. data/lib/rubocop/cop/commissioner.rb +15 -7
  31. data/lib/rubocop/cop/cop.rb +31 -6
  32. data/lib/rubocop/cop/corrector.rb +8 -7
  33. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  34. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  35. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  36. data/lib/rubocop/cop/generator.rb +3 -4
  37. data/lib/rubocop/cop/generator/configuration_injector.rb +2 -2
  38. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  39. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  40. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  41. data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
  42. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +3 -2
  43. data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
  44. data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
  45. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +22 -7
  46. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
  47. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
  48. data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
  49. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  50. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +14 -12
  51. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  52. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
  53. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  54. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +16 -8
  55. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
  56. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  57. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +41 -114
  58. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  59. data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
  60. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  61. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  62. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
  63. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  64. data/lib/rubocop/cop/layout/space_around_operators.rb +32 -7
  65. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  66. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
  67. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
  68. data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
  69. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  70. data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
  71. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  72. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  73. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  74. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  75. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
  76. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  77. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
  78. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  79. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  80. data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +26 -26
  81. data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +10 -12
  82. data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
  83. data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +6 -6
  84. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  85. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  86. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  87. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
  88. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  89. data/lib/rubocop/cop/lint/unused_block_argument.rb +22 -6
  90. data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
  91. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  92. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  93. data/lib/rubocop/cop/lint/void.rb +7 -26
  94. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  95. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  96. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
  97. data/lib/rubocop/cop/migration/department_name.rb +16 -1
  98. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  99. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -7
  100. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  101. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  102. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  103. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  104. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  105. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -4
  106. data/lib/rubocop/cop/mixin/trailing_comma.rb +14 -9
  107. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  108. data/lib/rubocop/cop/naming/file_name.rb +12 -5
  109. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  110. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
  111. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  112. data/lib/rubocop/cop/offense.rb +11 -0
  113. data/lib/rubocop/cop/registry.rb +8 -3
  114. data/lib/rubocop/cop/style/alias.rb +1 -1
  115. data/lib/rubocop/cop/style/array_join.rb +1 -1
  116. data/lib/rubocop/cop/style/attr.rb +10 -2
  117. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -6
  118. data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
  119. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  120. data/lib/rubocop/cop/style/copyright.rb +11 -7
  121. data/lib/rubocop/cop/style/documentation_method.rb +44 -0
  122. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
  123. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
  124. data/lib/rubocop/cop/style/empty_literal.rb +2 -2
  125. data/lib/rubocop/cop/style/empty_method.rb +5 -5
  126. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  127. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  128. data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
  129. data/lib/rubocop/cop/style/format_string.rb +10 -7
  130. data/lib/rubocop/cop/style/format_string_token.rb +15 -34
  131. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +10 -0
  132. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  133. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  134. data/lib/rubocop/cop/style/if_unless_modifier.rb +45 -3
  135. data/lib/rubocop/cop/style/infinite_loop.rb +5 -4
  136. data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
  137. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  138. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
  139. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +24 -227
  140. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +168 -0
  141. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  142. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
  143. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  144. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  145. data/lib/rubocop/cop/style/multiline_when_then.rb +6 -2
  146. data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
  147. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
  148. data/lib/rubocop/cop/style/next.rb +5 -5
  149. data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
  150. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  151. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  152. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  153. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  154. data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
  155. data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
  156. data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
  157. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  158. data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
  159. data/lib/rubocop/cop/style/redundant_return.rb +39 -29
  160. data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
  161. data/lib/rubocop/cop/style/safe_navigation.rb +19 -8
  162. data/lib/rubocop/cop/style/semicolon.rb +13 -2
  163. data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
  164. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  165. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  166. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  167. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  168. data/lib/rubocop/cop/team.rb +5 -0
  169. data/lib/rubocop/cop/util.rb +1 -1
  170. data/lib/rubocop/cop/utils/format_string.rb +10 -18
  171. data/lib/rubocop/cop/variable_force.rb +7 -5
  172. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  173. data/lib/rubocop/formatter/clang_style_formatter.rb +9 -6
  174. data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -12
  175. data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
  176. data/lib/rubocop/formatter/formatter_set.rb +16 -16
  177. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  178. data/lib/rubocop/formatter/pacman_formatter.rb +3 -3
  179. data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
  180. data/lib/rubocop/formatter/tap_formatter.rb +9 -6
  181. data/lib/rubocop/node_pattern.rb +4 -2
  182. data/lib/rubocop/options.rb +20 -26
  183. data/lib/rubocop/processed_source.rb +1 -1
  184. data/lib/rubocop/rake_task.rb +1 -0
  185. data/lib/rubocop/result_cache.rb +24 -8
  186. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  187. data/lib/rubocop/runner.rb +50 -29
  188. data/lib/rubocop/target_finder.rb +12 -6
  189. data/lib/rubocop/target_ruby.rb +151 -0
  190. data/lib/rubocop/version.rb +1 -1
  191. metadata +50 -34
  192. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ class MethodCallWithArgsParentheses
7
+ # Style omit_parentheses
8
+ module OmitParentheses
9
+ TRAILING_WHITESPACE_REGEX = /\s+\Z/.freeze
10
+
11
+ def on_send(node)
12
+ return unless node.parenthesized?
13
+ return if node.implicit_call?
14
+ return if super_call_without_arguments?(node)
15
+ return if allowed_camel_case_method_call?(node)
16
+ return if legitimate_call_with_parentheses?(node)
17
+
18
+ add_offense(node, location: node.loc.begin.join(node.loc.end))
19
+ end
20
+ alias on_csend on_send
21
+ alias on_super on_send
22
+ alias on_yield on_send
23
+
24
+ def autocorrect(node)
25
+ lambda do |corrector|
26
+ if parentheses_at_the_end_of_multiline_call?(node)
27
+ corrector.replace(args_begin(node), ' \\')
28
+ else
29
+ corrector.replace(args_begin(node), ' ')
30
+ end
31
+ corrector.remove(node.loc.end)
32
+ end
33
+ end
34
+
35
+ def message(_node = nil)
36
+ 'Omit parentheses for method calls with arguments.'
37
+ end
38
+
39
+ private
40
+
41
+ def super_call_without_arguments?(node)
42
+ node.super_type? && node.arguments.none?
43
+ end
44
+
45
+ def allowed_camel_case_method_call?(node)
46
+ node.camel_case_method? &&
47
+ (node.arguments.none? ||
48
+ cop_config['AllowParenthesesInCamelCaseMethod'])
49
+ end
50
+
51
+ def parentheses_at_the_end_of_multiline_call?(node)
52
+ node.multiline? &&
53
+ node.loc.begin.source_line
54
+ .gsub(TRAILING_WHITESPACE_REGEX, '')
55
+ .end_with?('(')
56
+ end
57
+
58
+ def legitimate_call_with_parentheses?(node)
59
+ call_in_literals?(node) ||
60
+ call_with_ambiguous_arguments?(node) ||
61
+ call_in_logical_operators?(node) ||
62
+ call_in_optional_arguments?(node) ||
63
+ allowed_multiline_call_with_parentheses?(node) ||
64
+ allowed_chained_call_with_parentheses?(node)
65
+ end
66
+
67
+ def call_in_literals?(node)
68
+ node.parent &&
69
+ (node.parent.pair_type? ||
70
+ node.parent.array_type? ||
71
+ node.parent.range_type? ||
72
+ splat?(node.parent) ||
73
+ ternary_if?(node.parent))
74
+ end
75
+
76
+ def call_in_logical_operators?(node)
77
+ node.parent &&
78
+ (logical_operator?(node.parent) ||
79
+ node.parent.send_type? &&
80
+ node.parent.arguments.any?(&method(:logical_operator?)))
81
+ end
82
+
83
+ def call_in_optional_arguments?(node)
84
+ node.parent &&
85
+ (node.parent.optarg_type? || node.parent.kwoptarg_type?)
86
+ end
87
+
88
+ def call_with_ambiguous_arguments?(node)
89
+ call_with_braced_block?(node) ||
90
+ call_as_argument_or_chain?(node) ||
91
+ hash_literal_in_arguments?(node) ||
92
+ node.descendants.any? do |n|
93
+ ambigious_literal?(n) || logical_operator?(n) ||
94
+ call_with_braced_block?(n)
95
+ end
96
+ end
97
+
98
+ def call_with_braced_block?(node)
99
+ (node.send_type? || node.super_type?) &&
100
+ node.block_node && node.block_node.braces?
101
+ end
102
+
103
+ def call_as_argument_or_chain?(node)
104
+ node.parent &&
105
+ (node.parent.send_type? && !assigned_before?(node.parent, node) ||
106
+ node.parent.csend_type? || node.parent.super_type?)
107
+ end
108
+
109
+ def hash_literal_in_arguments?(node)
110
+ node.arguments.any? do |n|
111
+ hash_literal?(n) ||
112
+ n.send_type? && node.descendants.any?(&method(:hash_literal?))
113
+ end
114
+ end
115
+
116
+ def allowed_multiline_call_with_parentheses?(node)
117
+ cop_config['AllowParenthesesInMultilineCall'] && node.multiline?
118
+ end
119
+
120
+ def allowed_chained_call_with_parentheses?(node)
121
+ return false unless cop_config['AllowParenthesesInChaining']
122
+
123
+ previous = node.descendants.first
124
+ return false unless previous&.send_type?
125
+
126
+ previous.parenthesized? ||
127
+ allowed_chained_call_with_parentheses?(previous)
128
+ end
129
+
130
+ def ambigious_literal?(node)
131
+ splat?(node) || ternary_if?(node) || regexp_slash_literal?(node) ||
132
+ unary_literal?(node)
133
+ end
134
+
135
+ def splat?(node)
136
+ node.splat_type? || node.kwsplat_type? || node.block_pass_type?
137
+ end
138
+
139
+ def ternary_if?(node)
140
+ node.if_type? && node.ternary?
141
+ end
142
+
143
+ def logical_operator?(node)
144
+ (node.and_type? || node.or_type?) && node.logical_operator?
145
+ end
146
+
147
+ def hash_literal?(node)
148
+ node.hash_type? && node.braces?
149
+ end
150
+
151
+ def regexp_slash_literal?(node)
152
+ node.regexp_type? && node.loc.begin.source == '/'
153
+ end
154
+
155
+ def unary_literal?(node)
156
+ node.numeric_type? && node.sign? ||
157
+ node.parent&.send_type? && node.parent&.unary_operation?
158
+ end
159
+
160
+ def assigned_before?(node, target)
161
+ node.assignment? &&
162
+ node.loc.operator.begin < target.loc.begin
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ class MethodCallWithArgsParentheses
7
+ # Style require_parentheses
8
+ module RequireParentheses
9
+ def on_send(node)
10
+ return if ignored_method?(node.method_name)
11
+ return if matches_ignored_pattern?(node.method_name)
12
+ return if eligible_for_parentheses_omission?(node)
13
+ return unless node.arguments? && !node.parenthesized?
14
+
15
+ add_offense(node)
16
+ end
17
+ alias on_csend on_send
18
+ alias on_super on_send
19
+ alias on_yield on_send
20
+
21
+ def autocorrect(node)
22
+ lambda do |corrector|
23
+ corrector.replace(args_begin(node), '(')
24
+
25
+ unless args_parenthesized?(node)
26
+ corrector.insert_after(args_end(node), ')')
27
+ end
28
+ end
29
+ end
30
+
31
+ def message(_node = nil)
32
+ 'Use parentheses for method calls with arguments.'
33
+ end
34
+
35
+ private
36
+
37
+ def eligible_for_parentheses_omission?(node)
38
+ node.operator_method? || node.setter_method? || ignored_macro?(node)
39
+ end
40
+
41
+ def included_macros_list
42
+ cop_config.fetch('IncludedMacros', []).map(&:to_sym)
43
+ end
44
+
45
+ def ignored_macro?(node)
46
+ cop_config['IgnoreMacros'] &&
47
+ node.macro? &&
48
+ !included_macros_list.include?(node.method_name)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -113,22 +113,30 @@ module RuboCop
113
113
  lambda do |corrector|
114
114
  if node.args_type?
115
115
  # offense is registered on args node when parentheses are unwanted
116
- corrector.replace(node.loc.begin, ' ')
117
- corrector.remove(node.loc.end)
116
+ correct_arguments(node, corrector)
118
117
  else
119
- args_expr = node.arguments.source_range
120
- args_with_space = range_with_surrounding_space(range: args_expr,
121
- side: :left)
122
- just_space = range_between(args_with_space.begin_pos,
123
- args_expr.begin_pos)
124
- corrector.replace(just_space, '(')
125
- corrector.insert_after(args_expr, ')')
118
+ correct_definition(node, corrector)
126
119
  end
127
120
  end
128
121
  end
129
122
 
130
123
  private
131
124
 
125
+ def correct_arguments(arg_node, corrector)
126
+ corrector.replace(arg_node.loc.begin, ' ')
127
+ corrector.remove(arg_node.loc.end)
128
+ end
129
+
130
+ def correct_definition(def_node, corrector)
131
+ arguments_range = def_node.arguments.source_range
132
+ args_with_space = range_with_surrounding_space(range: arguments_range,
133
+ side: :left)
134
+ leading_space = range_between(args_with_space.begin_pos,
135
+ arguments_range.begin_pos)
136
+ corrector.replace(leading_space, '(')
137
+ corrector.insert_after(arguments_range, ')')
138
+ end
139
+
132
140
  def require_parentheses?(args)
133
141
  style == :require_parentheses ||
134
142
  (style == :require_no_parentheses_except_multiline &&
@@ -103,7 +103,7 @@ module RuboCop
103
103
  .select(&:macro?)
104
104
 
105
105
  siblings.select do |sibling_node|
106
- sibling_node.method_name == send_node.method_name
106
+ sibling_node.method?(send_node.method_name)
107
107
  end
108
108
  end
109
109
 
@@ -53,7 +53,7 @@ module RuboCop
53
53
  end
54
54
 
55
55
  def max_line_length
56
- config.for_cop('Metrics/LineLength')['Max'] || 80
56
+ config.for_cop('Layout/LineLength')['Max'] || 80
57
57
  end
58
58
  end
59
59
  end
@@ -35,7 +35,7 @@ module RuboCop
35
35
  return if !node.children.last.nil? && !node.multiline? && node.then?
36
36
 
37
37
  # With more than one statements after then, there's not offense
38
- return if node.children.last&.begin_type?
38
+ return if accept_node_type?(node.body)
39
39
 
40
40
  add_offense(node, location: :begin)
41
41
  end
@@ -44,11 +44,15 @@ module RuboCop
44
44
  lambda do |corrector|
45
45
  corrector.remove(
46
46
  range_with_surrounding_space(
47
- range: node.loc.begin, side: :left
47
+ range: node.loc.begin, side: :left, newlines: false
48
48
  )
49
49
  )
50
50
  end
51
51
  end
52
+
53
+ def accept_node_type?(node)
54
+ node&.begin_type? || node&.array_type? || node&.hash_type?
55
+ end
52
56
  end
53
57
  end
54
58
  end
@@ -49,11 +49,13 @@ module RuboCop
49
49
  node.parent.condition.source_range.end_pos)
50
50
 
51
51
  lambda do |corrector|
52
- corrector.replace(range, new_expression(node.parent, node))
52
+ corrector.replace(range, new_expression(node))
53
53
  end
54
54
  end
55
55
 
56
- def new_expression(outer_node, inner_node)
56
+ def new_expression(inner_node)
57
+ outer_node = inner_node.parent
58
+
57
59
  operator = replacement_operator(outer_node.keyword)
58
60
  lh_operand = left_hand_operand(outer_node, operator)
59
61
  rh_operand = right_hand_operand(inner_node, outer_node.keyword)
@@ -49,17 +49,17 @@ module RuboCop
49
49
  def allowed_omission?(send_node)
50
50
  !send_node.arguments? || send_node.parenthesized? ||
51
51
  send_node.setter_method? || send_node.operator_method? ||
52
- whitelisted?(send_node)
52
+ allowed?(send_node)
53
53
  end
54
54
 
55
- def whitelisted?(send_node)
55
+ def allowed?(send_node)
56
56
  send_node.parent.arguments.one? &&
57
- whitelisted_methods.include?(send_node.method_name.to_s) &&
57
+ allowed_methods.include?(send_node.method_name.to_s) &&
58
58
  send_node.arguments.one?
59
59
  end
60
60
 
61
- def whitelisted_methods
62
- cop_config['Whitelist'] || []
61
+ def allowed_methods
62
+ cop_config['AllowedMethods'] || []
63
63
  end
64
64
  end
65
65
  end
@@ -20,18 +20,18 @@ module RuboCop
20
20
  # end
21
21
  #
22
22
  # # good
23
- # [1, 2].each do |o|
24
- # puts o unless o == 1
23
+ # [1, 2].each do |a|
24
+ # puts a if a == 1
25
25
  # end
26
26
  #
27
27
  # @example EnforcedStyle: always
28
28
  # # With `always` all conditions at the end of an iteration needs to be
29
29
  # # replaced by next - with `skip_modifier_ifs` the modifier if like
30
- # # this one are ignored: `[1, 2].each { |a| return 'yes' if a == 1 }`
30
+ # # this one are ignored: `[1, 2].each { |a| puts a if a == 1 }`
31
31
  #
32
32
  # # bad
33
- # [1, 2].each do |o|
34
- # puts o unless o == 1
33
+ # [1, 2].each do |a|
34
+ # puts a if a == 1
35
35
  # end
36
36
  #
37
37
  # # bad
@@ -5,27 +5,39 @@ module RuboCop
5
5
  module Style
6
6
  # This cop checks for non-nil checks, which are usually redundant.
7
7
  #
8
- # @example
8
+ # With `IncludeSemanticChanges` set to `false` by default, this cop
9
+ # does not report offenses for `!x.nil?` and does no changes that might
10
+ # change behavior.
11
+ #
12
+ # With `IncludeSemanticChanges` set to `true`, this cop reports offenses
13
+ # for `!x.nil?` and autocorrects that and `x != nil` to solely `x`, which
14
+ # is **usually** OK, but might change behavior.
9
15
  #
16
+ # @example
10
17
  # # bad
11
18
  # if x != nil
12
19
  # end
13
20
  #
14
- # # good (when not allowing semantic changes)
15
- # # bad (when allowing semantic changes)
16
- # if !x.nil?
17
- # end
18
- #
19
- # # good (when allowing semantic changes)
21
+ # # good
20
22
  # if x
21
23
  # end
22
24
  #
23
- # Non-nil checks are allowed if they are the final nodes of predicate.
24
- #
25
+ # # Non-nil checks are allowed if they are the final nodes of predicate.
25
26
  # # good
26
27
  # def signed_in?
27
28
  # !current_user.nil?
28
29
  # end
30
+ #
31
+ # @example IncludeSemanticChanges: false (default)
32
+ # # good
33
+ # if !x.nil?
34
+ # end
35
+ #
36
+ # @example IncludeSemanticChanges: true
37
+ # # bad
38
+ # if !x.nil?
39
+ # end
40
+ #
29
41
  class NonNilCheck < Cop
30
42
  def_node_matcher :not_equal_to_nil?, '(send _ :!= nil)'
31
43
  def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
@@ -9,18 +9,22 @@ module RuboCop
9
9
  # @example
10
10
  #
11
11
  # # bad
12
- #
13
12
  # 1000000
14
13
  # 1_00_000
15
14
  # 1_0000
16
15
  #
17
16
  # # good
18
- #
19
17
  # 1_000_000
20
18
  # 1000
21
19
  #
22
- # # good unless Strict is set
20
+ # @example Strict: false (default)
23
21
  #
22
+ # # good
23
+ # 10_000_00 # typical representation of $10,000 in cents
24
+ #
25
+ # @example Strict: true
26
+ #
27
+ # # bad
24
28
  # 10_000_00 # typical representation of $10,000 in cents
25
29
  #
26
30
  class NumericLiterals < Cop