rubocop 0.78.0 → 0.82.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +158 -48
  5. data/lib/rubocop.rb +19 -4
  6. data/lib/rubocop/ast/builder.rb +45 -42
  7. data/lib/rubocop/ast/node.rb +12 -19
  8. data/lib/rubocop/ast/node/array_node.rb +13 -0
  9. data/lib/rubocop/ast/node/block_node.rb +5 -1
  10. data/lib/rubocop/ast/node/case_match_node.rb +56 -0
  11. data/lib/rubocop/ast/node/def_node.rb +11 -0
  12. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  13. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +8 -0
  14. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  15. data/lib/rubocop/ast/traversal.rb +29 -10
  16. data/lib/rubocop/cli.rb +10 -4
  17. data/lib/rubocop/cli/command/show_cops.rb +11 -4
  18. data/lib/rubocop/comment_config.rb +6 -1
  19. data/lib/rubocop/config.rb +36 -10
  20. data/lib/rubocop/config_loader.rb +42 -33
  21. data/lib/rubocop/config_loader_resolver.rb +1 -1
  22. data/lib/rubocop/config_obsoletion.rb +4 -1
  23. data/lib/rubocop/config_validator.rb +66 -92
  24. data/lib/rubocop/cop/autocorrect_logic.rb +6 -3
  25. data/lib/rubocop/cop/badge.rb +5 -5
  26. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
  27. data/lib/rubocop/cop/corrector.rb +48 -24
  28. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  29. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
  30. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  31. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
  32. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  33. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  34. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -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 +1 -1
  38. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
  39. data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
  40. data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
  41. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  42. data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
  43. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  44. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  45. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -10
  46. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  47. data/lib/rubocop/cop/layout/hash_alignment.rb +8 -4
  48. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  49. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  50. data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
  51. data/lib/rubocop/cop/layout/leading_comment_space.rb +34 -3
  52. data/lib/rubocop/cop/layout/line_length.rb +32 -3
  53. data/lib/rubocop/cop/layout/multiline_block_layout.rb +15 -6
  54. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  55. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  56. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
  57. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
  58. data/lib/rubocop/cop/layout/space_around_operators.rb +19 -1
  59. data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -2
  60. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  61. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  62. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
  63. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  64. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
  65. data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
  66. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  67. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  68. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  69. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  70. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  71. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  72. data/lib/rubocop/cop/lint/loop.rb +6 -4
  73. data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
  74. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  75. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  76. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  77. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
  78. data/lib/rubocop/cop/lint/raise_exception.rb +75 -0
  79. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
  80. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +12 -7
  81. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  82. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
  83. data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
  84. data/lib/rubocop/cop/lint/suppressed_exception.rb +12 -22
  85. data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
  86. data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
  87. data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
  88. data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -0
  89. data/lib/rubocop/cop/migration/department_name.rb +47 -6
  90. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  91. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
  92. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
  93. data/lib/rubocop/cop/mixin/hash_transform_method.rb +171 -0
  94. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  95. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
  96. data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -3
  97. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -10
  98. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  99. data/lib/rubocop/cop/naming/constant_name.rb +2 -1
  100. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  101. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  102. data/lib/rubocop/cop/naming/method_name.rb +30 -0
  103. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  104. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  105. data/lib/rubocop/cop/registry.rb +15 -3
  106. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
  107. data/lib/rubocop/cop/style/alias.rb +4 -4
  108. data/lib/rubocop/cop/style/and_or.rb +5 -6
  109. data/lib/rubocop/cop/style/array_join.rb +1 -1
  110. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  111. data/lib/rubocop/cop/style/case_equality.rb +24 -1
  112. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  113. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  114. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
  115. data/lib/rubocop/cop/style/copyright.rb +1 -1
  116. data/lib/rubocop/cop/style/dir.rb +1 -1
  117. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
  118. data/lib/rubocop/cop/style/documentation.rb +43 -5
  119. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
  120. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  121. data/lib/rubocop/cop/style/each_with_object.rb +3 -3
  122. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  123. data/lib/rubocop/cop/style/end_block.rb +6 -0
  124. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  125. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  126. data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
  127. data/lib/rubocop/cop/style/format_string.rb +2 -2
  128. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
  129. data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
  130. data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
  131. data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
  132. data/lib/rubocop/cop/style/hash_transform_values.rb +80 -0
  133. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
  134. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  135. data/lib/rubocop/cop/style/lambda.rb +3 -2
  136. data/lib/rubocop/cop/style/lambda_call.rb +2 -2
  137. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
  138. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
  139. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  140. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  141. data/lib/rubocop/cop/style/module_function.rb +58 -12
  142. data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
  143. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  144. data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
  145. data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
  146. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
  147. data/lib/rubocop/cop/style/next.rb +2 -2
  148. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  149. data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
  150. data/lib/rubocop/cop/style/not.rb +1 -1
  151. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  152. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  153. data/lib/rubocop/cop/style/numeric_predicate.rb +5 -4
  154. data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
  155. data/lib/rubocop/cop/style/or_assignment.rb +4 -3
  156. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  157. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  158. data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
  159. data/lib/rubocop/cop/style/proc.rb +1 -1
  160. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  161. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  162. data/lib/rubocop/cop/style/redundant_condition.rb +18 -6
  163. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  164. data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
  165. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  166. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  167. data/lib/rubocop/cop/style/redundant_return.rb +5 -7
  168. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  169. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  170. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  171. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  172. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  173. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  174. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  175. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
  176. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
  177. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  178. data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
  179. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
  180. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +35 -22
  181. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
  182. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +88 -0
  183. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
  184. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  185. data/lib/rubocop/cop/style/unpack_first.rb +0 -4
  186. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  187. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  188. data/lib/rubocop/cop/style/word_array.rb +1 -1
  189. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  190. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
  191. data/lib/rubocop/cop/variable_force.rb +4 -1
  192. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  193. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  194. data/lib/rubocop/formatter/junit_formatter.rb +74 -0
  195. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  196. data/lib/rubocop/formatter/tap_formatter.rb +1 -1
  197. data/lib/rubocop/node_pattern.rb +96 -10
  198. data/lib/rubocop/options.rb +7 -1
  199. data/lib/rubocop/processed_source.rb +1 -4
  200. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  201. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  202. data/lib/rubocop/rspec/shared_contexts.rb +5 -4
  203. data/lib/rubocop/runner.rb +1 -1
  204. data/lib/rubocop/target_ruby.rb +151 -0
  205. data/lib/rubocop/version.rb +1 -1
  206. metadata +39 -12
  207. data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
  208. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
  209. data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
@@ -60,6 +60,10 @@ module RuboCop
60
60
  alias conflicting_styles_detected no_acceptable_style!
61
61
  alias unrecognized_style_detected no_acceptable_style!
62
62
 
63
+ def style_configured?
64
+ cop_config.key?(style_parameter_name)
65
+ end
66
+
63
67
  def style
64
68
  @style ||= begin
65
69
  s = cop_config[style_parameter_name].to_sym
@@ -20,7 +20,7 @@ module RuboCop
20
20
  return if ignored_node?(node)
21
21
 
22
22
  end_loc = node.loc.end
23
- return unless end_loc # Discard modifier forms of if/while/until.
23
+ return if accept_end_kw_alignment?(end_loc)
24
24
 
25
25
  matching = matching_ranges(end_loc, align_ranges)
26
26
 
@@ -49,6 +49,11 @@ module RuboCop
49
49
  add_offense(node, location: end_loc, message: msg)
50
50
  end
51
51
 
52
+ def accept_end_kw_alignment?(end_loc)
53
+ end_loc.nil? || # Discard modifier forms of if/while/until.
54
+ processed_source.lines[end_loc.line - 1] !~ /\A[ \t]*end/
55
+ end
56
+
52
57
  def style_parameter_name
53
58
  'EnforcedStyleAlignWith'
54
59
  end
@@ -39,15 +39,15 @@ module RuboCop
39
39
  end
40
40
  end
41
41
 
42
- def leading_comment_lines
43
- comments = processed_source.comments
44
-
45
- comments.each_with_object([]) do |comment, leading_comments|
46
- next if comment.loc.line > 3
47
-
48
- leading_comments << comment.text
42
+ def frozen_string_literal_specified?
43
+ leading_comment_lines.any? do |line|
44
+ MagicComment.parse(line).frozen_string_literal_specified?
49
45
  end
50
46
  end
47
+
48
+ def leading_comment_lines
49
+ processed_source.comments.first(3).map(&:text)
50
+ end
51
51
  end
52
52
  end
53
53
  end
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Common functionality for Style/HashTransformKeys and
6
+ # Style/HashTransformValues
7
+ module HashTransformMethod
8
+ def on_block(node)
9
+ on_bad_each_with_object(node) do |*match|
10
+ handle_possible_offense(node, match, 'each_with_object')
11
+ end
12
+ end
13
+
14
+ def on_send(node)
15
+ on_bad_hash_brackets_map(node) do |*match|
16
+ handle_possible_offense(node, match, 'Hash[_.map {...}]')
17
+ end
18
+ on_bad_map_to_h(node) do |*match|
19
+ handle_possible_offense(node, match, 'map {...}.to_h')
20
+ end
21
+ end
22
+
23
+ def on_csend(node)
24
+ on_bad_map_to_h(node) do |*match|
25
+ handle_possible_offense(node, match, 'map {...}.to_h')
26
+ end
27
+ end
28
+
29
+ def autocorrect(node)
30
+ lambda do |corrector|
31
+ correction = prepare_correction(node)
32
+ execute_correction(corrector, node, correction)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ # @abstract Implemented with `def_node_matcher`
39
+ def on_bad_each_with_object(_node)
40
+ raise NotImplementedError
41
+ end
42
+
43
+ # @abstract Implemented with `def_node_matcher`
44
+ def on_bad_hash_brackets_map(_node)
45
+ raise NotImplementedError
46
+ end
47
+
48
+ # @abstract Implemented with `def_node_matcher`
49
+ def on_bad_map_to_h(_node)
50
+ raise NotImplementedError
51
+ end
52
+
53
+ def handle_possible_offense(node, match, match_desc)
54
+ captures = extract_captures(match)
55
+
56
+ # If key didn't actually change either, this is most likely a false
57
+ # positive (receiver isn't a hash).
58
+ return if captures.noop_transformation?
59
+
60
+ # Can't `transform_keys` if key transformation uses value, or
61
+ # `transform_values` if value transformation uses key.
62
+ return if captures.transformation_uses_both_args?
63
+
64
+ add_offense(
65
+ node,
66
+ message: "Prefer `#{new_method_name}` over `#{match_desc}`."
67
+ )
68
+ end
69
+
70
+ # @abstract
71
+ #
72
+ # @return [Captures]
73
+ def extract_captures(_match)
74
+ raise NotImplementedError
75
+ end
76
+
77
+ # @abstract
78
+ #
79
+ # @return [String]
80
+ def new_method_name
81
+ raise NotImplementedError
82
+ end
83
+
84
+ def prepare_correction(node)
85
+ if (match = on_bad_each_with_object(node))
86
+ Autocorrection.from_each_with_object(node, match)
87
+ elsif (match = on_bad_hash_brackets_map(node))
88
+ Autocorrection.from_hash_brackets_map(node, match)
89
+ elsif (match = on_bad_map_to_h(node))
90
+ Autocorrection.from_map_to_h(node, match)
91
+ else
92
+ raise 'unreachable'
93
+ end
94
+ end
95
+
96
+ def execute_correction(corrector, node, correction)
97
+ correction.strip_prefix_and_suffix(node, corrector)
98
+ correction.set_new_method_name(new_method_name, corrector)
99
+
100
+ captures = extract_captures(correction.match)
101
+ correction.set_new_arg_name(captures.transformed_argname, corrector)
102
+ correction.set_new_body_expression(
103
+ captures.transforming_body_expr,
104
+ corrector
105
+ )
106
+ end
107
+
108
+ # Internal helper class to hold match data
109
+ Captures = Struct.new(
110
+ :transformed_argname,
111
+ :transforming_body_expr,
112
+ :unchanged_body_expr
113
+ ) do
114
+ def noop_transformation?
115
+ transforming_body_expr.lvar_type? &&
116
+ transforming_body_expr.children == [transformed_argname]
117
+ end
118
+
119
+ def transformation_uses_both_args?
120
+ transforming_body_expr.descendants.include?(unchanged_body_expr)
121
+ end
122
+ end
123
+
124
+ # Internal helper class to hold autocorrect data
125
+ Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do # rubocop:disable Metrics/BlockLength
126
+ def self.from_each_with_object(node, match)
127
+ new(match, node, 0, 0)
128
+ end
129
+
130
+ def self.from_hash_brackets_map(node, match)
131
+ new(match, node.children.last, 'Hash['.length, ']'.length)
132
+ end
133
+
134
+ def self.from_map_to_h(node, match)
135
+ strip_trailing_chars = node.parent&.block_type? ? 0 : '.to_h'.length
136
+ new(match, node.children.first, 0, strip_trailing_chars)
137
+ end
138
+
139
+ def strip_prefix_and_suffix(node, corrector)
140
+ expression = node.loc.expression
141
+ corrector.remove_leading(expression, leading)
142
+ corrector.remove_trailing(expression, trailing)
143
+ end
144
+
145
+ def set_new_method_name(new_method_name, corrector)
146
+ range = block_node.send_node.loc.selector
147
+ if (send_end = block_node.send_node.loc.end)
148
+ # If there are arguments (only true in the `each_with_object`
149
+ # case)
150
+ range = range.begin.join(send_end)
151
+ end
152
+ corrector.replace(range, new_method_name)
153
+ end
154
+
155
+ def set_new_arg_name(transformed_argname, corrector)
156
+ corrector.replace(
157
+ block_node.arguments.loc.expression,
158
+ "|#{transformed_argname}|"
159
+ )
160
+ end
161
+
162
+ def set_new_body_expression(transforming_body_expr, corrector)
163
+ corrector.replace(
164
+ block_node.body,
165
+ transforming_body_expr.loc.expression.source
166
+ )
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -63,7 +63,7 @@ module RuboCop
63
63
  end
64
64
 
65
65
  def tab_indentation_width
66
- config.for_cop('Layout/Tab')['IndentationWidth']
66
+ config.for_cop('Layout/IndentationStyle')['IndentationWidth']
67
67
  end
68
68
 
69
69
  def uri_regexp
@@ -5,15 +5,20 @@ module RuboCop
5
5
  # This module handles measurement and reporting of complexity in methods.
6
6
  module MethodComplexity
7
7
  include ConfigurableMax
8
+ include IgnoredMethods
8
9
  extend NodePattern::Macros
9
10
 
10
11
  def on_def(node)
12
+ return if ignored_method?(node.method_name)
13
+
11
14
  check_complexity(node, node.method_name)
12
15
  end
13
16
  alias on_defs on_def
14
17
 
15
18
  def on_block(node)
16
19
  define_method?(node) do |name|
20
+ return if ignored_method?(name)
21
+
17
22
  check_complexity(node, name)
18
23
  end
19
24
  end
@@ -58,12 +58,13 @@ module RuboCop
58
58
  end
59
59
 
60
60
  def indentation_multiplier
61
- return 1 if config.for_cop('Layout/Tab')['Enabled']
61
+ return 1 if config.for_cop('Layout/IndentationStyle')['Enabled']
62
62
 
63
63
  default_configuration = RuboCop::ConfigLoader.default_configuration
64
- config.for_cop('Layout/Tab')['IndentationWidth'] ||
64
+ config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
65
65
  config.for_cop('Layout/IndentationWidth')['Width'] ||
66
- default_configuration.for_cop('Layout/Tab')['IndentationWidth'] ||
66
+ default_configuration
67
+ .for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
67
68
  default_configuration.for_cop('Layout/IndentationWidth')['Width']
68
69
  end
69
70
  end
@@ -23,7 +23,7 @@ module RuboCop
23
23
  if comma_offset && !inside_comment?(after_last_item, comma_offset)
24
24
  check_comma(node, kind, after_last_item.begin_pos + comma_offset)
25
25
  elsif should_have_comma?(style, node)
26
- put_comma(node, items, kind)
26
+ put_comma(items, kind)
27
27
  end
28
28
  end
29
29
 
@@ -135,7 +135,7 @@ module RuboCop
135
135
 
136
136
  def avoid_comma(kind, comma_begin_pos, extra_info)
137
137
  range = range_between(comma_begin_pos, comma_begin_pos + 1)
138
- article = kind =~ /array/ ? 'an' : 'a'
138
+ article = /array/.match?(kind) ? 'an' : 'a'
139
139
  msg = format(
140
140
  MSG,
141
141
  command: 'Avoid',
@@ -145,9 +145,7 @@ module RuboCop
145
145
  add_offense(range, location: range, message: msg)
146
146
  end
147
147
 
148
- def put_comma(node, items, kind)
149
- return if avoid_autocorrect?(elements(node))
150
-
148
+ def put_comma(items, kind)
151
149
  last_item = items.last
152
150
  return if last_item.block_pass_type?
153
151
 
@@ -169,11 +167,6 @@ module RuboCop
169
167
  range_between(expr.begin_pos + ix, expr.end_pos)
170
168
  end
171
169
 
172
- # By default, there's no reason to avoid auto-correct.
173
- def avoid_autocorrect?(_nodes)
174
- false
175
- end
176
-
177
170
  def any_heredoc?(items)
178
171
  items.any? { |item| heredoc?(item) }
179
172
  end
@@ -22,7 +22,7 @@ module RuboCop
22
22
  MSG = 'Use CamelCase for classes and modules.'
23
23
 
24
24
  def on_class(node)
25
- return unless node.loc.name.source =~ /_/
25
+ return unless /_/.match?(node.loc.name.source)
26
26
 
27
27
  add_offense(node, location: :name)
28
28
  end
@@ -45,8 +45,9 @@ module RuboCop
45
45
  # SomeClass = Class.new(...)
46
46
  # SomeClass = Struct.new(...)
47
47
  return if allowed_assignment?(value)
48
+ return if SNAKE_CASE.match?(const_name)
48
49
 
49
- add_offense(node, location: :name) if const_name !~ SNAKE_CASE
50
+ add_offense(node, location: :name)
50
51
  end
51
52
 
52
53
  private
@@ -39,7 +39,7 @@ module RuboCop
39
39
  def meaningful_delimiters?(node)
40
40
  delimiters = delimiter_string(node)
41
41
 
42
- return false unless delimiters =~ /\w/
42
+ return false unless /\w/.match?(delimiters)
43
43
 
44
44
  forbidden_delimiters.none? do |forbidden_delimiter|
45
45
  delimiters =~ Regexp.new(forbidden_delimiter)
@@ -10,7 +10,7 @@ module RuboCop
10
10
  # directive. It can be configured to allow for memoized instance variables
11
11
  # prefixed with an underscore. Prefixing ivars with an underscore is a
12
12
  # convention that is used to implicitly indicate that an ivar should not
13
- # be set or referencd outside of the memoization method.
13
+ # be set or referenced outside of the memoization method.
14
14
  #
15
15
  # @example EnforcedStyleForLeadingUnderscores: disallowed (default)
16
16
  # # bad
@@ -31,9 +31,28 @@ module RuboCop
31
31
  class MethodName < Cop
32
32
  include ConfigurableNaming
33
33
  include IgnoredPattern
34
+ include RangeHelp
34
35
 
35
36
  MSG = 'Use %<style>s for method names.'
36
37
 
38
+ def_node_matcher :attr?, <<~PATTERN
39
+ (send nil? ${:attr_reader :attr_writer :attr_accessor :attr} $...)
40
+ PATTERN
41
+
42
+ def_node_matcher :sym_name, '(sym $_name)'
43
+ def_node_matcher :str_name, '(str $_name)'
44
+
45
+ def on_send(node)
46
+ return unless (attrs = attr?(node))
47
+
48
+ attrs.last.each do |name_item|
49
+ name = attr_name(name_item)
50
+ next if !name || matches_ignored_pattern?(name)
51
+
52
+ check_name(node, name, range_position(node))
53
+ end
54
+ end
55
+
37
56
  def on_def(node)
38
57
  return if node.operator_method? ||
39
58
  matches_ignored_pattern?(node.method_name)
@@ -44,6 +63,17 @@ module RuboCop
44
63
 
45
64
  private
46
65
 
66
+ def attr_name(name_item)
67
+ sym_name(name_item) || str_name(name_item)
68
+ end
69
+
70
+ def range_position(node)
71
+ selector_end_pos = node.loc.selector.end_pos + 1
72
+ expr_end_pos = node.loc.expression.end_pos
73
+
74
+ range_between(selector_end_pos, expr_end_pos)
75
+ end
76
+
47
77
  def message(style)
48
78
  format(MSG, style: style)
49
79
  end
@@ -49,7 +49,7 @@ module RuboCop
49
49
  def on_def(node)
50
50
  return unless node.arguments?
51
51
 
52
- check(node, node.arguments)
52
+ check(node, node.arguments.reject(&:forward_args_type?))
53
53
  end
54
54
  alias on_defs on_def
55
55
  end
@@ -73,7 +73,7 @@ module RuboCop
73
73
  node.body&.each_descendant(:lvar) do |var|
74
74
  next unless var.children.first == offending_name
75
75
 
76
- corrector.replace(var.loc.expression, preferred_name)
76
+ corrector.replace(var, preferred_name)
77
77
  end
78
78
  end
79
79
  end
@@ -22,12 +22,13 @@ module RuboCop
22
22
 
23
23
  # Registry that tracks all cops by their badge and department.
24
24
  class Registry
25
- def initialize(cops = [])
25
+ def initialize(cops = [], options = {})
26
26
  @registry = {}
27
27
  @departments = {}
28
28
  @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] }
29
29
 
30
30
  cops.each { |cop| enlist(cop) }
31
+ @options = options
31
32
  end
32
33
 
33
34
  def enlist(cop)
@@ -146,13 +147,24 @@ module RuboCop
146
147
 
147
148
  def enabled?(cop, config, only_safe)
148
149
  cfg = config.for_cop(cop)
150
+
151
+ cop_enabled = cfg.fetch('Enabled') == true ||
152
+ enabled_pending_cop?(cfg, config)
153
+
149
154
  if only_safe
150
- cfg.fetch('Enabled') && cfg.fetch('Safe', true)
155
+ cop_enabled && cfg.fetch('Safe', true)
151
156
  else
152
- cfg.fetch('Enabled')
157
+ cop_enabled
153
158
  end
154
159
  end
155
160
 
161
+ def enabled_pending_cop?(cop_cfg, config)
162
+ return false if @options[:disable_pending_cops]
163
+
164
+ cop_cfg.fetch('Enabled') == 'pending' &&
165
+ (@options[:enable_pending_cops] || config.enabled_new_cops?)
166
+ end
167
+
156
168
  def names
157
169
  cops.map(&:cop_name)
158
170
  end