rubocop 0.90.0 → 0.93.1

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 (201) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +79 -3
  4. data/lib/rubocop.rb +20 -5
  5. data/lib/rubocop/cached_data.rb +2 -1
  6. data/lib/rubocop/cli/command/execute_runner.rb +8 -0
  7. data/lib/rubocop/comment_config.rb +9 -5
  8. data/lib/rubocop/config_loader.rb +3 -3
  9. data/lib/rubocop/config_regeneration.rb +33 -0
  10. data/lib/rubocop/config_store.rb +3 -3
  11. data/lib/rubocop/cop/bundler/duplicated_gem.rb +5 -1
  12. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  13. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -0
  14. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  15. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +10 -10
  16. data/lib/rubocop/cop/generator.rb +1 -1
  17. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  18. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  19. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -0
  20. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  21. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -0
  22. data/lib/rubocop/cop/layout/array_alignment.rb +1 -0
  23. data/lib/rubocop/cop/layout/begin_end_alignment.rb +77 -0
  24. data/lib/rubocop/cop/layout/case_indentation.rb +4 -7
  25. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  26. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  27. data/lib/rubocop/cop/layout/dot_position.rb +6 -9
  28. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +2 -2
  29. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +4 -12
  30. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +13 -8
  31. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -2
  32. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -2
  33. data/lib/rubocop/cop/layout/end_alignment.rb +5 -10
  34. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +26 -4
  35. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +4 -13
  36. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -7
  37. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +0 -4
  38. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +6 -21
  39. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +3 -8
  40. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +2 -2
  41. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +2 -0
  42. data/lib/rubocop/cop/lint/ambiguous_operator.rb +2 -0
  43. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +18 -1
  44. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -2
  45. data/lib/rubocop/cop/lint/boolean_symbol.rb +3 -0
  46. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +74 -0
  47. data/lib/rubocop/cop/lint/debugger.rb +2 -3
  48. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -3
  49. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -4
  50. data/lib/rubocop/cop/lint/duplicate_require.rb +7 -2
  51. data/lib/rubocop/cop/lint/duplicate_rescue_exception.rb +2 -4
  52. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  53. data/lib/rubocop/cop/lint/empty_file.rb +1 -4
  54. data/lib/rubocop/cop/lint/erb_new_arguments.rb +2 -0
  55. data/lib/rubocop/cop/lint/float_comparison.rb +2 -2
  56. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +2 -2
  57. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +37 -0
  58. data/lib/rubocop/cop/lint/identity_comparison.rb +51 -0
  59. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +2 -5
  60. data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
  61. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
  62. data/lib/rubocop/cop/lint/multiple_comparison.rb +3 -1
  63. data/lib/rubocop/cop/lint/number_conversion.rb +1 -0
  64. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -2
  65. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  66. data/lib/rubocop/cop/lint/raise_exception.rb +1 -0
  67. data/lib/rubocop/cop/lint/rand_one.rb +2 -1
  68. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +22 -12
  69. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -4
  70. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  71. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +78 -0
  72. data/lib/rubocop/cop/lint/rescue_type.rb +0 -1
  73. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +3 -1
  74. data/lib/rubocop/cop/lint/shadowed_exception.rb +6 -6
  75. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  76. data/lib/rubocop/cop/lint/to_json.rb +16 -5
  77. data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -6
  78. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +3 -1
  79. data/lib/rubocop/cop/lint/uri_regexp.rb +2 -1
  80. data/lib/rubocop/cop/lint/useless_access_modifier.rb +3 -9
  81. data/lib/rubocop/cop/lint/useless_method_definition.rb +20 -27
  82. data/lib/rubocop/cop/lint/useless_times.rb +106 -0
  83. data/lib/rubocop/cop/metrics/block_length.rb +3 -1
  84. data/lib/rubocop/cop/metrics/class_length.rb +14 -6
  85. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +25 -16
  86. data/lib/rubocop/cop/mixin/comments_help.rb +3 -9
  87. data/lib/rubocop/cop/mixin/configurable_naming.rb +2 -2
  88. data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
  89. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +9 -0
  90. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -2
  91. data/lib/rubocop/cop/mixin/rescue_node.rb +1 -0
  92. data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -3
  93. data/lib/rubocop/cop/mixin/visibility_help.rb +4 -16
  94. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  95. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  96. data/lib/rubocop/cop/offense.rb +15 -2
  97. data/lib/rubocop/cop/security/eval.rb +1 -0
  98. data/lib/rubocop/cop/security/json_load.rb +1 -0
  99. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  100. data/lib/rubocop/cop/security/open.rb +1 -0
  101. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  102. data/lib/rubocop/cop/style/access_modifier_declarations.rb +7 -11
  103. data/lib/rubocop/cop/style/accessor_grouping.rb +3 -0
  104. data/lib/rubocop/cop/style/alias.rb +2 -0
  105. data/lib/rubocop/cop/style/array_coercion.rb +4 -0
  106. data/lib/rubocop/cop/style/array_join.rb +1 -0
  107. data/lib/rubocop/cop/style/attr.rb +1 -0
  108. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +2 -0
  109. data/lib/rubocop/cop/style/case_equality.rb +3 -0
  110. data/lib/rubocop/cop/style/case_like_if.rb +20 -4
  111. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -0
  112. data/lib/rubocop/cop/style/class_check.rb +6 -9
  113. data/lib/rubocop/cop/style/class_equality_comparison.rb +64 -0
  114. data/lib/rubocop/cop/style/class_methods_definitions.rb +42 -16
  115. data/lib/rubocop/cop/style/class_vars.rb +1 -2
  116. data/lib/rubocop/cop/style/combinable_loops.rb +13 -11
  117. data/lib/rubocop/cop/style/comment_annotation.rb +6 -0
  118. data/lib/rubocop/cop/style/commented_keyword.rb +7 -8
  119. data/lib/rubocop/cop/style/conditional_assignment.rb +49 -60
  120. data/lib/rubocop/cop/style/date_time.rb +12 -1
  121. data/lib/rubocop/cop/style/dir.rb +1 -0
  122. data/lib/rubocop/cop/style/double_negation.rb +1 -0
  123. data/lib/rubocop/cop/style/empty_literal.rb +3 -1
  124. data/lib/rubocop/cop/style/eval_with_location.rb +1 -3
  125. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  126. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  127. data/lib/rubocop/cop/style/explicit_block_argument.rb +7 -3
  128. data/lib/rubocop/cop/style/float_division.rb +2 -0
  129. data/lib/rubocop/cop/style/for.rb +0 -4
  130. data/lib/rubocop/cop/style/format_string.rb +1 -4
  131. data/lib/rubocop/cop/style/format_string_token.rb +1 -1
  132. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +24 -5
  133. data/lib/rubocop/cop/style/hash_transform_keys.rb +5 -11
  134. data/lib/rubocop/cop/style/hash_transform_values.rb +5 -11
  135. data/lib/rubocop/cop/style/if_unless_modifier.rb +0 -4
  136. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  137. data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -6
  138. data/lib/rubocop/cop/style/lambda_call.rb +3 -1
  139. data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
  140. data/lib/rubocop/cop/style/mixin_usage.rb +8 -27
  141. data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -2
  142. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +14 -1
  143. data/lib/rubocop/cop/style/multiline_when_then.rb +1 -0
  144. data/lib/rubocop/cop/style/nested_ternary_operator.rb +2 -0
  145. data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
  146. data/lib/rubocop/cop/style/non_nil_check.rb +2 -0
  147. data/lib/rubocop/cop/style/not.rb +1 -0
  148. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -3
  149. data/lib/rubocop/cop/style/one_line_conditional.rb +3 -1
  150. data/lib/rubocop/cop/style/optional_boolean_parameter.rb +12 -1
  151. data/lib/rubocop/cop/style/preferred_hash_methods.rb +2 -0
  152. data/lib/rubocop/cop/style/raise_args.rb +2 -3
  153. data/lib/rubocop/cop/style/random_with_offset.rb +4 -3
  154. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -9
  155. data/lib/rubocop/cop/style/redundant_begin.rb +36 -8
  156. data/lib/rubocop/cop/style/redundant_condition.rb +5 -1
  157. data/lib/rubocop/cop/style/redundant_conditional.rb +4 -5
  158. data/lib/rubocop/cop/style/redundant_exception.rb +1 -3
  159. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  160. data/lib/rubocop/cop/style/redundant_freeze.rb +2 -1
  161. data/lib/rubocop/cop/style/redundant_interpolation.rb +6 -1
  162. data/lib/rubocop/cop/style/redundant_parentheses.rb +14 -6
  163. data/lib/rubocop/cop/style/redundant_percent_q.rb +9 -11
  164. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +39 -24
  165. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -15
  166. data/lib/rubocop/cop/style/redundant_return.rb +17 -17
  167. data/lib/rubocop/cop/style/redundant_self.rb +7 -9
  168. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -2
  169. data/lib/rubocop/cop/style/redundant_sort.rb +12 -29
  170. data/lib/rubocop/cop/style/redundant_sort_by.rb +5 -9
  171. data/lib/rubocop/cop/style/rescue_standard_error.rb +20 -16
  172. data/lib/rubocop/cop/style/safe_navigation.rb +5 -0
  173. data/lib/rubocop/cop/style/sample.rb +2 -1
  174. data/lib/rubocop/cop/style/send.rb +2 -3
  175. data/lib/rubocop/cop/style/signal_exception.rb +2 -0
  176. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  177. data/lib/rubocop/cop/style/slicing_with_range.rb +2 -1
  178. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  179. data/lib/rubocop/cop/style/string_concatenation.rb +17 -3
  180. data/lib/rubocop/cop/style/strip.rb +1 -0
  181. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
  182. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +4 -3
  183. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  184. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -5
  185. data/lib/rubocop/cop/util.rb +0 -1
  186. data/lib/rubocop/cop/variable_force/branch.rb +0 -4
  187. data/lib/rubocop/core_ext/string.rb +1 -1
  188. data/lib/rubocop/directive_comment.rb +32 -0
  189. data/lib/rubocop/ext/regexp_node.rb +23 -7
  190. data/lib/rubocop/formatter/disabled_config_formatter.rb +12 -5
  191. data/lib/rubocop/options.rb +37 -17
  192. data/lib/rubocop/result_cache.rb +38 -15
  193. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  194. data/lib/rubocop/rspec/expect_offense.rb +5 -5
  195. data/lib/rubocop/runner.rb +37 -18
  196. data/lib/rubocop/target_finder.rb +27 -26
  197. data/lib/rubocop/target_ruby.rb +1 -1
  198. data/lib/rubocop/version.rb +6 -1
  199. metadata +19 -18
  200. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +0 -43
  201. data/lib/rubocop/cop/tokens_util.rb +0 -84
@@ -23,6 +23,7 @@ module RuboCop
23
23
  # end
24
24
  class RescueEnsureAlignment < Base
25
25
  include RangeHelp
26
+ include EndKeywordAlignment
26
27
  extend AutoCorrector
27
28
 
28
29
  MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
@@ -59,7 +60,7 @@ module RuboCop
59
60
  alignment_node = alignment_node(node)
60
61
  return if alignment_node.nil?
61
62
 
62
- alignment_loc = alignment_node.loc.expression
63
+ alignment_loc = alignment_location(alignment_node)
63
64
  kw_loc = node.loc.keyword
64
65
 
65
66
  return if alignment_loc.column == kw_loc.column || alignment_loc.line == kw_loc.line
@@ -67,16 +68,16 @@ module RuboCop
67
68
  add_offense(
68
69
  kw_loc, message: format_message(alignment_node, alignment_loc, kw_loc)
69
70
  ) do |corrector|
70
- autocorrect(corrector, node, alignment_node)
71
+ autocorrect(corrector, node, alignment_loc)
71
72
  end
72
73
  end
73
74
 
74
- def autocorrect(corrector, node, alignment_node)
75
+ def autocorrect(corrector, node, alignment_location)
75
76
  whitespace = whitespace_range(node)
76
77
  # Some inline node is sitting before current node.
77
78
  return nil unless whitespace.source.strip.empty?
78
79
 
79
- new_column = alignment_node.loc.column
80
+ new_column = alignment_location.column
80
81
 
81
82
  corrector.replace(whitespace, ' ' * new_column)
82
83
  end
@@ -180,6 +181,27 @@ module RuboCop
180
181
 
181
182
  false
182
183
  end
184
+
185
+ def alignment_location(alignment_node)
186
+ if begin_end_alignment_style == 'start_of_line'
187
+ start_line_range(alignment_node)
188
+ else
189
+ alignment_node.loc.expression
190
+ end
191
+ end
192
+
193
+ def begin_end_alignment_style
194
+ # FIXME: Workaround for pending status for `Layout/BeginEndAlignment` cop
195
+ # When RuboCop 1.0 is released, please replace it with the following condition.
196
+ #
197
+ # config.for_cop('Layout/BeginEndAlignment')['Enabled'] &&
198
+ # config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith']
199
+ if config.for_all_cops['NewCops'] == 'enable' ||
200
+ config.for_cop('Layout/BeginEndAlignment')['Enabled'] &&
201
+ config.for_cop('Layout/BeginEndAlignment')['Enabled'] != 'pending'
202
+ config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith']
203
+ end
204
+ end
183
205
  end
184
206
  end
185
207
  end
@@ -36,8 +36,8 @@ module RuboCop
36
36
  MSG = 'Surrounding space %<type>s in default value assignment.'
37
37
 
38
38
  def on_optarg(node)
39
- index = index_of_first_token(node)
40
- arg, equals, value = processed_source.tokens[index, 3]
39
+ tokens = processed_source.tokens_within(node)
40
+ arg, equals, value = tokens.take(3)
41
41
  check_optarg(arg, equals, value)
42
42
  end
43
43
 
@@ -51,22 +51,13 @@ module RuboCop
51
51
  style == :no_space && no_surrounding_space
52
52
  correct_style_detected
53
53
  else
54
- incorrect_style_detected(arg, value, space_on_both_sides,
55
- no_surrounding_space)
54
+ incorrect_style_detected(arg, value)
56
55
  end
57
56
  end
58
57
 
59
- def incorrect_style_detected(arg, value, space_on_both_sides,
60
- no_surrounding_space)
58
+ def incorrect_style_detected(arg, value)
61
59
  range = range_between(arg.end_pos, value.begin_pos)
62
60
 
63
- if style == :space && no_surrounding_space ||
64
- style == :no_space && space_on_both_sides
65
- return unless opposite_style_detected
66
- else
67
- return unless unrecognized_style_detected
68
- end
69
-
70
61
  add_offense(range) do |corrector|
71
62
  autocorrect(corrector, range)
72
63
  end
@@ -108,11 +108,11 @@ module RuboCop
108
108
  end
109
109
 
110
110
  def left_array_bracket(node)
111
- tokens(node).find(&:left_array_bracket?)
111
+ processed_source.tokens_within(node).find(&:left_array_bracket?)
112
112
  end
113
113
 
114
114
  def right_array_bracket(node)
115
- tokens(node).reverse.find(&:right_bracket?)
115
+ processed_source.tokens_within(node).reverse.find(&:right_bracket?)
116
116
  end
117
117
 
118
118
  def empty_config
@@ -120,7 +120,7 @@ module RuboCop
120
120
  end
121
121
 
122
122
  def next_to_newline?(node, token)
123
- tokens(node)[index_for(node, token) + 1].line != token.line
123
+ processed_source.tokens_within(node)[index_for(node, token) + 1].line != token.line
124
124
  end
125
125
 
126
126
  def end_has_own_line?(token)
@@ -131,7 +131,7 @@ module RuboCop
131
131
  end
132
132
 
133
133
  def index_for(node, token)
134
- tokens(node).index(token)
134
+ processed_source.tokens_within(node).index(token)
135
135
  end
136
136
 
137
137
  def line_and_column_for(token)
@@ -153,7 +153,7 @@ module RuboCop
153
153
  end
154
154
 
155
155
  def next_to_comment?(node, token)
156
- tokens(node)[index_for(node, token) + 1].comment?
156
+ processed_source.tokens_within(node)[index_for(node, token) + 1].comment?
157
157
  end
158
158
 
159
159
  def compact_offenses(node, left, right, start_ok, end_ok)
@@ -184,9 +184,9 @@ module RuboCop
184
184
  def multi_dimensional_array?(node, token, side: :right)
185
185
  i = index_for(node, token)
186
186
  if side == :right
187
- tokens(node)[i - 1].right_bracket?
187
+ processed_source.tokens_within(node)[i - 1].right_bracket?
188
188
  else
189
- tokens(node)[i + 1].left_array_bracket?
189
+ processed_source.tokens_within(node)[i + 1].left_array_bracket?
190
190
  end
191
191
  end
192
192
 
@@ -206,8 +206,6 @@ module RuboCop
206
206
 
207
207
  def no_space(begin_pos, end_pos, msg)
208
208
  if style == :space
209
- return unless opposite_style_detected
210
-
211
209
  offense(begin_pos, end_pos, msg)
212
210
  else
213
211
  correct_style_detected
@@ -216,8 +214,6 @@ module RuboCop
216
214
 
217
215
  def space(begin_pos, end_pos, msg)
218
216
  if style == :no_space
219
- return unless opposite_style_detected
220
-
221
217
  offense(begin_pos, end_pos, msg)
222
218
  else
223
219
  correct_style_detected
@@ -72,29 +72,15 @@ module RuboCop
72
72
  MSG = 'Space inside %<problem>s.'
73
73
 
74
74
  def on_hash(node)
75
- tokens = processed_source.tokens
75
+ tokens = processed_source.tokens_within(node)
76
+ return unless tokens.first.left_brace? && tokens.last.right_curly_brace?
76
77
 
77
- hash_literal_with_braces(node) do |begin_index, end_index|
78
- check(tokens[begin_index], tokens[begin_index + 1])
79
- return if begin_index == end_index - 1
80
-
81
- check(tokens[end_index - 1], tokens[end_index])
82
- end
78
+ check(tokens[0], tokens[1])
79
+ check(tokens[-2], tokens[-1]) if tokens.size > 2
83
80
  end
84
81
 
85
82
  private
86
83
 
87
- def hash_literal_with_braces(node)
88
- tokens = processed_source.tokens
89
- begin_index = index_of_first_token(node)
90
- return unless tokens[begin_index].left_brace?
91
-
92
- end_index = index_of_last_token(node)
93
- return unless tokens[end_index].right_curly_brace?
94
-
95
- yield begin_index, end_index
96
- end
97
-
98
84
  def check(token1, token2)
99
85
  # No offense if line break inside.
100
86
  return if token1.line < token2.line
@@ -126,12 +112,11 @@ module RuboCop
126
112
 
127
113
  def incorrect_style_detected(token1, token2,
128
114
  expect_space, is_empty_braces)
115
+ return unless ambiguous_or_unexpected_style_detected(style, token1.text == token2.text)
116
+
129
117
  brace = (token1.text == '{' ? token1 : token2).pos
130
118
  range = expect_space ? brace : space_range(brace)
131
119
 
132
- style = expect_space ? :no_space : :space
133
- return unless ambiguous_or_unexpected_style_detected(style, token1.text == token2.text)
134
-
135
120
  add_offense(range, message: message(brace, is_empty_braces, expect_space)) do |corrector|
136
121
  autocorrect(corrector, range)
137
122
  end
@@ -61,13 +61,12 @@ module RuboCop
61
61
  MSG = '%<command>s space inside reference brackets.'
62
62
  EMPTY_MSG = '%<command>s space inside empty reference brackets.'
63
63
 
64
- BRACKET_METHODS = %i[[] []=].freeze
64
+ RESTRICT_ON_SEND = %i[[] []=].freeze
65
65
 
66
66
  def on_send(node)
67
67
  return if node.multiline?
68
- return unless bracket_method?(node)
69
68
 
70
- tokens = tokens(node)
69
+ tokens = processed_source.tokens_within(node)
71
70
  left_token = left_ref_bracket(node, tokens)
72
71
  return unless left_token
73
72
 
@@ -99,15 +98,11 @@ module RuboCop
99
98
  end
100
99
 
101
100
  def reference_brackets(node)
102
- tokens = tokens(node)
101
+ tokens = processed_source.tokens_within(node)
103
102
  left = left_ref_bracket(node, tokens)
104
103
  [left, closing_bracket(tokens, left)]
105
104
  end
106
105
 
107
- def bracket_method?(node)
108
- BRACKET_METHODS.include?(node.method_name)
109
- end
110
-
111
106
  def left_ref_bracket(node, tokens)
112
107
  current_token = tokens.reverse.find(&:left_ref_bracket?)
113
108
  previous_token = previous_token(current_token)
@@ -54,8 +54,8 @@ module RuboCop
54
54
  end
55
55
 
56
56
  def delimiters(begin_node)
57
- left = processed_source.tokens[index_of_first_token(begin_node)]
58
- right = processed_source.tokens[index_of_last_token(begin_node)]
57
+ left = processed_source.first_token_of(begin_node)
58
+ right = processed_source.last_token_of(begin_node)
59
59
  [left, right]
60
60
  end
61
61
  end
@@ -15,6 +15,8 @@ module RuboCop
15
15
  #
16
16
  # # good
17
17
  # # With parentheses, there's no ambiguity.
18
+ # some_method(a { |val| puts val })
19
+ # # or (different meaning)
18
20
  # some_method(a) { |val| puts val }
19
21
  #
20
22
  # # good
@@ -43,6 +43,8 @@ module RuboCop
43
43
  next unless diagnostic.reason == :ambiguous_prefix
44
44
 
45
45
  offense_node = find_offense_node_by(diagnostic)
46
+ next unless offense_node
47
+
46
48
  message = message(diagnostic)
47
49
 
48
50
  add_offense(
@@ -47,7 +47,24 @@ module RuboCop
47
47
  regexp_node.source_range.begin_pos == diagnostic.location.begin_pos
48
48
  end
49
49
 
50
- node.parent
50
+ find_offense_node(node.parent, node)
51
+ end
52
+
53
+ def find_offense_node(node, regexp_receiver)
54
+ return node unless node.parent
55
+
56
+ if node.parent.send_type? || method_chain_to_regexp_receiver?(node, regexp_receiver)
57
+ node = find_offense_node(node.parent, regexp_receiver)
58
+ end
59
+
60
+ node
61
+ end
62
+
63
+ def method_chain_to_regexp_receiver?(node, regexp_receiver)
64
+ return false unless (parent = node.parent)
65
+ return false unless (parent_receiver = parent.receiver)
66
+
67
+ parent.parent && parent_receiver.receiver == regexp_receiver
51
68
  end
52
69
  end
53
70
  end
@@ -19,6 +19,7 @@ module RuboCop
19
19
 
20
20
  MSG = '`%<double_colon>sBigDecimal.new()` is deprecated. ' \
21
21
  'Use `%<double_colon>sBigDecimal()` instead.'
22
+ RESTRICT_ON_SEND = %i[new].freeze
22
23
 
23
24
  def_node_matcher :big_decimal_new, <<~PATTERN
24
25
  (send
@@ -26,8 +27,6 @@ module RuboCop
26
27
  PATTERN
27
28
 
28
29
  def on_send(node)
29
- return unless node.method?(:new)
30
-
31
30
  big_decimal_new(node) do |captured_value|
32
31
  double_colon = captured_value ? '::' : ''
33
32
  message = format(MSG, double_colon: double_colon)
@@ -32,6 +32,9 @@ module RuboCop
32
32
  def on_sym(node)
33
33
  return unless boolean_symbol?(node)
34
34
 
35
+ parent = node.parent
36
+ return if parent&.array_type? && parent&.percent_literal?(:symbol)
37
+
35
38
  add_offense(node, message: format(MSG, boolean: node.value)) do |corrector|
36
39
  autocorrect(corrector, node)
37
40
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Do not define constants within a block, since the block's scope does not
7
+ # isolate or namespace the constant in any way.
8
+ #
9
+ # If you are trying to define that constant once, define it outside of
10
+ # the block instead, or use a variable or method if defining the constant
11
+ # in the outer scope would be problematic.
12
+ #
13
+ # For meta-programming, use `const_set`.
14
+ #
15
+ # @example
16
+ # # bad
17
+ # task :lint do
18
+ # FILES_TO_LINT = Dir['lib/*.rb']
19
+ # end
20
+ #
21
+ # # bad
22
+ # describe 'making a request' do
23
+ # class TestRequest; end
24
+ # end
25
+ #
26
+ # # bad
27
+ # module M
28
+ # extend ActiveSupport::Concern
29
+ # included do
30
+ # LIST = []
31
+ # end
32
+ # end
33
+ #
34
+ # # good
35
+ # task :lint do
36
+ # files_to_lint = Dir['lib/*.rb']
37
+ # end
38
+ #
39
+ # # good
40
+ # describe 'making a request' do
41
+ # let(:test_request) { Class.new }
42
+ # # see also `stub_const` for RSpec
43
+ # end
44
+ #
45
+ # # good
46
+ # module M
47
+ # extend ActiveSupport::Concern
48
+ # included do
49
+ # const_set(:LIST, [])
50
+ # end
51
+ # end
52
+ class ConstantDefinitionInBlock < Base
53
+ MSG = 'Do not define constants this way within a block.'
54
+
55
+ def_node_matcher :constant_assigned_in_block?, <<~PATTERN
56
+ ({^block_type? [^begin_type? ^^block_type?]} nil? ...)
57
+ PATTERN
58
+
59
+ def_node_matcher :module_defined_in_block?, <<~PATTERN
60
+ ({^block_type? [^begin_type? ^^block_type?]} ...)
61
+ PATTERN
62
+
63
+ def on_casgn(node)
64
+ add_offense(node) if constant_assigned_in_block?(node)
65
+ end
66
+
67
+ def on_class(node)
68
+ add_offense(node) if module_defined_in_block?(node)
69
+ end
70
+ alias on_module on_class
71
+ end
72
+ end
73
+ end
74
+ end
@@ -35,10 +35,10 @@ module RuboCop
35
35
  class Debugger < Base
36
36
  MSG = 'Remove debugger entry point `%<source>s`.'
37
37
 
38
- DEBUGGER_METHODS = %i[
38
+ RESTRICT_ON_SEND = %i[
39
39
  debugger byebug remote_byebug pry remote_pry pry_remote console rescue
40
40
  save_and_open_page save_and_open_screenshot save_screenshot irb
41
- ].to_set.freeze
41
+ ].freeze
42
42
 
43
43
  def_node_matcher :kernel?, <<~PATTERN
44
44
  {
@@ -62,7 +62,6 @@ module RuboCop
62
62
  PATTERN
63
63
 
64
64
  def on_send(node)
65
- return unless DEBUGGER_METHODS.include?(node.method_name)
66
65
  return unless debugger_call?(node) || binding_irb?(node)
67
66
 
68
67
  add_offense(node)
@@ -62,11 +62,9 @@ module RuboCop
62
62
  replacement: :block_given?)
63
63
  ].freeze
64
64
 
65
- DEPRECATED_METHODS = DEPRECATED_METHODS_OBJECT.map(&:deprecated_method).freeze
65
+ RESTRICT_ON_SEND = DEPRECATED_METHODS_OBJECT.map(&:deprecated_method).freeze
66
66
 
67
67
  def on_send(node)
68
- return unless DEPRECATED_METHODS.include?(node.method_name)
69
-
70
68
  check(node) do |data|
71
69
  message = format(MSG, current: deprecated_method(data),
72
70
  prefer: replacement_method(data))