rubocop 1.9.0 → 1.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -14
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +70 -17
  5. data/config/obsoletion.yml +4 -0
  6. data/lib/rubocop.rb +5 -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/config.rb +4 -1
  11. data/lib/rubocop/cop/base.rb +1 -0
  12. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
  13. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  14. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
  15. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
  16. data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
  17. data/lib/rubocop/cop/exclude_limit.rb +26 -0
  18. data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
  19. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
  20. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  21. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  22. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
  23. data/lib/rubocop/cop/generator.rb +2 -2
  24. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  25. data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -0
  26. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  27. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
  28. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
  29. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  30. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
  31. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +1 -0
  32. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +1 -0
  33. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  34. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
  35. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
  36. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  37. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +11 -8
  38. data/lib/rubocop/cop/layout/argument_alignment.rb +6 -5
  39. data/lib/rubocop/cop/layout/array_alignment.rb +7 -6
  40. data/lib/rubocop/cop/layout/assignment_indentation.rb +6 -3
  41. data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
  42. data/lib/rubocop/cop/layout/block_end_newline.rb +4 -8
  43. data/lib/rubocop/cop/layout/class_structure.rb +1 -0
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +14 -15
  45. data/lib/rubocop/cop/layout/comment_indentation.rb +16 -16
  46. data/lib/rubocop/cop/layout/else_alignment.rb +9 -6
  47. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +20 -3
  48. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
  49. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  50. data/lib/rubocop/cop/layout/first_argument_indentation.rb +27 -7
  51. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +9 -6
  52. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +22 -15
  53. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -5
  54. data/lib/rubocop/cop/layout/indentation_consistency.rb +9 -6
  55. data/lib/rubocop/cop/layout/indentation_style.rb +27 -30
  56. data/lib/rubocop/cop/layout/indentation_width.rb +20 -9
  57. data/lib/rubocop/cop/layout/line_length.rb +2 -1
  58. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
  59. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -5
  60. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +10 -5
  61. data/lib/rubocop/cop/layout/parameter_alignment.rb +6 -5
  62. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  63. data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
  64. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  65. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
  66. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
  67. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
  68. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
  69. data/lib/rubocop/cop/lint/debugger.rb +60 -14
  70. data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
  71. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
  72. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
  73. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
  74. data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
  75. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  76. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  77. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
  78. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  79. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
  80. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  81. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
  82. data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
  83. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
  84. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
  85. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
  86. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
  87. data/lib/rubocop/cop/lint/number_conversion.rb +11 -2
  88. data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
  89. data/lib/rubocop/cop/lint/rand_one.rb +1 -0
  90. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -2
  91. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  92. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
  93. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
  94. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
  95. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
  96. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
  97. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  98. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
  99. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
  100. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  101. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  102. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  103. data/lib/rubocop/cop/lint/suppressed_exception.rb +44 -1
  104. data/lib/rubocop/cop/lint/symbol_conversion.rb +91 -3
  105. data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
  106. data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
  107. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
  108. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  109. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
  110. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
  111. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
  112. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
  113. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
  114. data/lib/rubocop/cop/lint/useless_times.rb +3 -0
  115. data/lib/rubocop/cop/message_annotator.rb +4 -1
  116. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  117. data/lib/rubocop/cop/metrics/module_length.rb +1 -0
  118. data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
  119. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
  120. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
  121. data/lib/rubocop/cop/mixin/alignment.rb +10 -3
  122. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  123. data/lib/rubocop/cop/mixin/code_length.rb +3 -1
  124. data/lib/rubocop/cop/mixin/comments_help.rb +5 -1
  125. data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
  126. data/lib/rubocop/cop/mixin/def_node.rb +1 -0
  127. data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
  128. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
  129. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  130. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
  131. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
  132. data/lib/rubocop/cop/mixin/line_length_help.rb +11 -6
  133. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
  134. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +3 -1
  135. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -23
  136. data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
  137. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
  138. data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
  139. data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
  140. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -6
  141. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
  142. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  143. data/lib/rubocop/cop/naming/constant_name.rb +2 -0
  144. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +6 -0
  145. data/lib/rubocop/cop/naming/method_name.rb +3 -0
  146. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  147. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -0
  148. data/lib/rubocop/cop/registry.rb +10 -1
  149. data/lib/rubocop/cop/security/eval.rb +1 -0
  150. data/lib/rubocop/cop/security/json_load.rb +1 -0
  151. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  152. data/lib/rubocop/cop/security/open.rb +1 -0
  153. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  154. data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -2
  155. data/lib/rubocop/cop/style/alias.rb +1 -0
  156. data/lib/rubocop/cop/style/and_or.rb +3 -1
  157. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  158. data/lib/rubocop/cop/style/array_coercion.rb +2 -0
  159. data/lib/rubocop/cop/style/array_join.rb +1 -0
  160. data/lib/rubocop/cop/style/attr.rb +1 -0
  161. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +59 -71
  162. data/lib/rubocop/cop/style/bisected_attr_accessor/macro.rb +62 -0
  163. data/lib/rubocop/cop/style/case_equality.rb +2 -1
  164. data/lib/rubocop/cop/style/case_like_if.rb +15 -4
  165. data/lib/rubocop/cop/style/class_equality_comparison.rb +3 -0
  166. data/lib/rubocop/cop/style/collection_compact.rb +2 -0
  167. data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
  168. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  169. data/lib/rubocop/cop/style/commented_keyword.rb +10 -10
  170. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  171. data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
  172. data/lib/rubocop/cop/style/date_time.rb +3 -0
  173. data/lib/rubocop/cop/style/dir.rb +1 -0
  174. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  175. data/lib/rubocop/cop/style/documentation.rb +30 -3
  176. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  177. data/lib/rubocop/cop/style/double_negation.rb +3 -2
  178. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
  179. data/lib/rubocop/cop/style/each_with_object.rb +1 -0
  180. data/lib/rubocop/cop/style/empty_literal.rb +9 -0
  181. data/lib/rubocop/cop/style/endless_method.rb +1 -0
  182. data/lib/rubocop/cop/style/eval_with_location.rb +90 -28
  183. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  184. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
  185. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -1
  186. data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
  187. data/lib/rubocop/cop/style/float_division.rb +4 -0
  188. data/lib/rubocop/cop/style/format_string.rb +2 -0
  189. data/lib/rubocop/cop/style/format_string_token.rb +1 -0
  190. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +0 -3
  191. data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
  192. data/lib/rubocop/cop/style/hash_conversion.rb +108 -0
  193. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
  194. data/lib/rubocop/cop/style/hash_except.rb +1 -0
  195. data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
  196. data/lib/rubocop/cop/style/hash_syntax.rb +16 -15
  197. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
  198. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
  199. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +37 -11
  200. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  201. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  202. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +46 -2
  203. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +19 -3
  204. data/lib/rubocop/cop/style/min_max.rb +1 -0
  205. data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
  206. data/lib/rubocop/cop/style/module_function.rb +5 -0
  207. data/lib/rubocop/cop/style/multiline_method_signature.rb +10 -3
  208. data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
  209. data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
  210. data/lib/rubocop/cop/style/negated_if_else_condition.rb +16 -2
  211. data/lib/rubocop/cop/style/nil_comparison.rb +6 -1
  212. data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
  213. data/lib/rubocop/cop/style/non_nil_check.rb +7 -0
  214. data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
  215. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
  216. data/lib/rubocop/cop/style/option_hash.rb +1 -0
  217. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  218. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
  219. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  220. data/lib/rubocop/cop/style/proc.rb +1 -0
  221. data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
  222. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
  223. data/lib/rubocop/cop/style/redundant_begin.rb +44 -4
  224. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
  225. data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
  226. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
  227. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  228. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
  229. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
  230. data/lib/rubocop/cop/style/redundant_return.rb +4 -0
  231. data/lib/rubocop/cop/style/redundant_self.rb +7 -3
  232. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
  233. data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
  234. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
  235. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  236. data/lib/rubocop/cop/style/rescue_modifier.rb +17 -14
  237. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
  238. data/lib/rubocop/cop/style/return_nil.rb +6 -0
  239. data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
  240. data/lib/rubocop/cop/style/sample.rb +1 -0
  241. data/lib/rubocop/cop/style/signal_exception.rb +3 -0
  242. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  243. data/lib/rubocop/cop/style/single_line_methods.rb +4 -1
  244. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
  245. data/lib/rubocop/cop/style/sole_nested_conditional.rb +20 -4
  246. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  247. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  248. data/lib/rubocop/cop/style/string_chars.rb +38 -0
  249. data/lib/rubocop/cop/style/string_concatenation.rb +1 -0
  250. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
  251. data/lib/rubocop/cop/style/strip.rb +1 -0
  252. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
  253. data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
  254. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -0
  255. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +5 -0
  256. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  257. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
  258. data/lib/rubocop/cop/style/unless_logical_operators.rb +105 -0
  259. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  260. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  261. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
  262. data/lib/rubocop/cop/util.rb +4 -1
  263. data/lib/rubocop/directive_comment.rb +69 -9
  264. data/lib/rubocop/ext/regexp_parser.rb +3 -6
  265. data/lib/rubocop/formatter/clang_style_formatter.rb +4 -2
  266. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  267. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
  268. data/lib/rubocop/formatter/tap_formatter.rb +4 -2
  269. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  270. data/lib/rubocop/magic_comment.rb +1 -1
  271. data/lib/rubocop/name_similarity.rb +1 -1
  272. data/lib/rubocop/target_finder.rb +1 -0
  273. data/lib/rubocop/target_ruby.rb +21 -13
  274. data/lib/rubocop/version.rb +1 -1
  275. metadata +14 -7
@@ -34,7 +34,10 @@ module RuboCop
34
34
  end
35
35
 
36
36
  def self.create(hash, path, check: true)
37
- new(hash, path).tap { |config| config.check if check }
37
+ config = new(hash, path)
38
+ config.check if check
39
+
40
+ config
38
41
  end
39
42
 
40
43
  def loaded_features
@@ -34,6 +34,7 @@ module RuboCop
34
34
  class Base # rubocop:disable Metrics/ClassLength
35
35
  extend RuboCop::AST::Sexp
36
36
  extend NodePattern::Macros
37
+ extend ExcludeLimit
37
38
  include RuboCop::AST::Sexp
38
39
  include Util
39
40
  include IgnoredNode
@@ -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,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # Allows specified configuration options to have an exclude limit
5
+ # ie. a maximum value tracked that it can be used by `--auto-gen-config`.
6
+ module ExcludeLimit
7
+ # Sets up a configuration option to have an exclude limit tracked.
8
+ # The parameter name given is transformed into a method name (eg. `Max`
9
+ # becomes `self.max=` and `MinDigits` becomes `self.min_digits=`).
10
+ def exclude_limit(parameter_name, method_name: transform(parameter_name))
11
+ define_method("#{method_name}=") do |value|
12
+ cfg = config_to_allow_offenses
13
+ cfg[:exclude_limit] ||= {}
14
+ current_max = cfg[:exclude_limit][parameter_name]
15
+ value = [current_max, value].max if current_max
16
+ cfg[:exclude_limit][parameter_name] = value
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def transform(parameter_name)
23
+ parameter_name.gsub(/(?<!\A)(?=[A-Z])/, '_').downcase
24
+ end
25
+ end
26
+ end
@@ -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