rubocop 1.92.2 → 1.93.0

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 (191) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +5 -1
  4. data/lib/rubocop/#directive_comment.rb# +224 -0
  5. data/lib/rubocop/cache_config.rb +3 -0
  6. data/lib/rubocop/cli.rb +5 -4
  7. data/lib/rubocop/config.rb +22 -0
  8. data/lib/rubocop/config_store.rb +1 -0
  9. data/lib/rubocop/config_validator.rb +45 -1
  10. data/lib/rubocop/cop/base.rb +5 -1
  11. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  12. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +32 -18
  13. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  14. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  15. data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
  16. data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
  17. data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
  18. data/lib/rubocop/cop/internal_affairs.rb +3 -0
  19. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  20. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  21. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  22. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  23. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  24. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  25. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +26 -6
  26. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +55 -16
  27. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  28. data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
  29. data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
  30. data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -2
  31. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  32. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  33. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  34. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  35. data/lib/rubocop/cop/layout/line_length.rb +14 -1
  36. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  37. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  38. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  39. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  40. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  41. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  42. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  43. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  44. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  45. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  46. data/lib/rubocop/cop/lint/duplicate_methods.rb +53 -15
  47. data/lib/rubocop/cop/lint/duplicate_require.rb +9 -2
  48. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  49. data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
  50. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
  51. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  52. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  53. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +53 -5
  54. data/lib/rubocop/cop/lint/number_conversion.rb +14 -0
  55. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
  56. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
  57. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  58. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  59. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  60. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
  61. data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
  62. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  63. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  64. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  65. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  66. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  67. data/lib/rubocop/cop/lint/useless_assignment.rb +71 -1
  68. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
  69. data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
  70. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
  71. data/lib/rubocop/cop/lint/useless_times.rb +52 -12
  72. data/lib/rubocop/cop/lint/void.rb +20 -9
  73. data/lib/rubocop/cop/message_annotator.rb +6 -8
  74. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  75. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +49 -18
  76. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  77. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  78. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  79. data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
  80. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  81. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  82. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  83. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  84. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  85. data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
  86. data/lib/rubocop/cop/naming/block_forwarding.rb +11 -3
  87. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  88. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  89. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  90. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  91. data/lib/rubocop/cop/style/and_or.rb +8 -1
  92. data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
  93. data/lib/rubocop/cop/style/array_coercion.rb +1 -1
  94. data/lib/rubocop/cop/style/array_intersect.rb +15 -7
  95. data/lib/rubocop/cop/style/block_delimiters.rb +5 -1
  96. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
  97. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  98. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  99. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  100. data/lib/rubocop/cop/style/data_inheritance.rb +82 -9
  101. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  102. data/lib/rubocop/cop/style/dig_chain.rb +13 -2
  103. data/lib/rubocop/cop/style/each_with_object.rb +21 -5
  104. data/lib/rubocop/cop/style/empty_class_definition.rb +21 -4
  105. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  106. data/lib/rubocop/cop/style/empty_method.rb +27 -4
  107. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  108. data/lib/rubocop/cop/style/endless_method.rb +30 -10
  109. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  110. data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
  111. data/lib/rubocop/cop/style/file_read.rb +5 -1
  112. data/lib/rubocop/cop/style/file_write.rb +89 -20
  113. data/lib/rubocop/cop/style/for.rb +59 -4
  114. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  115. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  116. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  117. data/lib/rubocop/cop/style/hash_lookup_method.rb +12 -5
  118. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  119. data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
  120. data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
  121. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  122. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  123. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  124. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  125. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  126. data/lib/rubocop/cop/style/lambda.rb +13 -5
  127. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  128. data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
  129. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  130. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  131. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
  132. data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
  133. data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
  134. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
  135. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  136. data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
  137. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
  138. data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
  139. data/lib/rubocop/cop/style/nil_lambda.rb +22 -17
  140. data/lib/rubocop/cop/style/non_nil_check.rb +14 -1
  141. data/lib/rubocop/cop/style/operator_method_call.rb +37 -24
  142. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
  143. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  144. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  145. data/lib/rubocop/cop/style/raise_args.rb +15 -0
  146. data/lib/rubocop/cop/style/redundant_array_constructor.rb +18 -1
  147. data/lib/rubocop/cop/style/redundant_begin.rb +117 -17
  148. data/lib/rubocop/cop/style/redundant_each.rb +26 -1
  149. data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
  150. data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
  151. data/lib/rubocop/cop/style/redundant_interpolation.rb +37 -16
  152. data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
  153. data/lib/rubocop/cop/style/redundant_percent_q.rb +15 -2
  154. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
  155. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  156. data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
  157. data/lib/rubocop/cop/style/redundant_sort.rb +6 -2
  158. data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
  159. data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
  160. data/lib/rubocop/cop/style/select_by_range.rb +1 -6
  161. data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
  162. data/lib/rubocop/cop/style/self_assignment.rb +0 -4
  163. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  164. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  165. data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
  166. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  167. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  168. data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
  169. data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
  170. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  171. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  172. data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
  173. data/lib/rubocop/cop/style/struct_inheritance.rb +82 -16
  174. data/lib/rubocop/cop/style/swap_values.rb +10 -13
  175. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  176. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  177. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
  178. data/lib/rubocop/cop/style/word_array.rb +1 -3
  179. data/lib/rubocop/cop/util.rb +2 -0
  180. data/lib/rubocop/cop/variable_force.rb +14 -1
  181. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  182. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  183. data/lib/rubocop/result_cache.rb +9 -3
  184. data/lib/rubocop/rspec/cop_helper.rb +8 -2
  185. data/lib/rubocop/rspec/shared_contexts.rb +11 -11
  186. data/lib/rubocop/runner.rb +21 -7
  187. data/lib/rubocop/server/cache.rb +24 -5
  188. data/lib/rubocop/target_ruby.rb +6 -8
  189. data/lib/rubocop/todo_audit.rb +61 -15
  190. data/lib/rubocop/version.rb +18 -8
  191. metadata +9 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e582386de45680cecfa5173359f6ccb24894b2440ef9d6fec5ec5c0d28d0b41
4
- data.tar.gz: ef3bdbb7e39a1960f5e1feb41df80f7afef2f35148a44693566f725b99715a3a
3
+ metadata.gz: f85c9581854e9f222ff94a59b3f3547ab227d98951a7f141cf7a2d1b0339c5e5
4
+ data.tar.gz: 4707a5a1502dc167f12e70a19c3d29d7860701aacc8fd350d2ed2d3489b4a4f3
5
5
  SHA512:
6
- metadata.gz: dff3c8ee84b5334d4aabb751f7de89b2cf2b87d65ad7970913132f558909d80da6306f60707f0ba1001d826a22c3491d9ab716c82defb20bb10216654cf75902
7
- data.tar.gz: 6c6913ed8f1d4f30ee0044df54c2c370aa59b2f1f91de340f33295af795fcd56138b75583c37049c3fd117ffd51241600bd24e0c5e82d41b58304821a8170a25
6
+ metadata.gz: 9dd135a4463eb93e7f656a2d50bda93a2e8bae7592f2573a69e8c9d951c902a737b790c0a2d2c7febfc9237a00b79318697cc2e1100f42419aaa3e89a00bcfb1
7
+ data.tar.gz: 45987d73a3f0b09ebf35b4bd24cb9cc481eead4a5ba31163b491f1ec35052455f9a90b9682c560bfffdabd399fb9d79da6ca104df486705d5506654f99898e60
data/README.md CHANGED
@@ -51,7 +51,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
51
51
  in your `Gemfile`:
52
52
 
53
53
  ```rb
54
- gem 'rubocop', '~> 1.92', require: false
54
+ gem 'rubocop', '~> 1.93', require: false
55
55
  ```
56
56
 
57
57
  See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
data/config/default.yml CHANGED
@@ -166,8 +166,12 @@ AllCops:
166
166
  # - `default`
167
167
  # - `parser_whitequark` ... https://github.com/whitequark/parser
168
168
  # - `parser_prism` ... https://github.com/ruby/prism (`Prism::Translation::Parser`)
169
+ # - `prism` ... https://github.com/ruby/prism (without the translation layer)
169
170
  # Not every parser engine can handle every `TargetRubyVersion`. `parser_whitequark`
170
- # only works with versions 3.4 and below, `parser_prism` with versions 3.3 and above.
171
+ # only works with versions 3.4 and below, `parser_prism` and `prism` with versions 3.3
172
+ # and above. `prism` needs Prism 2.0 or later. `default` is `parser_whitequark` for
173
+ # versions below 3.3; for the others it is `prism` when Prism 2.0 or later is installed,
174
+ # and `parser_prism` otherwise.
171
175
  ParserEngine: default
172
176
  # Determines if a notification for extension libraries should be shown when
173
177
  # rubocop is run. Keys are the name of the extension, and values are an array
@@ -0,0 +1,224 @@
1
+ et# frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # This class wraps the `Parser::Source::Comment` object that represents a
5
+ # special `rubocop:disable` and `rubocop:enable` comment and exposes what
6
+ # cops it contains.
7
+ class DirectiveComment
8
+ # @api private
9
+ LINT_DEPARTMENT = 'Lint'
10
+ # @api private
11
+ LINT_REDUNDANT_DIRECTIVE_COP = "#{LINT_DEPARTMENT}/RedundantCopDisableDirective"
12
+ # @api private
13
+ LINT_SYNTAX_COP = "#{LINT_DEPARTMENT}/Syntax"
14
+ # @api private
15
+ COP_NAME_PATTERN = '([A-Za-z]\w+/)*(?:[A-Za-z]\w+)'
16
+ # @api private
17
+ COP_NAME_PATTERN_NC = '(?:[A-Za-z]\w+/)*[A-Za-z]\w+'
18
+ # @api private
19
+ COP_NAMES_PATTERN_NC = "(?:#{COP_NAME_PATTERN_NC} , )*#{COP_NAME_PATTERN_NC}"
20
+ # @api private
21
+ COP_NAMES_PATTERN = "(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
22
+ # @api private
23
+ COPS_PATTERN = "(all|#{COP_NAMES_PATTERN})"
24
+ # @api private
25
+ PUSH_POP_ARGS_PATTERN = "([+\\-]#{COP_NAME_PATTERN_NC}(?:\\s+[+\\-]#{COP_NAME_PATTERN_NC})*)"
26
+ # @api private
27
+ AVAILABLE_MODES = %w[disable enable todo push pop].freeze
28
+ # @api private
29
+ DIRECTIVE_MARKER_PATTERN = '# rubocop : '
30
+ # @api private
31
+ DIRECTIVE_MARKER_REGEXP = Regexp.new(DIRECTIVE_MARKER_PATTERN.gsub(' ', '\s*'))
32
+ # @api private
33
+ DIRECTIVE_HEADER_PATTERN = "#{DIRECTIVE_MARKER_PATTERN}((?:#{AVAILABLE_MODES.join('|')}))\\b"
34
+ # @api private
35
+ DIRECTIVE_COMMENT_REGEXP = Regexp.new(
36
+ "#{DIRECTIVE_HEADER_PATTERN}(?:\\s+#{COPS_PATTERN}|\\s+#{PUSH_POP_ARGS_PATTERN})?"
37
+ .gsub(' ', '\s*')
38
+ )
39
+ # @api private
40
+ TRAILING_COMMENT_MARKER = '--'
41
+ # @api private
42
+ MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP = Regexp.new(
43
+ "\\A#{DIRECTIVE_HEADER_PATTERN}\\s*\\z".gsub(' ', '\s*')
44
+ )
45
+
46
+ def self.before_comment(line)
47
+ line.split(DIRECTIVE_COMMENT_REGEXP).first
48
+ end
49
+
50
+ attr_reader :comment, :cop_registry, :mode, :cops
51
+
52
+ def initialize(comment, cop_registry = Cop::Registry.global)
53
+ @comment = comment
54
+ @cop_registry = cop_registry
55
+ match_data = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
56
+ @match_data = match_data&.pre_match&.match?(/\A#\s*\z/) ? nil : match_data
57
+ @mode, @cops = match_captures
58
+ end
59
+
60
+ # Checks if the comment starts with `# rubocop:` marker
61
+ def start_with_marker?
62
+ comment.text.start_with?(DIRECTIVE_MARKER_REGEXP)
63
+ end
64
+
65
+ # Checks if the comment is malformed as a `# rubocop:` directive
66
+ def malformed?
67
+ return true if !start_with_marker? || @match_data.nil?
68
+ return true if missing_cop_name?
69
+
70
+ tail = @match_data.post_match.lstrip
71
+ !(tail.empty? || tail.start_with?(TRAILING_COMMENT_MARKER))
72
+ end
73
+
74
+ # Checks if the directive comment is missing a cop name
75
+ def missing_cop_name?
76
+ return false if push? || pop?
77
+
78
+ MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP.match?(comment.text)
79
+ end
80
+
81
+ # Checks if this directive relates to single line
82
+ def single_line?
83
+ !comment.text.start_with?(DIRECTIVE_COMMENT_REGEXP)
84
+ end
85
+
86
+ # Checks if this directive contains all the given cop names
87
+ def match?(cop_names)
88
+ parsed_cop_names.uniq.sort == cop_names.uniq.sort
89
+ end
90
+
91
+ def range
92
+ match = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
93
+ begin_pos = comment.source_range.begin_pos
94
+ Parser::Source::Range.new(
95
+ comment.source_range.source_buffer, begin_pos + match.begin(0), begin_pos + match.end(0)
96
+ )
97
+ end
98
+
99
+ # Returns match captures to directive comment pattern
100
+ def match_captures
101
+ @match_captures ||= @match_data && begin
102
+ captures = @match_data.captures
103
+ mode = captures[0]
104
+ # COPS_PATTERN is at captures[1], PUSH_POP_ARGS_PATTERN is at captures[4]
105
+ cops = captures[1] || captures[4]
106
+ [mode, cops]
107
+ end
108
+ end
109
+
110
+ # Checks if this directive disables cops
111
+ def disabled?
112
+ %w[disable todo].include?(mode)
113
+ end
114
+
115
+ # Checks if this directive enables cops
116
+ def enabled?
117
+ mode == 'enable'
118
+ end
119
+
120
+ # Checks if this directive is a push
121
+ def push?
122
+ mode == 'push'
123
+ end
124
+
125
+ # Checks if this directive is a pop
126
+ def pop?
127
+ mode == 'pop'
128
+ end
129
+
130
+ # Returns the push arguments as a hash of cop names with their operations
131
+ def push_args
132
+ @push_args ||= parse_push_args
133
+ end
134
+
135
+ # Checks if this directive enables all cops
136
+ def enabled_all?
137
+ !disabled? && all_cops?
138
+ end
139
+
140
+ # Checks if this directive disables all cops
141
+ def disabled_all?
142
+ disabled? && all_cops?
143
+ end
144
+
145
+ # Checks if all cops specified in this directive
146
+ def all_cops?
147
+ cops == 'all'
148
+ end
149
+
150
+ # Returns array of specified in this directive cop names
151
+ def cop_names
152
+ @cop_names ||= all_cops? ? all_cop_names : parsed_cop_names
153
+ end
154
+
155
+ # Returns an array of cops for this directive comment, without resolving departments
156
+ def raw_cop_names
157
+ @raw_cop_names ||= (cops || '').split(/,\s*/)
158
+ end
159
+
160
+ # Returns array of specified in this directive department names
161
+ # when all department disabled
162
+ def department_names
163
+ raw_cop_names.select { |cop| department?(cop) }
164
+ end
165
+
166
+ # Checks if directive departments include cop
167
+ def in_directive_department?(cop)
168
+ department_names.any? { |department| cop.start_with?(department) }
169
+ end
170
+
171
+ # Checks if cop department has already used in directive comment
172
+ def overridden_by_department?(cop)
173
+ in_directive_department?(cop) && raw_cop_names.include?(cop)
174
+ end
175
+
176
+ def directive_count
177
+ raw_cop_names.count
178
+ end
179
+
180
+ # Returns line number for directive
181
+ def line_number
182
+ comment.source_range.line
183
+ end
184
+
185
+ private
186
+
187
+ def parsed_cop_names
188
+ cops = raw_cop_names.map do |name|
189
+ department?(name) ? cop_names_for_department(name) : name
190
+ end.flatten
191
+ cops - [LINT_SYNTAX_COP]
192
+ end
193
+
194
+ def department?(name)
195
+ cop_registry.department?(name)
196
+ end
197
+
198
+ def all_cop_names
199
+ exclude_lint_department_cops(cop_registry.names)
200
+ end
201
+
202
+ def cop_names_for_department(department)
203
+ names = cop_registry.names_for_department(department)
204
+ department == LINT_DEPARTMENT ? exclude_lint_department_cops(names) : names
205
+ end
206
+
207
+ def exclude_lint_department_cops(cops)
208
+ cops - [LINT_REDUNDANT_DIRECTIVE_COP, LINT_SYNTAX_COP]
209
+ end
210
+
211
+ def parse_push_args
212
+ return {} unless push? && cops
213
+
214
+ args = {}
215
+ cops.split.each do |cop_spec|
216
+ op = cop_spec[0]
217
+ cop_name = cop_spec[1..]
218
+ args[op] ||= []
219
+ args[op] << cop_name
220
+ end
221
+ args
222
+ end
223
+ end
224
+ end
@@ -4,6 +4,9 @@ module RuboCop
4
4
  # This class represents the cache config of the caching RuboCop runs.
5
5
  # @api private
6
6
  class CacheConfig
7
+ # The directory within the cache root where the server keeps its state files.
8
+ SERVER_DIR = 'server'
9
+
7
10
  def self.root_dir
8
11
  root = ENV.fetch('RUBOCOP_CACHE_ROOT', nil)
9
12
  root ||= yield
data/lib/rubocop/cli.rb CHANGED
@@ -11,10 +11,11 @@ module RuboCop
11
11
  STATUS_ERROR = 2
12
12
  STATUS_INTERRUPTED = Signal.list['INT'] + 128
13
13
  DEFAULT_PARALLEL_OPTIONS = %i[
14
- color config debug display_style_guide display_time display_only_fail_level_offenses
15
- display_only_failed editor_mode except extra_details fail_level fix_layout format formatters
16
- ignore_disable_comments lint max_offenses_per_cop only only_guide_cops out preview require
17
- safe
14
+ color config debug display_cop_names display_style_guide display_time
15
+ display_only_fail_level_offenses display_only_failed editor_mode except extra_details
16
+ fail_level fix_layout force_exclusion format formatters ignore_disable_comments lint
17
+ max_offenses_per_cop only only_guide_cops out preview report_unused_todo_entries
18
+ require safe
18
19
  autocorrect safe_autocorrect autocorrect_all
19
20
  ].freeze
20
21
 
@@ -89,6 +89,12 @@ module RuboCop
89
89
  self
90
90
  end
91
91
 
92
+ # @api private
93
+ def validate_parser_engine
94
+ @validator.validate_parser_engine
95
+ self
96
+ end
97
+
92
98
  def_delegators :@hash, :[], :[]=, :delete, :dig, :each, :key?, :keys, :each_key,
93
99
  :fetch, :map, :merge, :replace, :to_h, :to_hash, :transform_values
94
100
  def_delegators :@validator, :validate, :target_ruby_version
@@ -184,6 +190,22 @@ module RuboCop
184
190
  @clusivity_config_exists_cache[badge.to_s] = cop_config['Include'] || cop_config['Exclude']
185
191
  end
186
192
 
193
+ # Loading a plugin merges the configuration of its cops into the default configuration,
194
+ # which only the configurations resolved afterwards are based on. For a configuration
195
+ # resolved by `ConfigLoader`, a cop that has an entry in the default configuration but not in
196
+ # this configuration therefore comes from a plugin that this configuration does not load,
197
+ # such as a `rubocop-rspec` cop for the root `.rubocop.yml` when only `spec/.rubocop.yml`
198
+ # loads `rubocop-rspec`.
199
+ #
200
+ # @return [Boolean] whether the default configuration has an entry for the cop of the badge
201
+ # that this configuration does not have
202
+ # @api private
203
+ def missing_default_config_for?(badge)
204
+ cop_name = badge.to_s
205
+
206
+ !key?(cop_name) && ConfigLoader.default_configuration.key?(cop_name)
207
+ end
208
+
187
209
  # @return [Config] for the given department name.
188
210
  # Note: the 'Enabled' attribute will be present only if specified
189
211
  # at the department's level
@@ -32,6 +32,7 @@ module RuboCop
32
32
 
33
33
  def options_config=(options_config)
34
34
  loaded_config = ConfigLoader.load_file(options_config)
35
+ loaded_config.validate_after_resolution
35
36
  @options_config = ConfigLoader.merge_with_default(loaded_config, options_config)
36
37
  end
37
38
 
@@ -23,7 +23,10 @@ module RuboCop
23
23
  CONFIG_CHECK_KEYS = %w[Enabled Safe SafeAutoCorrect AutoCorrect References].to_set.freeze
24
24
  CONFIG_CHECK_DEPARTMENTS = %w[pending preview override_department].freeze
25
25
  CONFIG_CHECK_AUTOCORRECTS = %w[always contextual disabled].freeze
26
- private_constant :CONFIG_CHECK_KEYS, :CONFIG_CHECK_DEPARTMENTS
26
+ PRISM_PARSER_ENGINES = %i[parser_prism prism].freeze
27
+ PARSER_GEM_ENGINES = %i[parser_whitequark parser_prism].freeze
28
+ private_constant :CONFIG_CHECK_KEYS, :CONFIG_CHECK_DEPARTMENTS, :PRISM_PARSER_ENGINES,
29
+ :PARSER_GEM_ENGINES
27
30
 
28
31
  def_delegators :@config, :smart_loaded_path, :for_all_cops
29
32
 
@@ -64,6 +67,16 @@ module RuboCop
64
67
  check_target_ruby
65
68
  end
66
69
 
70
+ # Checks that the parser engine can parse the target Ruby version. This is
71
+ # for the runner to call before it parses a file, because a configuration
72
+ # that no file is inspected with is free to name any engine.
73
+ def validate_parser_engine
74
+ return if @parser_engine_validated
75
+
76
+ check_parser_engine
77
+ @parser_engine_validated = true
78
+ end
79
+
67
80
  def target_ruby_version
68
81
  target_ruby.version
69
82
  end
@@ -99,6 +112,37 @@ module RuboCop
99
112
  raise ValidationError, msg
100
113
  end
101
114
 
115
+ def check_parser_engine
116
+ parser_engine = @config.parser_engine
117
+ prism = PRISM_PARSER_ENGINES.include?(parser_engine)
118
+ old_ruby = target_ruby_version < Version::MINIMUM_PARSABLE_PRISM_VERSION
119
+
120
+ if prism && old_ruby
121
+ raise ValidationError,
122
+ "The `#{parser_engine}` parser engine supports target Ruby versions " \
123
+ "#{Version::MINIMUM_PARSABLE_PRISM_VERSION} and above, but RuboCop found Ruby " \
124
+ "version #{target_ruby_version} in #{target_ruby.source}."
125
+ end
126
+ return if ProcessedSource.parser_gem_available?
127
+
128
+ check_parser_gem(parser_engine, old_ruby)
129
+ end
130
+
131
+ def check_parser_gem(parser_engine, old_ruby)
132
+ if old_ruby
133
+ raise ValidationError,
134
+ "RuboCop found Ruby version #{target_ruby_version} in #{target_ruby.source}, and " \
135
+ 'analyzing code for Ruby versions below ' \
136
+ "#{Version::MINIMUM_PARSABLE_PRISM_VERSION} needs the `parser` gem. " \
137
+ 'Add it to your Gemfile.'
138
+ end
139
+ return unless PARSER_GEM_ENGINES.include?(parser_engine)
140
+
141
+ raise ValidationError, "The `#{parser_engine}` parser engine needs the `parser` gem. " \
142
+ 'Add it to your Gemfile, or remove `ParserEngine` from your ' \
143
+ 'configuration.'
144
+ end
145
+
102
146
  def alert_about_unrecognized_cops(invalid_cop_names)
103
147
  unknown_cops = list_unknown_cops(invalid_cop_names)
104
148
 
@@ -319,8 +319,12 @@ module RuboCop
319
319
  end
320
320
 
321
321
  # There should be very limited reasons for a Cop to do it's own parsing
322
+ # The source is parsed the way the file under investigation was.
322
323
  def parse(source, path = nil)
323
- ProcessedSource.new(source, target_ruby_version, path, parser_engine: parser_engine)
324
+ ruby_version = processed_source&.ruby_version || target_ruby_version
325
+ engine = processed_source&.parser_engine || parser_engine
326
+
327
+ ProcessedSource.new(source, ruby_version, path, parser_engine: engine)
324
328
  end
325
329
 
326
330
  # @api private
@@ -6,9 +6,6 @@ module RuboCop
6
6
  class EachToForCorrector
7
7
  extend NodePattern::Macros
8
8
 
9
- CORRECTION_WITH_ARGUMENTS = 'for %<variables>s in %<collection>s do'
10
- CORRECTION_WITHOUT_ARGUMENTS = 'for _ in %<enumerable>s do'
11
-
12
9
  def initialize(block_node)
13
10
  @block_node = block_node
14
11
  @collection_node = block_node.receiver
@@ -16,7 +13,8 @@ module RuboCop
16
13
  end
17
14
 
18
15
  def call(corrector)
19
- corrector.replace(offending_range, correction)
16
+ corrector.insert_before(block_node, "for #{loop_variables} in ")
17
+ corrector.replace(offending_range, opening_keyword)
20
18
  corrector.replace(block_node.loc.end, closing_keyword) if block_node.braces?
21
19
  end
22
20
 
@@ -24,21 +22,9 @@ module RuboCop
24
22
 
25
23
  attr_reader :block_node, :collection_node, :argument_node
26
24
 
27
- def correction
28
- keyword = if block_node.arguments?
29
- format(
30
- CORRECTION_WITH_ARGUMENTS,
31
- collection: collection_node.source,
32
- variables: loop_variables
33
- )
34
- else
35
- format(CORRECTION_WITHOUT_ARGUMENTS, enumerable: collection_node.source)
36
- end
37
-
38
- padding_needed?(offending_range.end_pos) ? "#{keyword} " : keyword
39
- end
40
-
41
25
  def loop_variables
26
+ return '_' unless block_node.arguments?
27
+
42
28
  variables = argument_node.children.map { |argument| loop_variable(argument) }.join(', ')
43
29
 
44
30
  argument_node.source.match?(/,\s*\|\z/) ? "#{variables}," : variables
@@ -53,6 +39,10 @@ module RuboCop
53
39
  lone_target ? "(#{variables},)" : "(#{variables})"
54
40
  end
55
41
 
42
+ def opening_keyword
43
+ padding_needed?(offending_range.end_pos) ? ' do ' : ' do'
44
+ end
45
+
56
46
  def closing_keyword
57
47
  padding_needed?(block_node.loc.end.begin_pos - 1) ? ' end' : 'end'
58
48
  end
@@ -64,7 +54,7 @@ module RuboCop
64
54
  end
65
55
 
66
56
  def offending_range
67
- begin_range = block_node.source_range.begin
57
+ begin_range = collection_node.source_range.end
68
58
 
69
59
  if block_node.arguments?
70
60
  begin_range.join(argument_node.source_range.end)
@@ -6,8 +6,6 @@ module RuboCop
6
6
  class ForToEachCorrector
7
7
  extend NodePattern::Macros
8
8
 
9
- CORRECTION = '%<collection>s%<dot>seach do |%<argument>s|'
10
-
11
9
  def initialize(for_node)
12
10
  @for_node = for_node
13
11
  @variable_node = for_node.variable
@@ -15,9 +13,11 @@ module RuboCop
15
13
  end
16
14
 
17
15
  def call(corrector)
18
- offending_range = for_node.source_range.begin.join(end_range)
19
-
20
- corrector.replace(offending_range, correction)
16
+ corrector.replace(
17
+ for_node.source_range.begin.join(collection_node.source_range.begin),
18
+ requires_parentheses? ? '(' : ''
19
+ )
20
+ corrector.replace(collection_end.end.join(end_range), correction)
21
21
  end
22
22
 
23
23
  private
@@ -25,20 +25,10 @@ module RuboCop
25
25
  attr_reader :for_node, :variable_node, :collection_node
26
26
 
27
27
  def correction
28
- format(
29
- CORRECTION,
30
- collection: collection_source,
31
- dot: collection_node.csend_type? ? '&.' : '.',
32
- argument: variable_node.source
33
- )
34
- end
28
+ closing_parenthesis = requires_parentheses? ? ')' : ''
29
+ dot = collection_node.csend_type? ? '&.' : '.'
35
30
 
36
- def collection_source
37
- if requires_parentheses?
38
- "(#{collection_node.source})"
39
- else
40
- collection_node.source
41
- end
31
+ "#{closing_parenthesis}#{dot}each do |#{block_parameters}|"
42
32
  end
43
33
 
44
34
  def requires_parentheses?
@@ -66,6 +56,30 @@ module RuboCop
66
56
  collection_node.source_range
67
57
  end
68
58
  end
59
+
60
+ def block_parameters
61
+ parameters = variable_node.source_range
62
+ source = parameters.source
63
+
64
+ trailing_commas_in_parentheses.reverse_each do |comma|
65
+ source[comma.begin_pos - parameters.begin_pos, comma.size] = ''
66
+ end
67
+ source << ',' if trailing_comma_required?
68
+ source
69
+ end
70
+
71
+ def trailing_commas_in_parentheses
72
+ variable_node.each_node(:mlhs).filter_map do |mlhs|
73
+ next unless mlhs.loc_is?(:end, ')')
74
+
75
+ gap = mlhs.children.last.source_range.end.join(mlhs.loc.end.begin)
76
+ gap if gap.source.include?(',')
77
+ end.sort_by(&:begin_pos)
78
+ end
79
+
80
+ def trailing_comma_required?
81
+ variable_node.mlhs_type? && variable_node.children.one? && !variable_node.loc.begin
82
+ end
69
83
  end
70
84
  end
71
85
  end
@@ -45,19 +45,16 @@ module RuboCop
45
45
  end
46
46
 
47
47
  def nested_closing_bracket_moved_before_comment?
48
- last_element = children(node).last
49
- return false unless (comment = processed_source.comment_at_line(last_element.last_line - 1))
50
- return false unless (nested = nested_literal_with_closing_bracket_on_own_line(last_element))
48
+ last_line = children(node).last.last_line
51
49
 
52
- own_line_comment?(comment) || !(nested.chained? || nested.argument?)
50
+ node.each_descendant.any? { |nested| nested_closing_bracket_to_move?(nested, last_line) }
53
51
  end
54
52
 
55
- def nested_literal_with_closing_bracket_on_own_line(last_element)
56
- last_line = last_element.last_line
53
+ def nested_closing_bracket_to_move?(descendant, line)
54
+ return false unless same_kind_as_node?(descendant)
55
+ return false unless closing_bracket_on_own_line?(descendant, line)
57
56
 
58
- last_element.each_node.find do |descendant|
59
- same_kind_as_node?(descendant) && closing_bracket_on_own_line?(descendant, last_line)
60
- end
57
+ closing_bracket_moved_before_comment?(descendant)
61
58
  end
62
59
 
63
60
  def same_kind_as_node?(descendant)
@@ -73,15 +70,26 @@ module RuboCop
73
70
  closing_bracket.source_line[0...closing_bracket.column].strip.empty?
74
71
  end
75
72
 
76
- def own_line_comment?(comment)
77
- comment.source_range.source_line.lstrip.start_with?('#')
73
+ def closing_bracket_moved_before_comment?(literal)
74
+ last_element = literal.call_type? ? literal.last_argument : literal.children.last
75
+ return false if last_element.nil?
76
+ return false if new_line_needed_before_closing_brace?(literal)
77
+
78
+ comment_before_closing_brace?(literal, last_element)
78
79
  end
79
80
 
80
81
  def correct_next_line_brace(corrector, end_range)
81
- corrector.remove(range_with_surrounding_space(node.loc.end, side: :left))
82
+ corrector.remove(space_before_closing_brace(end_range))
82
83
  corrector.insert_before(end_range, content_if_comment_present(corrector, node))
83
84
  end
84
85
 
86
+ def space_before_closing_brace(end_range)
87
+ range = range_with_surrounding_space(node.loc.end, side: :left, continuations: true)
88
+ return range if range.begin_pos >= end_range.end_pos
89
+
90
+ range.with(begin_pos: end_range.end_pos)
91
+ end
92
+
85
93
  def correct_heredoc_argument_method_chain(corrector, end_range)
86
94
  return unless (parent = node.parent)
87
95
  return unless use_heredoc_argument_method_chain?(parent)
@@ -93,13 +101,19 @@ module RuboCop
93
101
  end
94
102
 
95
103
  def content_if_comment_present(corrector, node)
96
- if processed_source.comment_at_line(children(node).last.last_line)
104
+ if comment_before_closing_brace?(node, children(node).last)
97
105
  select_content_to_be_inserted_after_last_element(corrector, node)
98
106
  else
99
107
  node.loc.end.source
100
108
  end
101
109
  end
102
110
 
111
+ def comment_before_closing_brace?(literal, last_element)
112
+ lines_before_closing_brace = last_element.last_line...literal.loc.end.line
113
+
114
+ processed_source.each_comment_in_lines(lines_before_closing_brace).any?
115
+ end
116
+
103
117
  def use_heredoc_argument_method_chain?(parent)
104
118
  return false unless node.respond_to?(:first_argument)
105
119
  return false unless (first_argument = node.first_argument)
@@ -47,7 +47,7 @@ module RuboCop
47
47
  private
48
48
 
49
49
  def gem_spec_with_ruby_version?(node)
50
- gem_specification(processed_source.ast) && ruby_version?(node)
50
+ ruby_version?(node) && gem_specification(processed_source.ast).any?
51
51
  end
52
52
  end
53
53
  end