rubocop 1.9.1 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (253) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -13
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +67 -17
  5. data/config/obsoletion.yml +4 -0
  6. data/lib/rubocop.rb +4 -0
  7. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  8. data/lib/rubocop/cli/command/suggest_extensions.rb +3 -2
  9. data/lib/rubocop/comment_config.rb +43 -94
  10. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
  11. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  12. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
  13. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
  14. data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
  15. data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
  16. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
  17. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  18. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  19. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
  20. data/lib/rubocop/cop/generator.rb +2 -2
  21. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  22. data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -0
  23. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  24. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
  25. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
  26. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  27. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
  28. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +1 -0
  29. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +1 -0
  30. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  31. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
  32. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
  33. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  34. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +11 -8
  35. data/lib/rubocop/cop/layout/argument_alignment.rb +6 -5
  36. data/lib/rubocop/cop/layout/array_alignment.rb +7 -6
  37. data/lib/rubocop/cop/layout/assignment_indentation.rb +6 -3
  38. data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
  39. data/lib/rubocop/cop/layout/block_end_newline.rb +4 -8
  40. data/lib/rubocop/cop/layout/class_structure.rb +1 -0
  41. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +14 -15
  42. data/lib/rubocop/cop/layout/comment_indentation.rb +16 -16
  43. data/lib/rubocop/cop/layout/else_alignment.rb +9 -6
  44. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  45. data/lib/rubocop/cop/layout/first_argument_indentation.rb +12 -6
  46. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +9 -6
  47. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +22 -15
  48. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -5
  49. data/lib/rubocop/cop/layout/indentation_consistency.rb +9 -6
  50. data/lib/rubocop/cop/layout/indentation_style.rb +27 -30
  51. data/lib/rubocop/cop/layout/indentation_width.rb +20 -9
  52. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
  53. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +6 -5
  54. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -5
  55. data/lib/rubocop/cop/layout/parameter_alignment.rb +6 -5
  56. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  57. data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -1
  58. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  59. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
  60. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
  61. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
  62. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
  63. data/lib/rubocop/cop/lint/debugger.rb +60 -14
  64. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
  65. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
  66. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
  67. data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
  68. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  69. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  70. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
  71. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  72. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
  73. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  74. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
  75. data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
  76. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
  77. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
  78. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
  79. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
  80. data/lib/rubocop/cop/lint/number_conversion.rb +9 -0
  81. data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
  82. data/lib/rubocop/cop/lint/rand_one.rb +1 -0
  83. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -2
  84. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  85. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
  86. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
  87. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
  88. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
  89. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
  90. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  91. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
  92. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
  93. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  94. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  95. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  96. data/lib/rubocop/cop/lint/suppressed_exception.rb +44 -1
  97. data/lib/rubocop/cop/lint/symbol_conversion.rb +89 -2
  98. data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
  99. data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
  100. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
  101. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  102. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
  103. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
  104. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
  105. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
  106. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
  107. data/lib/rubocop/cop/lint/useless_times.rb +3 -0
  108. data/lib/rubocop/cop/metrics/module_length.rb +1 -0
  109. data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -0
  110. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
  111. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
  112. data/lib/rubocop/cop/mixin/alignment.rb +10 -3
  113. data/lib/rubocop/cop/mixin/comments_help.rb +5 -1
  114. data/lib/rubocop/cop/mixin/def_node.rb +1 -0
  115. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  116. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
  117. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  118. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
  119. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
  120. data/lib/rubocop/cop/mixin/line_length_help.rb +11 -6
  121. data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
  122. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +3 -1
  123. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -3
  124. data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
  125. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
  126. data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
  127. data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
  128. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -6
  129. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
  130. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  131. data/lib/rubocop/cop/naming/constant_name.rb +2 -0
  132. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +6 -0
  133. data/lib/rubocop/cop/naming/method_name.rb +3 -0
  134. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  135. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -0
  136. data/lib/rubocop/cop/registry.rb +10 -1
  137. data/lib/rubocop/cop/security/eval.rb +1 -0
  138. data/lib/rubocop/cop/security/json_load.rb +1 -0
  139. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  140. data/lib/rubocop/cop/security/open.rb +1 -0
  141. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  142. data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -1
  143. data/lib/rubocop/cop/style/alias.rb +1 -0
  144. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  145. data/lib/rubocop/cop/style/array_coercion.rb +2 -0
  146. data/lib/rubocop/cop/style/array_join.rb +1 -0
  147. data/lib/rubocop/cop/style/attr.rb +1 -0
  148. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +59 -71
  149. data/lib/rubocop/cop/style/bisected_attr_accessor/macro.rb +62 -0
  150. data/lib/rubocop/cop/style/case_equality.rb +2 -1
  151. data/lib/rubocop/cop/style/case_like_if.rb +15 -4
  152. data/lib/rubocop/cop/style/class_equality_comparison.rb +3 -0
  153. data/lib/rubocop/cop/style/collection_compact.rb +2 -0
  154. data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
  155. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  156. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  157. data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
  158. data/lib/rubocop/cop/style/date_time.rb +3 -0
  159. data/lib/rubocop/cop/style/dir.rb +1 -0
  160. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  161. data/lib/rubocop/cop/style/documentation.rb +30 -3
  162. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  163. data/lib/rubocop/cop/style/double_negation.rb +3 -2
  164. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
  165. data/lib/rubocop/cop/style/each_with_object.rb +1 -0
  166. data/lib/rubocop/cop/style/empty_literal.rb +9 -0
  167. data/lib/rubocop/cop/style/endless_method.rb +1 -0
  168. data/lib/rubocop/cop/style/eval_with_location.rb +89 -27
  169. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  170. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
  171. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -1
  172. data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
  173. data/lib/rubocop/cop/style/float_division.rb +4 -0
  174. data/lib/rubocop/cop/style/format_string.rb +2 -0
  175. data/lib/rubocop/cop/style/format_string_token.rb +1 -0
  176. data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
  177. data/lib/rubocop/cop/style/hash_conversion.rb +105 -0
  178. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
  179. data/lib/rubocop/cop/style/hash_except.rb +1 -0
  180. data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
  181. data/lib/rubocop/cop/style/hash_syntax.rb +16 -15
  182. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
  183. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
  184. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -0
  185. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  186. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  187. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +40 -0
  188. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +20 -2
  189. data/lib/rubocop/cop/style/min_max.rb +1 -0
  190. data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
  191. data/lib/rubocop/cop/style/module_function.rb +5 -0
  192. data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
  193. data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
  194. data/lib/rubocop/cop/style/negated_if_else_condition.rb +16 -2
  195. data/lib/rubocop/cop/style/nil_comparison.rb +3 -0
  196. data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
  197. data/lib/rubocop/cop/style/non_nil_check.rb +7 -0
  198. data/lib/rubocop/cop/style/numeric_predicate.rb +3 -0
  199. data/lib/rubocop/cop/style/option_hash.rb +1 -0
  200. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  201. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
  202. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  203. data/lib/rubocop/cop/style/proc.rb +1 -0
  204. data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
  205. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
  206. data/lib/rubocop/cop/style/redundant_begin.rb +33 -4
  207. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
  208. data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
  209. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
  210. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  211. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
  212. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
  213. data/lib/rubocop/cop/style/redundant_return.rb +4 -0
  214. data/lib/rubocop/cop/style/redundant_self.rb +7 -3
  215. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
  216. data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
  217. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
  218. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  219. data/lib/rubocop/cop/style/rescue_modifier.rb +17 -14
  220. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
  221. data/lib/rubocop/cop/style/return_nil.rb +6 -0
  222. data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
  223. data/lib/rubocop/cop/style/sample.rb +1 -0
  224. data/lib/rubocop/cop/style/signal_exception.rb +3 -0
  225. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  226. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
  227. data/lib/rubocop/cop/style/sole_nested_conditional.rb +16 -0
  228. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  229. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  230. data/lib/rubocop/cop/style/string_chars.rb +38 -0
  231. data/lib/rubocop/cop/style/string_concatenation.rb +1 -0
  232. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
  233. data/lib/rubocop/cop/style/strip.rb +1 -0
  234. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
  235. data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
  236. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -0
  237. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +5 -0
  238. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  239. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
  240. data/lib/rubocop/cop/style/unless_logical_operators.rb +105 -0
  241. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  242. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  243. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
  244. data/lib/rubocop/directive_comment.rb +64 -9
  245. data/lib/rubocop/ext/regexp_parser.rb +3 -6
  246. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  247. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  248. data/lib/rubocop/magic_comment.rb +1 -1
  249. data/lib/rubocop/name_similarity.rb +1 -1
  250. data/lib/rubocop/target_finder.rb +1 -0
  251. data/lib/rubocop/target_ruby.rb +21 -13
  252. data/lib/rubocop/version.rb +1 -1
  253. metadata +13 -7
@@ -30,7 +30,7 @@ module RuboCop
30
30
  # if Dir.exist?(local)
31
31
  # gem 'rubocop', path: local
32
32
  # elsif ENV['RUBOCOP_VERSION'] == 'master'
33
- # gem 'rubocop', git: 'https://github.com/rubocop-hq/rubocop.git'
33
+ # gem 'rubocop', git: 'https://github.com/rubocop/rubocop.git'
34
34
  # else
35
35
  # gem 'rubocop', '~> 0.90.0'
36
36
  # end
@@ -57,6 +57,7 @@ module RuboCop
57
57
 
58
58
  private
59
59
 
60
+ # @!method gem_declarations(node)
60
61
  def_node_search :gem_declarations, '(send nil? :gem str ...)'
61
62
 
62
63
  def duplicated_gem_nodes
@@ -66,6 +66,7 @@ module RuboCop
66
66
  VERSION_SPECIFIERS_OPTION = 'version_specifiers'
67
67
  RESTRICT_ON_SEND = %i[gem].freeze
68
68
 
69
+ # @!method gem_declaration?(node)
69
70
  def_node_matcher :gem_declaration?, '(send nil? :gem str ...)'
70
71
 
71
72
  def on_send(node)
@@ -36,6 +36,7 @@ module RuboCop
36
36
 
37
37
  RESTRICT_ON_SEND = %i[source].freeze
38
38
 
39
+ # @!method insecure_protocol_source?(node)
39
40
  def_node_matcher :insecure_protocol_source?, <<~PATTERN
40
41
  (send nil? :source
41
42
  $(sym ${:gemcutter :rubygems :rubyforge}))
@@ -64,6 +64,7 @@ module RuboCop
64
64
  declarations.to_a[node_index - 1]
65
65
  end
66
66
 
67
+ # @!method gem_declarations(node)
67
68
  def_node_search :gem_declarations, <<~PATTERN
68
69
  (:send nil? :gem str ...)
69
70
  PATTERN
@@ -12,7 +12,7 @@ module RuboCop
12
12
  class << self
13
13
  attr_reader :processed_source
14
14
 
15
- def correct(processed_source, node, column_delta)
15
+ def correct(corrector, processed_source, node, column_delta)
16
16
  return unless node
17
17
 
18
18
  @processed_source = processed_source
@@ -21,11 +21,8 @@ module RuboCop
21
21
 
22
22
  taboo_ranges = inside_string_ranges(node)
23
23
 
24
- lambda do |corrector|
25
- each_line(expr) do |line_begin_pos|
26
- autocorrect_line(corrector, line_begin_pos, expr, column_delta,
27
- taboo_ranges)
28
- end
24
+ each_line(expr) do |line_begin_pos|
25
+ autocorrect_line(corrector, line_begin_pos, expr, column_delta, taboo_ranges)
29
26
  end
30
27
  end
31
28
 
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Gemspec
6
+ # This cop checks that `date =` is not used in gemspec file.
7
+ # It is set automatically when the gem is packaged.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # Gem::Specification.new do |spec|
13
+ # s.name = 'your_cool_gem_name'
14
+ # spec.date = Time.now.strftime('%Y-%m-%d')
15
+ # end
16
+ #
17
+ # # good
18
+ # Gem::Specification.new do |spec|
19
+ # s.name = 'your_cool_gem_name'
20
+ # end
21
+ #
22
+ class DateAssignment < Base
23
+ include RangeHelp
24
+ extend AutoCorrector
25
+
26
+ MSG = 'Do not use `date =` in gemspec, it is set automatically when the gem is packaged.'
27
+
28
+ # @!method gem_specification(node)
29
+ def_node_matcher :gem_specification, <<~PATTERN
30
+ (block
31
+ (send
32
+ (const
33
+ (const {cbase nil?} :Gem) :Specification) :new)
34
+ ...)
35
+ PATTERN
36
+
37
+ def on_block(block_node)
38
+ return unless gem_specification(block_node)
39
+
40
+ block_parameter = block_node.arguments.first.source
41
+
42
+ date_assignment = block_node.descendants.detect do |node|
43
+ node.send_type? && node.receiver&.source == block_parameter && node.method?(:date=)
44
+ end
45
+
46
+ return unless date_assignment
47
+
48
+ add_offense(date_assignment) do |corrector|
49
+ range = range_by_whole_lines(date_assignment.source_range, include_final_newline: true)
50
+
51
+ corrector.remove(range)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -40,6 +40,7 @@ module RuboCop
40
40
  MSG = '`%<assignment>s` method calls already given on line '\
41
41
  '%<line_of_first_occurrence>d of the gemspec.'
42
42
 
43
+ # @!method gem_specification(node)
43
44
  def_node_search :gem_specification, <<~PATTERN
44
45
  (block
45
46
  (send
@@ -49,6 +50,7 @@ module RuboCop
49
50
  (arg $_)) ...)
50
51
  PATTERN
51
52
 
53
+ # @!method assignment_method_declarations(node)
52
54
  def_node_search :assignment_method_declarations, <<~PATTERN
53
55
  (send
54
56
  (lvar #match_block_variable_name?) #assignment_method? ...)
@@ -95,6 +95,7 @@ module RuboCop
95
95
  node.method_name
96
96
  end
97
97
 
98
+ # @!method dependency_declarations(node)
98
99
  def_node_search :dependency_declarations, <<~PATTERN
99
100
  (send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} (str _) ...)
100
101
  PATTERN
@@ -55,10 +55,12 @@ module RuboCop
55
55
  '.rubocop.yml) should be equal.'
56
56
  MISSING_MSG = '`required_ruby_version` should be specified.'
57
57
 
58
+ # @!method required_ruby_version(node)
58
59
  def_node_search :required_ruby_version, <<~PATTERN
59
60
  (send _ :required_ruby_version= $_)
60
61
  PATTERN
61
62
 
63
+ # @!method defined_ruby_version(node)
62
64
  def_node_matcher :defined_ruby_version, <<~PATTERN
63
65
  {$(str _) $(array (str _) (str _))
64
66
  (send (const (const nil? :Gem) :Requirement) :new $(str _))}
@@ -28,8 +28,10 @@ module RuboCop
28
28
  class RubyVersionGlobalsUsage < Base
29
29
  MSG = 'Do not use `RUBY_VERSION` in gemspec file.'
30
30
 
31
+ # @!method ruby_version?(node)
31
32
  def_node_matcher :ruby_version?, '(const {cbase nil?} :RUBY_VERSION)'
32
33
 
34
+ # @!method gem_specification?(node)
33
35
  def_node_search :gem_specification?, <<~PATTERN
34
36
  (block
35
37
  (send
@@ -9,7 +9,7 @@ module RuboCop
9
9
  # @api private
10
10
  class Generator
11
11
  # NOTE: RDoc 5.1.0 or lower has the following issue.
12
- # https://github.com/rubocop-hq/rubocop/issues/7043
12
+ # https://github.com/rubocop/rubocop/issues/7043
13
13
  #
14
14
  # The following `String#gsub` can be replaced with
15
15
  # squiggly heredoc when RuboCop supports Ruby 2.5 or higher
@@ -58,7 +58,7 @@ module RuboCop
58
58
  # TODO: Implement the cop in here.
59
59
  #
60
60
  # In many cases, you can use a node matcher for matching node pattern.
61
- # See https://github.com/rubocop-hq/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
61
+ # See https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
62
62
  #
63
63
  # For example
64
64
  MSG = 'Use `#good_method` instead of `#bad_method`.'
@@ -4,6 +4,7 @@ require_relative 'internal_affairs/empty_line_between_expect_offense_and_correct
4
4
  require_relative 'internal_affairs/example_description'
5
5
  require_relative 'internal_affairs/method_name_equal'
6
6
  require_relative 'internal_affairs/node_destructuring'
7
+ require_relative 'internal_affairs/node_matcher_directive'
7
8
  require_relative 'internal_affairs/node_type_predicate'
8
9
  require_relative 'internal_affairs/offense_location_keyword'
9
10
  require_relative 'internal_affairs/redundant_described_class_as_subject'
@@ -57,6 +57,7 @@ module RuboCop
57
57
  /\b(does not|doesn't) (auto[- ]?)?correct/
58
58
  ].freeze
59
59
 
60
+ # @!method offense_example?(node)
60
61
  def_node_matcher :offense_example?, <<~PATTERN
61
62
  (block
62
63
  (send _ {:it :specify} $_description)
@@ -20,6 +20,7 @@ module RuboCop
20
20
  '`method_name == %<method_name>s`.'
21
21
  RESTRICT_ON_SEND = %i[==].freeze
22
22
 
23
+ # @!method method_name?(node)
23
24
  def_node_matcher :method_name?, <<~PATTERN
24
25
  (send
25
26
  $(send
@@ -19,10 +19,12 @@ module RuboCop
19
19
  MSG = 'Use the methods provided with the node extensions instead ' \
20
20
  'of manually destructuring nodes.'
21
21
 
22
+ # @!method node_variable?(node)
22
23
  def_node_matcher :node_variable?, <<~PATTERN
23
24
  {(lvar [#node_suffix? _]) (send nil? [#node_suffix? _])}
24
25
  PATTERN
25
26
 
27
+ # @!method node_destructuring?(node)
26
28
  def_node_matcher :node_destructuring?, <<~PATTERN
27
29
  {(masgn (mlhs ...) {(send #node_variable? :children) (array (splat #node_variable?))})}
28
30
  PATTERN
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Checks that node matcher definitions are tagged with a YARD `@!method`
7
+ # directive so that editors are able to find the dynamically defined
8
+ # method.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # def_node_matcher :foo?, <<~PATTERN
13
+ # ...
14
+ # PATTERN
15
+ #
16
+ # # good
17
+ # # @!method foo?(node)
18
+ # def_node_matcher :foo?, <<~PATTERN
19
+ # ...
20
+ # PATTERN
21
+ #
22
+ class NodeMatcherDirective < Base
23
+ extend AutoCorrector
24
+ include RangeHelp
25
+
26
+ MSG = 'Preceed `%<method>s` with a `@!method` YARD directive.'
27
+ MSG_WRONG_NAME = '`@!method` YARD directive has invalid method name, ' \
28
+ 'use `%<expected>s` instead of `%<actual>s`.'
29
+ MSG_TOO_MANY = 'Multiple `@!method` YARD directives found for this matcher.'
30
+
31
+ RESTRICT_ON_SEND = %i[def_node_matcher def_node_search].to_set.freeze
32
+ REGEXP = /^\s*#\s*@!method\s+(?<method_name>[a-z0-9_]+[?!]?)(?:\((?<args>.*)\))?/.freeze
33
+
34
+ # @!method pattern_matcher?(node)
35
+ def_node_matcher :pattern_matcher?, <<~PATTERN
36
+ (send _ %RESTRICT_ON_SEND {str sym} {str dstr})
37
+ PATTERN
38
+
39
+ def on_send(node)
40
+ return if node.arguments.none?
41
+ return unless valid_method_name?(node)
42
+
43
+ actual_name = node.arguments.first.value
44
+ directives = method_directives(node)
45
+ return too_many_directives(node) if directives.size > 1
46
+
47
+ directive = directives.first
48
+ return if directive_correct?(directive, actual_name)
49
+
50
+ register_offense(node, directive, actual_name)
51
+ end
52
+
53
+ private
54
+
55
+ def valid_method_name?(node)
56
+ node.arguments.first.str_type? || node.arguments.first.sym_type?
57
+ end
58
+
59
+ def method_directives(node)
60
+ comments = processed_source.ast_with_comments[node]
61
+
62
+ comments.map do |comment|
63
+ match = comment.text.match(REGEXP)
64
+ next unless match
65
+
66
+ { node: comment, method_name: match[:method_name], args: match[:args] }
67
+ end.compact
68
+ end
69
+
70
+ def too_many_directives(node)
71
+ add_offense(node, message: MSG_TOO_MANY)
72
+ end
73
+
74
+ def directive_correct?(directive, actual_name)
75
+ directive && directive[:method_name] == actual_name.to_s
76
+ end
77
+
78
+ def register_offense(node, directive, actual_name)
79
+ message = formatted_message(directive, actual_name, node.method_name)
80
+
81
+ add_offense(node, message: message) do |corrector|
82
+ if directive
83
+ correct_directive(corrector, directive, actual_name)
84
+ else
85
+ insert_directive(corrector, node, actual_name)
86
+ end
87
+ end
88
+ end
89
+
90
+ def formatted_message(directive, actual_name, method_name)
91
+ if directive
92
+ format(MSG_WRONG_NAME, expected: actual_name, actual: directive[:method_name])
93
+ else
94
+ format(MSG, method: method_name)
95
+ end
96
+ end
97
+
98
+ def insert_directive(corrector, node, actual_name)
99
+ # If the pattern matcher uses arguments (`%1`, `%2`, etc.), include them in the directive
100
+ arguments = pattern_arguments(node.arguments[1].source)
101
+
102
+ range = range_with_surrounding_space(
103
+ range: node.loc.expression,
104
+ side: :left,
105
+ newlines: false
106
+ )
107
+ indentation = range.source.match(/^\s*/)[0]
108
+ directive = "#{indentation}# @!method #{actual_name}(#{arguments.join(', ')})\n"
109
+ directive = "\n#{directive}" if add_newline?(node)
110
+
111
+ corrector.insert_before(range, directive)
112
+ end
113
+
114
+ def pattern_arguments(pattern)
115
+ arguments = %w[node]
116
+ max_pattern_var = pattern.scan(/(?<=%)\d+/).map(&:to_i).max
117
+ max_pattern_var&.times { |i| arguments << "arg#{i + 1}" }
118
+ arguments
119
+ end
120
+
121
+ def add_newline?(node)
122
+ # Determine if a blank line should be inserted before the new directive
123
+ # in order to spread out pattern matchers
124
+ return if node.sibling_index&.zero?
125
+ return unless node.parent
126
+
127
+ prev_sibling = node.parent.child_nodes[node.sibling_index - 1]
128
+ return unless prev_sibling && pattern_matcher?(prev_sibling)
129
+
130
+ node.loc.line == last_line(prev_sibling) + 1
131
+ end
132
+
133
+ def last_line(node)
134
+ if node.last_argument.heredoc?
135
+ node.last_argument.loc.heredoc_end.line
136
+ else
137
+ node.loc.last_line
138
+ end
139
+ end
140
+
141
+ def correct_directive(corrector, directive, actual_name)
142
+ correct = "@!method #{actual_name}"
143
+ regexp = /@!method\s+#{Regexp.escape(directive[:method_name])}/
144
+
145
+ replacement = directive[:node].text.gsub(regexp, correct)
146
+ corrector.replace(directive[:node], replacement)
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -19,6 +19,7 @@ module RuboCop
19
19
  MSG = 'Use `#%<type>s_type?` to check node type.'
20
20
  RESTRICT_ON_SEND = %i[==].freeze
21
21
 
22
+ # @!method node_type_check(node)
22
23
  def_node_matcher :node_type_check, <<~PATTERN
23
24
  (send (send $_ :type) :== (sym $_))
24
25
  PATTERN
@@ -34,10 +34,12 @@ module RuboCop
34
34
 
35
35
  private
36
36
 
37
+ # @!method node_type_check(node)
37
38
  def_node_matcher :node_type_check, <<~PATTERN
38
39
  (send nil? :add_offense $_node $hash)
39
40
  PATTERN
40
41
 
42
+ # @!method offending_location_argument(node)
41
43
  def_node_matcher :offending_location_argument, <<~PATTERN
42
44
  (pair (sym :location) $(send (send $_node :loc) $_keyword))
43
45
  PATTERN
@@ -21,6 +21,7 @@ module RuboCop
21
21
 
22
22
  MSG = 'Remove the redundant `subject`%<additional_message>s.'
23
23
 
24
+ # @!method described_class_subject?(node)
24
25
  def_node_matcher :described_class_subject?, <<~PATTERN
25
26
  (block
26
27
  (send nil? :subject
@@ -25,6 +25,7 @@ module RuboCop
25
25
 
26
26
  MSG = 'Remove `let` that is `RuboCop::Config.new` with no arguments%<additional_message>s.'
27
27
 
28
+ # @!method let_rubocop_config_new?(node)
28
29
  def_node_matcher :let_rubocop_config_new?, <<~PATTERN
29
30
  (block
30
31
  (send nil? :let
@@ -23,6 +23,7 @@ module RuboCop
23
23
  MSG = 'Redundant location argument to `#add_offense`.'
24
24
  RESTRICT_ON_SEND = %i[add_offense].freeze
25
25
 
26
+ # @!method redundant_location_argument(node)
26
27
  def_node_matcher :redundant_location_argument, <<~PATTERN
27
28
  (send nil? :add_offense _
28
29
  (hash <$(pair (sym :location) (sym :expression)) ...>)
@@ -26,16 +26,19 @@ module RuboCop
26
26
  MSG = 'Redundant message argument to `#add_offense`.'
27
27
  RESTRICT_ON_SEND = %i[add_offense].freeze
28
28
 
29
+ # @!method node_type_check(node)
29
30
  def_node_matcher :node_type_check, <<~PATTERN
30
31
  (send nil? :add_offense $_node $hash)
31
32
  PATTERN
32
33
 
34
+ # @!method redundant_message_argument(node)
33
35
  def_node_matcher :redundant_message_argument, <<~PATTERN
34
36
  (pair
35
37
  (sym :message)
36
38
  ${(const nil? :MSG) (send nil? :message) (send nil? :message _)})
37
39
  PATTERN
38
40
 
41
+ # @!method message_method_call(node)
39
42
  def_node_matcher :message_method_call, '(send nil? :message $_node)'
40
43
 
41
44
  def on_send(node)