rubocop 1.8.0 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -13
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +89 -22
  5. data/config/obsoletion.yml +4 -0
  6. data/lib/rubocop.rb +9 -0
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +5 -4
  8. data/lib/rubocop/cli/command/execute_runner.rb +1 -1
  9. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  10. data/lib/rubocop/config.rb +5 -2
  11. data/lib/rubocop/config_loader.rb +7 -14
  12. data/lib/rubocop/config_store.rb +12 -1
  13. data/lib/rubocop/cop/base.rb +2 -1
  14. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
  15. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  16. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
  17. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
  18. data/lib/rubocop/cop/exclude_limit.rb +26 -0
  19. data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
  20. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
  21. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
  22. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  23. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
  24. data/lib/rubocop/cop/generator.rb +3 -5
  25. data/lib/rubocop/cop/internal_affairs.rb +6 -1
  26. data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +68 -0
  27. data/lib/rubocop/cop/internal_affairs/example_description.rb +90 -0
  28. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  29. data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
  30. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
  31. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  32. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
  33. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +62 -0
  34. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +65 -0
  35. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  36. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
  37. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
  38. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  39. data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
  40. data/lib/rubocop/cop/layout/class_structure.rb +8 -2
  41. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
  42. data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
  43. data/lib/rubocop/cop/layout/first_argument_indentation.rb +22 -3
  44. data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
  45. data/lib/rubocop/cop/layout/line_length.rb +2 -1
  46. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
  47. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  48. data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
  49. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  50. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
  51. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
  52. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
  53. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
  54. data/lib/rubocop/cop/lint/debugger.rb +60 -14
  55. data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
  56. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
  57. data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
  58. data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
  59. data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
  60. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  61. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  62. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
  63. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
  64. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
  65. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
  66. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
  67. data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
  68. data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
  69. data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
  70. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
  71. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
  72. data/lib/rubocop/cop/lint/number_conversion.rb +43 -6
  73. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +47 -0
  74. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +39 -0
  75. data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
  76. data/lib/rubocop/cop/lint/rand_one.rb +1 -0
  77. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +5 -3
  78. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  79. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
  80. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
  81. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
  82. data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
  83. data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
  84. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
  85. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
  86. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
  87. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  88. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
  89. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  90. data/lib/rubocop/cop/lint/symbol_conversion.rb +103 -0
  91. data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
  92. data/lib/rubocop/cop/lint/triple_quotes.rb +71 -0
  93. data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
  94. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
  95. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
  96. data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
  97. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
  98. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
  99. data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
  100. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
  101. data/lib/rubocop/cop/lint/useless_times.rb +3 -0
  102. data/lib/rubocop/cop/message_annotator.rb +4 -1
  103. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  104. data/lib/rubocop/cop/metrics/module_length.rb +1 -0
  105. data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
  106. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
  107. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
  108. data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -0
  109. data/lib/rubocop/cop/mixin/code_length.rb +3 -1
  110. data/lib/rubocop/cop/mixin/comments_help.rb +0 -1
  111. data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
  112. data/lib/rubocop/cop/mixin/def_node.rb +1 -0
  113. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
  114. data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
  115. data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
  116. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
  117. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
  118. data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
  119. data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
  120. data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
  121. data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
  122. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +5 -1
  123. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
  124. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
  125. data/lib/rubocop/cop/naming/constant_name.rb +2 -0
  126. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -0
  127. data/lib/rubocop/cop/naming/method_name.rb +3 -0
  128. data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
  129. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +38 -5
  130. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  131. data/lib/rubocop/cop/registry.rb +1 -1
  132. data/lib/rubocop/cop/security/eval.rb +1 -0
  133. data/lib/rubocop/cop/security/json_load.rb +1 -0
  134. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  135. data/lib/rubocop/cop/security/open.rb +1 -0
  136. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  137. data/lib/rubocop/cop/severity.rb +3 -3
  138. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
  139. data/lib/rubocop/cop/style/alias.rb +1 -0
  140. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
  141. data/lib/rubocop/cop/style/array_coercion.rb +2 -0
  142. data/lib/rubocop/cop/style/array_join.rb +1 -0
  143. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  144. data/lib/rubocop/cop/style/attr.rb +1 -0
  145. data/lib/rubocop/cop/style/case_equality.rb +2 -1
  146. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -0
  147. data/lib/rubocop/cop/style/collection_compact.rb +2 -0
  148. data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
  149. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  150. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
  151. data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
  152. data/lib/rubocop/cop/style/date_time.rb +3 -0
  153. data/lib/rubocop/cop/style/dir.rb +1 -0
  154. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -9
  155. data/lib/rubocop/cop/style/documentation.rb +5 -0
  156. data/lib/rubocop/cop/style/documentation_method.rb +1 -0
  157. data/lib/rubocop/cop/style/double_negation.rb +3 -2
  158. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
  159. data/lib/rubocop/cop/style/each_with_object.rb +1 -0
  160. data/lib/rubocop/cop/style/empty_literal.rb +9 -0
  161. data/lib/rubocop/cop/style/endless_method.rb +1 -0
  162. data/lib/rubocop/cop/style/eval_with_location.rb +140 -49
  163. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  164. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
  165. data/lib/rubocop/cop/style/explicit_block_argument.rb +12 -1
  166. data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
  167. data/lib/rubocop/cop/style/float_division.rb +7 -0
  168. data/lib/rubocop/cop/style/format_string.rb +2 -0
  169. data/lib/rubocop/cop/style/format_string_token.rb +19 -2
  170. data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
  171. data/lib/rubocop/cop/style/hash_conversion.rb +105 -0
  172. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
  173. data/lib/rubocop/cop/style/hash_except.rb +1 -0
  174. data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
  175. data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
  176. data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
  177. data/lib/rubocop/cop/style/if_inside_else.rb +14 -7
  178. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +122 -0
  179. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  180. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  181. data/lib/rubocop/cop/style/min_max.rb +1 -0
  182. data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
  183. data/lib/rubocop/cop/style/module_function.rb +5 -0
  184. data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
  185. data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
  186. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -0
  187. data/lib/rubocop/cop/style/nil_comparison.rb +6 -0
  188. data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
  189. data/lib/rubocop/cop/style/non_nil_check.rb +30 -13
  190. data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
  191. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
  192. data/lib/rubocop/cop/style/option_hash.rb +1 -0
  193. data/lib/rubocop/cop/style/or_assignment.rb +2 -0
  194. data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
  195. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
  196. data/lib/rubocop/cop/style/proc.rb +1 -0
  197. data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
  198. data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
  199. data/lib/rubocop/cop/style/redundant_begin.rb +7 -1
  200. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
  201. data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
  202. data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
  203. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  204. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
  205. data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
  206. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
  207. data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
  208. data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
  209. data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
  210. data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
  211. data/lib/rubocop/cop/style/return_nil.rb +6 -0
  212. data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
  213. data/lib/rubocop/cop/style/sample.rb +1 -0
  214. data/lib/rubocop/cop/style/signal_exception.rb +3 -0
  215. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  216. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  217. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
  218. data/lib/rubocop/cop/style/sole_nested_conditional.rb +28 -4
  219. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  220. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  221. data/lib/rubocop/cop/style/string_concatenation.rb +2 -1
  222. data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
  223. data/lib/rubocop/cop/style/strip.rb +1 -0
  224. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -0
  225. data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
  226. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -1
  227. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -0
  228. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  229. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
  230. data/lib/rubocop/cop/style/unless_logical_operators.rb +99 -0
  231. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  232. data/lib/rubocop/cop/style/while_until_modifier.rb +2 -4
  233. data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
  234. data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
  235. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -0
  236. data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
  237. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
  238. data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
  239. data/lib/rubocop/magic_comment.rb +30 -1
  240. data/lib/rubocop/name_similarity.rb +1 -1
  241. data/lib/rubocop/options.rb +1 -1
  242. data/lib/rubocop/rspec/expect_offense.rb +5 -2
  243. data/lib/rubocop/runner.rb +1 -0
  244. data/lib/rubocop/target_ruby.rb +21 -13
  245. data/lib/rubocop/version.rb +2 -2
  246. metadata +21 -7
@@ -33,6 +33,7 @@ module RuboCop
33
33
  MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'
34
34
  MSG_WITH_INDEX = 'Remove redundant `with_index`.'
35
35
 
36
+ # @!method redundant_with_index?(node)
36
37
  def_node_matcher :redundant_with_index?, <<~PATTERN
37
38
  (block
38
39
  $(send
@@ -34,6 +34,7 @@ module RuboCop
34
34
 
35
35
  MSG_WITH_OBJECT = 'Remove redundant `with_object`.'
36
36
 
37
+ # @!method redundant_with_object?(node)
37
38
  def_node_matcher :redundant_with_object?, <<~PATTERN
38
39
  (block
39
40
  $(send _ {:each_with_object :with_object}
@@ -29,6 +29,7 @@ module RuboCop
29
29
  MSG = 'Do not chain ordinary method call' \
30
30
  ' after safe navigation operator.'
31
31
 
32
+ # @!method bad_method?(node)
32
33
  def_node_matcher :bad_method?, <<~PATTERN
33
34
  {
34
35
  (send $(csend ...) $_ ...)
@@ -25,6 +25,7 @@ module RuboCop
25
25
  MSG = 'Avoid calling `empty?` with the safe navigation operator ' \
26
26
  'in conditionals.'
27
27
 
28
+ # @!method safe_navigation_empty_in_conditional?(node)
28
29
  def_node_matcher :safe_navigation_empty_in_conditional?, <<~PATTERN
29
30
  (if (csend (send ...) :empty?) ...)
30
31
  PATTERN
@@ -43,6 +43,7 @@ module RuboCop
43
43
  SEND_METHODS = %i[send public_send __send__].freeze
44
44
  RESTRICT_ON_SEND = SEND_METHODS
45
45
 
46
+ # @!method send_with_mixin_argument?(node)
46
47
  def_node_matcher :send_with_mixin_argument?, <<~PATTERN
47
48
  (send
48
49
  (const _ _) {:#{SEND_METHODS.join(' :')}}
@@ -67,6 +67,7 @@ module RuboCop
67
67
  MSG = 'Argument `%<argument>s` was shadowed by a local variable ' \
68
68
  'before it was used.'
69
69
 
70
+ # @!method uses_var?(node)
70
71
  def_node_search :uses_var?, '(lvar %)'
71
72
 
72
73
  def self.joining_forces
@@ -42,6 +42,7 @@ module RuboCop
42
42
  class ShadowingOuterLocalVariable < Base
43
43
  MSG = 'Shadowing outer local variable - `%<variable>s`.'
44
44
 
45
+ # @!method ractor_block?(node)
45
46
  def_node_matcher :ractor_block?, <<~PATTERN
46
47
  (block (send (const nil? :Ractor) :new ...) ...)
47
48
  PATTERN
@@ -29,6 +29,7 @@ module RuboCop
29
29
  STRUCT_METHOD_NAMES = Struct.instance_methods
30
30
  STRUCT_MEMBER_NAME_TYPES = %i[sym str].freeze
31
31
 
32
+ # @!method struct_new(node)
32
33
  def_node_matcher :struct_new, <<~PATTERN
33
34
  (send
34
35
  (const ${nil? cbase} :Struct) :new ...)
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks for uses of literal strings converted to
7
+ # a symbol where a literal symbol could be used instead.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # 'string'.to_sym
12
+ # :symbol.to_sym
13
+ # 'underscored_string'.to_sym
14
+ # :'underscored_symbol'
15
+ # 'hyphenated-string'.to_sym
16
+ #
17
+ # # good
18
+ # :string
19
+ # :symbol
20
+ # :underscored_string
21
+ # :underscored_symbol
22
+ # :'hyphenated-string'
23
+ #
24
+ class SymbolConversion < Base
25
+ extend AutoCorrector
26
+
27
+ MSG = 'Unnecessary symbol conversion; use `%<correction>s` instead.'
28
+ RESTRICT_ON_SEND = %i[to_sym intern].freeze
29
+
30
+ def on_send(node)
31
+ return unless node.receiver
32
+ return unless node.receiver.str_type? || node.receiver.sym_type?
33
+
34
+ register_offense(node, correction: node.receiver.value.to_sym.inspect)
35
+ end
36
+
37
+ def on_sym(node)
38
+ return if properly_quoted?(node.source, node.value.inspect)
39
+
40
+ # `alias` arguments are symbols but since a symbol that requires
41
+ # being quoted is not a valid method identifier, it can be ignored
42
+ return if in_alias?(node)
43
+
44
+ # The `%I[]` and `%i[]` macros are parsed as normal arrays of symbols
45
+ # so they need to be ignored.
46
+ return if in_percent_literal_array?(node)
47
+
48
+ # Symbol hash keys have a different format and need to be handled separately
49
+ return correct_hash_key(node) if hash_key?(node)
50
+
51
+ register_offense(node, correction: node.value.inspect)
52
+ end
53
+
54
+ private
55
+
56
+ def register_offense(node, correction:, message: format(MSG, correction: correction))
57
+ add_offense(node, message: message) do |corrector|
58
+ corrector.replace(node, correction)
59
+ end
60
+ end
61
+
62
+ def properly_quoted?(source, value)
63
+ return true if !source.match?(/['"]/) || value.end_with?('=')
64
+
65
+ source == value ||
66
+ # `Symbol#inspect` uses double quotes, but allow single-quoted
67
+ # symbols to work as well.
68
+ source.tr("'", '"') == value
69
+ end
70
+
71
+ def in_alias?(node)
72
+ node.parent&.alias_type?
73
+ end
74
+
75
+ def in_percent_literal_array?(node)
76
+ node.parent&.array_type? && node.parent&.percent_literal?
77
+ end
78
+
79
+ def hash_key?(node)
80
+ node.parent&.pair_type? && node == node.parent.child_nodes.first
81
+ end
82
+
83
+ def correct_hash_key(node)
84
+ # Although some operators can be converted to symbols normally
85
+ # (ie. `:==`), these are not accepted as hash keys and will
86
+ # raise a syntax error (eg. `{ ==: ... }`). Therefore, if the
87
+ # symbol does not start with an alpha-numeric or underscore, it
88
+ # will be ignored.
89
+ return unless node.value.to_s.match?(/\A[a-z0-9_]/i)
90
+
91
+ correction = node.value.inspect.gsub(/\A:/, '')
92
+ return if properly_quoted?(node.source, correction)
93
+
94
+ register_offense(
95
+ node,
96
+ correction: correction,
97
+ message: format(MSG, correction: "#{correction}:")
98
+ )
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -23,14 +23,17 @@ module RuboCop
23
23
 
24
24
  RESTRICT_ON_SEND = %i[to_enum enum_for].freeze
25
25
 
26
+ # @!method enum_conversion_call?(node)
26
27
  def_node_matcher :enum_conversion_call?, <<~PATTERN
27
28
  (send {nil? self} {:to_enum :enum_for} $_ $...)
28
29
  PATTERN
29
30
 
31
+ # @!method method_name?(node, name)
30
32
  def_node_matcher :method_name?, <<~PATTERN
31
33
  {(send nil? {:__method__ :__callee__}) (sym %1)}
32
34
  PATTERN
33
35
 
36
+ # @!method passing_keyword_arg?(node, name)
34
37
  def_node_matcher :passing_keyword_arg?, <<~PATTERN
35
38
  (pair (sym %1) (lvar %1))
36
39
  PATTERN
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks for "triple quotes" (strings delimted by any odd number
7
+ # of quotes greater than 1).
8
+ #
9
+ # Ruby allows multiple strings to be implicitly concatenated by just
10
+ # being adjacent in a statement (ie. `"foo""bar" == "foobar"`). This sometimes
11
+ # gives the impression that there is something special about triple quotes, but
12
+ # in fact it is just extra unnecessary quotes and produces the same string. Each
13
+ # pair of quotes produces an additional concatenated empty string, so the result
14
+ # is still only the "actual" string within the delimiters.
15
+ #
16
+ # NOTE: Although this cop is called triple quotes, the same behavior is present
17
+ # for strings delimited by 5, 7, etc. quotation marks.
18
+ #
19
+ # @example
20
+ # # bad
21
+ # """
22
+ # A string
23
+ # """
24
+ #
25
+ # # bad
26
+ # '''
27
+ # A string
28
+ # '''
29
+ #
30
+ # # good
31
+ # "
32
+ # A string
33
+ # "
34
+ #
35
+ # # good
36
+ # <<STRING
37
+ # A string
38
+ # STRING
39
+ #
40
+ # # good (but not the same spacing as the bad case)
41
+ # 'A string'
42
+ class TripleQuotes < Base
43
+ extend AutoCorrector
44
+
45
+ MSG = 'Delimiting a string with multiple quotes has no effect, use a single quote instead.'
46
+
47
+ def on_dstr(node)
48
+ return if (empty_str_nodes = empty_str_nodes(node)).none?
49
+
50
+ opening_quotes = node.source.scan(/(?<=\A)['"]*/)[0]
51
+ return if opening_quotes.size < 3
52
+
53
+ # If the node is composed of only empty `str` nodes, keep one
54
+ empty_str_nodes.shift if empty_str_nodes.size == node.child_nodes.size
55
+
56
+ add_offense(node) do |corrector|
57
+ empty_str_nodes.each do |str|
58
+ corrector.remove(str)
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def empty_str_nodes(node)
66
+ node.each_child_node(:str).select { |str| str.value == '' }
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -22,6 +22,7 @@ module RuboCop
22
22
 
23
23
  MSG = 'Use `Integer` instead of `%<klass>s`.'
24
24
 
25
+ # @!method fixnum_or_bignum_const(node)
25
26
  def_node_matcher :fixnum_or_bignum_const, <<~PATTERN
26
27
  (:const {nil? (:cbase)} ${:Fixnum :Bignum})
27
28
  PATTERN
@@ -66,6 +66,7 @@ module RuboCop
66
66
  MSG = 'Ensure the accumulator `%<accum>s` will be modified by `%<method>s`.'
67
67
  MSG_INDEX = 'Do not return an element of the accumulator in `%<method>s`.'
68
68
 
69
+ # @!method reduce_with_block?(node)
69
70
  def_node_matcher :reduce_with_block?, <<~PATTERN
70
71
  {
71
72
  (block (send _recv {:reduce :inject} ...) args ...)
@@ -73,10 +74,12 @@ module RuboCop
73
74
  }
74
75
  PATTERN
75
76
 
77
+ # @!method accumulator_index?(node, accumulator_name)
76
78
  def_node_matcher :accumulator_index?, <<~PATTERN
77
79
  (send (lvar %1) {:[] :[]=} ...)
78
80
  PATTERN
79
81
 
82
+ # @!method element_modified?(node, element_name)
80
83
  def_node_search :element_modified?, <<~PATTERN
81
84
  {
82
85
  (send _receiver !{:[] :[]=} <`(lvar %1) `_ ...>) # method(el, ...)
@@ -86,6 +89,7 @@ module RuboCop
86
89
  }
87
90
  PATTERN
88
91
 
92
+ # @!method lvar_used?(node, name)
89
93
  def_node_matcher :lvar_used?, <<~PATTERN
90
94
  {
91
95
  (lvar %1)
@@ -96,6 +100,7 @@ module RuboCop
96
100
  }
97
101
  PATTERN
98
102
 
103
+ # @!method expression_values(node)
99
104
  def_node_search :expression_values, <<~PATTERN
100
105
  {
101
106
  (%RuboCop::AST::Node::VARIABLES $_)
@@ -51,6 +51,7 @@ module RuboCop
51
51
 
52
52
  private
53
53
 
54
+ # @!method flow_command?(node)
54
55
  def_node_matcher :flow_command?, <<~PATTERN
55
56
  {
56
57
  return next break retry redo
@@ -131,6 +131,7 @@ module RuboCop
131
131
  end
132
132
  end
133
133
 
134
+ # @!method break_command?(node)
134
135
  def_node_matcher :break_command?, <<~PATTERN
135
136
  {
136
137
  return break
@@ -62,6 +62,7 @@ module RuboCop
62
62
  include UnusedArgument
63
63
  extend AutoCorrector
64
64
 
65
+ # @!method not_implemented?(node)
65
66
  def_node_matcher :not_implemented?, <<~PATTERN
66
67
  {(send nil? :raise (const {nil? cbase} :NotImplementedError))
67
68
  (send nil? :fail ...)}
@@ -47,6 +47,7 @@ module RuboCop
47
47
  METHOD_NAMES = %i[escape encode unescape decode].freeze
48
48
  RESTRICT_ON_SEND = METHOD_NAMES
49
49
 
50
+ # @!method uri_escape_unescape?(node)
50
51
  def_node_matcher :uri_escape_unescape?, <<~PATTERN
51
52
  (send
52
53
  (const ${nil? cbase} :URI) ${:#{METHOD_NAMES.join(' :')}}
@@ -150,18 +150,22 @@ module RuboCop
150
150
  corrector.remove(range)
151
151
  end
152
152
 
153
+ # @!method static_method_definition?(node)
153
154
  def_node_matcher :static_method_definition?, <<~PATTERN
154
155
  {def (send nil? {:attr :attr_reader :attr_writer :attr_accessor} ...)}
155
156
  PATTERN
156
157
 
158
+ # @!method dynamic_method_definition?(node)
157
159
  def_node_matcher :dynamic_method_definition?, <<~PATTERN
158
160
  {(send nil? :define_method ...) (block (send nil? :define_method ...) ...)}
159
161
  PATTERN
160
162
 
163
+ # @!method class_or_instance_eval?(node)
161
164
  def_node_matcher :class_or_instance_eval?, <<~PATTERN
162
165
  (block (send _ {:class_eval :instance_eval}) ...)
163
166
  PATTERN
164
167
 
168
+ # @!method class_or_module_or_struct_new_call?(node)
165
169
  def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
166
170
  (block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
167
171
  PATTERN
@@ -56,6 +56,7 @@ module RuboCop
56
56
 
57
57
  private
58
58
 
59
+ # @!method setter_call_to_local_variable?(node)
59
60
  def_node_matcher :setter_call_to_local_variable?, <<~PATTERN
60
61
  [(send (lvar _) ...) setter_method?]
61
62
  PATTERN
@@ -27,14 +27,17 @@ module RuboCop
27
27
  MSG = 'Useless call to `%<count>i.times` detected.'
28
28
  RESTRICT_ON_SEND = %i[times].freeze
29
29
 
30
+ # @!method times_call?(node)
30
31
  def_node_matcher :times_call?, <<~PATTERN
31
32
  (send (int $_) :times (block-pass (sym $_))?)
32
33
  PATTERN
33
34
 
35
+ # @!method block_arg(node)
34
36
  def_node_matcher :block_arg, <<~PATTERN
35
37
  (block _ (args (arg $_)) ...)
36
38
  PATTERN
37
39
 
40
+ # @!method block_reassigns_arg?(node)
38
41
  def_node_search :block_reassigns_arg?, <<~PATTERN
39
42
  (lvasgn %)
40
43
  PATTERN
@@ -85,8 +85,11 @@ module RuboCop
85
85
  end
86
86
  end
87
87
 
88
+ # Returns the base style guide URL from AllCops or the specific department
89
+ #
90
+ # @return [String] style guide URL
88
91
  def style_guide_base_url
89
- department_name = cop_name.split('/').first
92
+ department_name = cop_name.split('/')[0..-2].join('/')
90
93
 
91
94
  config.for_department(department_name)['StyleGuideBaseURL'] ||
92
95
  config.for_all_cops['StyleGuideBaseURL']
@@ -12,13 +12,13 @@ module RuboCop
12
12
  #
13
13
  # The maximum level of nesting allowed is configurable.
14
14
  class BlockNesting < Base
15
- include ConfigurableMax
16
-
17
15
  NESTING_BLOCKS = %i[
18
16
  case if while while_post
19
17
  until until_post for resbody
20
18
  ].freeze
21
19
 
20
+ exclude_limit 'Max'
21
+
22
22
  def on_new_investigation
23
23
  return if processed_source.blank?
24
24
 
@@ -44,6 +44,7 @@ module RuboCop
44
44
 
45
45
  private
46
46
 
47
+ # @!method module_definition?(node)
47
48
  def_node_matcher :module_definition?, <<~PATTERN
48
49
  (casgn nil? _ (block (send (const {nil? cbase} :Module) :new) ...))
49
50
  PATTERN
@@ -51,7 +51,8 @@ module RuboCop
51
51
  # end
52
52
  #
53
53
  class ParameterLists < Base
54
- include ConfigurableMax
54
+ exclude_limit 'Max'
55
+ exclude_limit 'MaxOptionalParameters'
55
56
 
56
57
  MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
57
58
  '[%<count>d/%<max>d]'
@@ -70,7 +71,9 @@ module RuboCop
70
71
  count: optargs.count
71
72
  )
72
73
 
73
- add_offense(node, message: message)
74
+ add_offense(node, message: message) do
75
+ self.max_optional_parameters = optargs.count
76
+ end
74
77
  end
75
78
  alias on_defs on_def
76
79
 
@@ -87,6 +90,7 @@ module RuboCop
87
90
 
88
91
  private
89
92
 
93
+ # @!method argument_to_lambda_or_proc?(node)
90
94
  def_node_matcher :argument_to_lambda_or_proc?, <<~PATTERN
91
95
  ^lambda_or_proc?
92
96
  PATTERN