rubocop 1.92.1 → 1.92.3

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 (160) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubocop/#directive_comment.rb# +224 -0
  3. data/lib/rubocop/cache_config.rb +3 -0
  4. data/lib/rubocop/cli.rb +5 -4
  5. data/lib/rubocop/config.rb +16 -4
  6. data/lib/rubocop/config_obsoletion.rb +9 -0
  7. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
  8. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  9. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
  10. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
  11. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
  12. data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
  13. data/lib/rubocop/cop/layout/class_structure.rb +91 -13
  14. data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
  15. data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
  16. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
  17. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +15 -7
  18. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
  19. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
  20. data/lib/rubocop/cop/layout/end_alignment.rb +6 -12
  21. data/lib/rubocop/cop/layout/extra_spacing.rb +22 -12
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
  23. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
  24. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
  25. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  26. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  27. data/lib/rubocop/cop/layout/line_length.rb +23 -5
  28. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
  29. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
  30. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
  31. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  32. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
  33. data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
  34. data/lib/rubocop/cop/layout/space_around_operators.rb +31 -4
  35. data/lib/rubocop/cop/layout/space_before_block_braces.rb +12 -0
  36. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
  37. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  38. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +29 -11
  39. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
  40. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
  41. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  42. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
  43. data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
  44. data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
  45. data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
  46. data/lib/rubocop/cop/lint/interpolation_check.rb +27 -18
  47. data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
  48. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
  49. data/lib/rubocop/cop/lint/nested_percent_literal.rb +3 -0
  50. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
  51. data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
  52. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +66 -3
  53. data/lib/rubocop/cop/lint/percent_string_array.rb +75 -9
  54. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +45 -8
  55. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
  56. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
  57. data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
  58. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +11 -4
  59. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
  60. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
  61. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
  62. data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
  63. data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
  64. data/lib/rubocop/cop/lint/useless_times.rb +13 -1
  65. data/lib/rubocop/cop/lint/void.rb +20 -9
  66. data/lib/rubocop/cop/message_annotator.rb +6 -8
  67. data/lib/rubocop/cop/mixin/comments_help.rb +2 -2
  68. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
  69. data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
  70. data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
  71. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +17 -1
  72. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  73. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
  74. data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
  75. data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
  76. data/lib/rubocop/cop/mixin/range_help.rb +13 -0
  77. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
  78. data/lib/rubocop/cop/mixin/statement_modifier.rb +14 -1
  79. data/lib/rubocop/cop/mixin/trailing_comma.rb +4 -2
  80. data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
  81. data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
  82. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
  83. data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
  84. data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
  85. data/lib/rubocop/cop/style/and_or.rb +41 -11
  86. data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
  87. data/lib/rubocop/cop/style/case_equality.rb +14 -3
  88. data/lib/rubocop/cop/style/class_and_module_children.rb +12 -15
  89. data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
  90. data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
  91. data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
  92. data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
  93. data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
  94. data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
  95. data/lib/rubocop/cop/style/empty_literal.rb +7 -0
  96. data/lib/rubocop/cop/style/empty_method.rb +16 -4
  97. data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
  98. data/lib/rubocop/cop/style/endless_method.rb +64 -16
  99. data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
  100. data/lib/rubocop/cop/style/float_division.rb +16 -1
  101. data/lib/rubocop/cop/style/for.rb +59 -4
  102. data/lib/rubocop/cop/style/guard_clause.rb +56 -11
  103. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  104. data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
  105. data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
  106. data/lib/rubocop/cop/style/hash_lookup_method.rb +21 -2
  107. data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
  108. data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
  109. data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
  110. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
  111. data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
  112. data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
  113. data/lib/rubocop/cop/style/lambda.rb +13 -5
  114. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  115. data/lib/rubocop/cop/style/map_into_array.rb +4 -0
  116. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
  117. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
  118. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +12 -1
  119. data/lib/rubocop/cop/style/method_def_parentheses.rb +9 -1
  120. data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
  121. data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
  122. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  123. data/lib/rubocop/cop/style/numeric_predicate.rb +7 -0
  124. data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
  125. data/lib/rubocop/cop/style/parentheses_around_condition.rb +20 -10
  126. data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
  127. data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
  128. data/lib/rubocop/cop/style/quoted_symbols.rb +5 -7
  129. data/lib/rubocop/cop/style/raise_args.rb +66 -7
  130. data/lib/rubocop/cop/style/reduce_to_hash.rb +32 -34
  131. data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
  132. data/lib/rubocop/cop/style/redundant_begin.rb +120 -18
  133. data/lib/rubocop/cop/style/redundant_each.rb +8 -1
  134. data/lib/rubocop/cop/style/redundant_exception.rb +13 -9
  135. data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
  136. data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
  137. data/lib/rubocop/cop/style/redundant_self.rb +9 -1
  138. data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
  139. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  140. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
  141. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  142. data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
  143. data/lib/rubocop/cop/style/single_line_methods.rb +26 -2
  144. data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
  145. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
  146. data/lib/rubocop/cop/style/string_concatenation.rb +28 -8
  147. data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
  148. data/lib/rubocop/cop/style/symbol_array.rb +1 -3
  149. data/lib/rubocop/cop/style/word_array.rb +1 -3
  150. data/lib/rubocop/cop/util.rb +2 -0
  151. data/lib/rubocop/cop/variable_force.rb +14 -1
  152. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  153. data/lib/rubocop/formatter/junit_formatter.rb +13 -6
  154. data/lib/rubocop/plugin/configuration_integrator.rb +24 -3
  155. data/lib/rubocop/result_cache.rb +9 -3
  156. data/lib/rubocop/runner.rb +6 -1
  157. data/lib/rubocop/server/cache.rb +24 -5
  158. data/lib/rubocop/todo_audit.rb +61 -15
  159. data/lib/rubocop/version.rb +1 -1
  160. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f289b93d86b59bdf80728f8130f7daeb1b6b6f940d6144cc425b627efe00061
4
- data.tar.gz: c87922f68ef6b6fb2d1cdf8ac29998908f6be8f7d39d8ed14b7a0772d33a6b69
3
+ metadata.gz: dbea37d22f7a4329c753283a2265783f5cb6f029ca8ee58eecb95d78a71aa829
4
+ data.tar.gz: b757c016c48a6efa892be211e33d5d54748fd1312d28b800b55033b28b1e3c47
5
5
  SHA512:
6
- metadata.gz: cc6d21f74027a289aa14bf41cacbf840a0d156c93a33fa3526ee75607cb6d85ca3a6e219f4117c4d70462ef80fd8ae81d62ffd74cc8451c89c67754c4c719c87
7
- data.tar.gz: 8a0a4af9e4cbba6b0c242461cacba0ef1fe5ba6049e5d5814eeac5db118f73a18142927ea33ea43a9ffd7b751f340893abd861eeb7fcc502506897d09c74e860
6
+ metadata.gz: 380deff3fb32d85b956a3b9e37adfa346ebbe09350fc762b4b762413df0eeb8e986489eed2419d5970ef365c4445d1a356b4a9600f1555f42b1d34455f27baf1
7
+ data.tar.gz: 04151cda0a03b6c870c14380c2c318da725c17333befedd9ff4ea1937c260b97f09d3ce999559e980f51d3daaf6967185f5ae1df99ff2fe654a07f93751ee80b
@@ -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
 
@@ -56,10 +56,6 @@ module RuboCop
56
56
  deprecated_config = @for_cop[deprecated_cop_name]
57
57
  next if deprecated_config.empty?
58
58
 
59
- warn Rainbow(<<~WARNING).yellow
60
- Warning: Using `#{deprecated_cop_name}` configuration in #{loaded_path} for `#{cop}`.
61
- WARNING
62
-
63
59
  cop_options.merge!(@for_cop[deprecated_cop_name])
64
60
  end
65
61
  end
@@ -188,6 +184,22 @@ module RuboCop
188
184
  @clusivity_config_exists_cache[badge.to_s] = cop_config['Include'] || cop_config['Exclude']
189
185
  end
190
186
 
187
+ # Loading a plugin merges the configuration of its cops into the default configuration,
188
+ # which only the configurations resolved afterwards are based on. For a configuration
189
+ # resolved by `ConfigLoader`, a cop that has an entry in the default configuration but not in
190
+ # this configuration therefore comes from a plugin that this configuration does not load,
191
+ # such as a `rubocop-rspec` cop for the root `.rubocop.yml` when only `spec/.rubocop.yml`
192
+ # loads `rubocop-rspec`.
193
+ #
194
+ # @return [Boolean] whether the default configuration has an entry for the cop of the badge
195
+ # that this configuration does not have
196
+ # @api private
197
+ def missing_default_config_for?(badge)
198
+ cop_name = badge.to_s
199
+
200
+ !key?(cop_name) && ConfigLoader.default_configuration.key?(cop_name)
201
+ end
202
+
191
203
  # @return [Config] for the given department name.
192
204
  # Note: the 'Enabled' attribute will be present only if specified
193
205
  # at the department's level
@@ -76,6 +76,15 @@ module RuboCop
76
76
  raise ValidationError, messages.join("\n")
77
77
  end
78
78
 
79
+ # Messages for every violated rule, regardless of severity, without raising.
80
+ # Used for configuration the user does not own (a plugin's defaults), where an
81
+ # obsolete key should be reported but must not stop the run. Extracted cops are
82
+ # left out: whether their gem is loaded is only known once every plugin has been
83
+ # integrated, and this is called while plugins are still loading.
84
+ def messages
85
+ rules.grep_v(ExtractedCop).select(&:violated?).map(&:message)
86
+ end
87
+
79
88
  def legacy_cop_names
80
89
  # Used by DepartmentName#qualified_legacy_cop_name
81
90
  cop_rules.map(&:old_name)
@@ -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)
@@ -12,7 +12,7 @@ module RuboCop
12
12
  when :no_empty_lines
13
13
  corrector.remove(range)
14
14
  when :empty_lines
15
- corrector.insert_before(range.begin, "\n")
15
+ corrector.replace(range.begin.adjust(begin_pos: -1), "\n\n")
16
16
  end
17
17
  end
18
18
 
@@ -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 |#{variable_node.source}|"
42
32
  end
43
33
 
44
34
  def requires_parentheses?
@@ -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)
@@ -57,17 +57,21 @@ module RuboCop
57
57
 
58
58
  def check_body(body, node)
59
59
  modifiers = body.each_child_node(:send).select(&:bare_access_modifier?)
60
- end_range = node.loc.end
60
+ base_range = indentation_base_range(node)
61
61
 
62
62
  modifiers.each do |modifier|
63
63
  next if same_line?(node, modifier)
64
64
 
65
- check_modifier(modifier, end_range)
65
+ check_modifier(modifier, base_range)
66
66
  end
67
67
  end
68
68
 
69
- def check_modifier(send_node, end_range)
70
- offset = column_offset_between(send_node.source_range, end_range)
69
+ def indentation_base_range(node)
70
+ node.any_block_type? ? node.loc.end : node.loc.keyword
71
+ end
72
+
73
+ def check_modifier(send_node, base_range)
74
+ offset = column_offset_between(send_node.source_range, base_range)
71
75
 
72
76
  @column_delta = expected_indent_offset - offset
73
77
  if @column_delta.zero?
@@ -30,6 +30,19 @@ module RuboCop
30
30
 
31
31
  MSG = 'Expression at %<line>d, %<column>d should be on its own line.'
32
32
 
33
+ # Moving the closing brace below a heredoc leaves behind text that another cop
34
+ # inserts right after the brace in the same pass.
35
+ def self.autocorrect_incompatible_with
36
+ [
37
+ Lint::AmbiguousBlockAssociation,
38
+ Style::MethodCallWithArgsParentheses,
39
+ Style::NestedParenthesizedCalls,
40
+ Style::TrailingCommaInArguments,
41
+ Style::TrailingCommaInArrayLiteral,
42
+ Style::TrailingCommaInHashLiteral
43
+ ]
44
+ end
45
+
33
46
  def on_block(node)
34
47
  return if node.single_line?
35
48
 
@@ -49,13 +62,12 @@ module RuboCop
49
62
 
50
63
  def register_offense(node, offense_range)
51
64
  add_offense(node.loc.end, message: message(node)) do |corrector|
52
- replacement = "\n#{offense_range.source.lstrip}"
65
+ next if overlaps_inner_heredoc_correction?(node)
53
66
 
54
- if (heredoc = last_heredoc_argument(node.body))
55
- corrector.remove(offense_range)
56
- corrector.insert_after(heredoc.loc.heredoc_end, replacement)
67
+ if (heredoc = heredoc_argument_after_block_end(node))
68
+ correct_with_heredoc(corrector, offense_range, heredoc)
57
69
  else
58
- corrector.replace(offense_range, replacement)
70
+ corrector.replace(offense_range, "\n#{offense_range.source.lstrip}")
59
71
  end
60
72
  end
61
73
  end
@@ -64,6 +76,25 @@ module RuboCop
64
76
  format(MSG, line: node.loc.end.line, column: node.loc.end.column + 1)
65
77
  end
66
78
 
79
+ def overlaps_inner_heredoc_correction?(node)
80
+ node.each_descendant(:any_block).any? do |block|
81
+ same_line?(block.loc.end, node.loc.end) && heredoc_correction?(block)
82
+ end
83
+ end
84
+
85
+ def heredoc_correction?(node)
86
+ return false if node.single_line?
87
+ return false if begins_its_line?(node.loc.end)
88
+
89
+ !heredoc_argument_after_block_end(node).nil?
90
+ end
91
+
92
+ def heredoc_argument_after_block_end(node)
93
+ return unless (heredoc = last_heredoc_argument(node.body))
94
+
95
+ heredoc if heredoc.loc.heredoc_end.begin_pos > node.loc.end.begin_pos
96
+ end
97
+
67
98
  def last_heredoc_argument(node)
68
99
  return unless node&.call_type?
69
100
  return unless (arguments = node&.arguments)
@@ -74,6 +105,21 @@ module RuboCop
74
105
  last_heredoc_argument(node.children.first)
75
106
  end
76
107
 
108
+ def correct_with_heredoc(corrector, offense_range, heredoc)
109
+ range = offense_range.with(end_pos: code_end_pos(offense_range))
110
+
111
+ corrector.remove(range)
112
+ corrector.insert_after(heredoc.loc.heredoc_end, "\n#{range.source.strip}")
113
+ end
114
+
115
+ def code_end_pos(range)
116
+ comment = processed_source.comment_at_line(range.last_line)
117
+ return range.source_buffer.line_range(range.last_line).end_pos if comment.nil?
118
+
119
+ code_range = range.with(end_pos: comment.source_range.begin_pos)
120
+ code_range.begin_pos + code_range.source.rstrip.length
121
+ end
122
+
77
123
  def offense_range(node)
78
124
  node.children.compact.last.source_range.end.join(node.loc.end)
79
125
  end