rubocop 0.75.0 → 0.79.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 (192) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +341 -316
  4. data/lib/rubocop.rb +48 -31
  5. data/lib/rubocop/ast/builder.rb +43 -41
  6. data/lib/rubocop/ast/node.rb +5 -1
  7. data/lib/rubocop/ast/node/block_node.rb +2 -0
  8. data/lib/rubocop/ast/node/def_node.rb +11 -0
  9. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  10. data/lib/rubocop/ast/node/return_node.rb +24 -0
  11. data/lib/rubocop/ast/traversal.rb +11 -3
  12. data/lib/rubocop/cli.rb +11 -227
  13. data/lib/rubocop/cli/command.rb +21 -0
  14. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  15. data/lib/rubocop/cli/command/base.rb +33 -0
  16. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  17. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  18. data/lib/rubocop/cli/command/show_cops.rb +80 -0
  19. data/lib/rubocop/cli/command/version.rb +17 -0
  20. data/lib/rubocop/cli/environment.rb +21 -0
  21. data/lib/rubocop/comment_config.rb +2 -2
  22. data/lib/rubocop/config.rb +8 -1
  23. data/lib/rubocop/config_loader.rb +20 -20
  24. data/lib/rubocop/config_loader_resolver.rb +2 -1
  25. data/lib/rubocop/config_obsoletion.rb +73 -11
  26. data/lib/rubocop/config_validator.rb +77 -110
  27. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  28. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  30. data/lib/rubocop/cop/commissioner.rb +15 -7
  31. data/lib/rubocop/cop/cop.rb +31 -6
  32. data/lib/rubocop/cop/corrector.rb +8 -7
  33. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  34. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  35. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  36. data/lib/rubocop/cop/generator.rb +3 -4
  37. data/lib/rubocop/cop/generator/configuration_injector.rb +2 -2
  38. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  39. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  40. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  41. data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
  42. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +3 -2
  43. data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
  44. data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
  45. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +22 -7
  46. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
  47. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
  48. data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
  49. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  50. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +14 -12
  51. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  52. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
  53. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  54. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +16 -8
  55. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
  56. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  57. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +41 -114
  58. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  59. data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
  60. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  61. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  62. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
  63. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  64. data/lib/rubocop/cop/layout/space_around_operators.rb +32 -7
  65. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  66. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
  67. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
  68. data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
  69. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  70. data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
  71. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  72. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  73. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  74. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  75. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
  76. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  77. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
  78. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  79. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  80. data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +26 -26
  81. data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +10 -12
  82. data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
  83. data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +6 -6
  84. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  85. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  86. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  87. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
  88. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  89. data/lib/rubocop/cop/lint/unused_block_argument.rb +22 -6
  90. data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
  91. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  92. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  93. data/lib/rubocop/cop/lint/void.rb +7 -26
  94. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  95. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  96. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
  97. data/lib/rubocop/cop/migration/department_name.rb +16 -1
  98. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  99. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -7
  100. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  101. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  102. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  103. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  104. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  105. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -4
  106. data/lib/rubocop/cop/mixin/trailing_comma.rb +14 -9
  107. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  108. data/lib/rubocop/cop/naming/file_name.rb +12 -5
  109. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  110. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
  111. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  112. data/lib/rubocop/cop/offense.rb +11 -0
  113. data/lib/rubocop/cop/registry.rb +8 -3
  114. data/lib/rubocop/cop/style/alias.rb +1 -1
  115. data/lib/rubocop/cop/style/array_join.rb +1 -1
  116. data/lib/rubocop/cop/style/attr.rb +10 -2
  117. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -6
  118. data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
  119. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  120. data/lib/rubocop/cop/style/copyright.rb +11 -7
  121. data/lib/rubocop/cop/style/documentation_method.rb +44 -0
  122. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
  123. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
  124. data/lib/rubocop/cop/style/empty_literal.rb +2 -2
  125. data/lib/rubocop/cop/style/empty_method.rb +5 -5
  126. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  127. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  128. data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
  129. data/lib/rubocop/cop/style/format_string.rb +10 -7
  130. data/lib/rubocop/cop/style/format_string_token.rb +15 -34
  131. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +10 -0
  132. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  133. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  134. data/lib/rubocop/cop/style/if_unless_modifier.rb +45 -3
  135. data/lib/rubocop/cop/style/infinite_loop.rb +5 -4
  136. data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
  137. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  138. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
  139. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +24 -227
  140. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +168 -0
  141. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  142. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
  143. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  144. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  145. data/lib/rubocop/cop/style/multiline_when_then.rb +6 -2
  146. data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
  147. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
  148. data/lib/rubocop/cop/style/next.rb +5 -5
  149. data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
  150. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  151. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  152. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  153. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  154. data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
  155. data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
  156. data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
  157. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  158. data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
  159. data/lib/rubocop/cop/style/redundant_return.rb +39 -29
  160. data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
  161. data/lib/rubocop/cop/style/safe_navigation.rb +19 -8
  162. data/lib/rubocop/cop/style/semicolon.rb +13 -2
  163. data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
  164. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  165. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  166. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  167. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  168. data/lib/rubocop/cop/team.rb +5 -0
  169. data/lib/rubocop/cop/util.rb +1 -1
  170. data/lib/rubocop/cop/utils/format_string.rb +10 -18
  171. data/lib/rubocop/cop/variable_force.rb +7 -5
  172. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  173. data/lib/rubocop/formatter/clang_style_formatter.rb +9 -6
  174. data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -12
  175. data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
  176. data/lib/rubocop/formatter/formatter_set.rb +16 -16
  177. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  178. data/lib/rubocop/formatter/pacman_formatter.rb +3 -3
  179. data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
  180. data/lib/rubocop/formatter/tap_formatter.rb +9 -6
  181. data/lib/rubocop/node_pattern.rb +4 -2
  182. data/lib/rubocop/options.rb +20 -26
  183. data/lib/rubocop/processed_source.rb +1 -1
  184. data/lib/rubocop/rake_task.rb +1 -0
  185. data/lib/rubocop/result_cache.rb +24 -8
  186. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  187. data/lib/rubocop/runner.rb +50 -29
  188. data/lib/rubocop/target_finder.rb +12 -6
  189. data/lib/rubocop/target_ruby.rb +151 -0
  190. data/lib/rubocop/version.rb +1 -1
  191. metadata +50 -34
  192. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
@@ -1,20 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Metrics/ClassLength
4
3
  module RuboCop
5
4
  # The CLI is a class responsible of handling all the command line interface
6
5
  # logic.
7
6
  class CLI
8
- include Formatter::TextUtil
9
-
10
- PHASE_1 = 'Phase 1 of 2: run Metrics/LineLength cop'
11
- PHASE_2 = 'Phase 2 of 2: run all cops'
12
-
13
- PHASE_1_OVERRIDDEN = '(skipped because the default Metrics/LineLength:Max' \
14
- ' is overridden)'
15
- PHASE_1_DISABLED = '(skipped because Metrics/LineLength is ' \
16
- 'disabled)'
17
-
18
7
  STATUS_SUCCESS = 0
19
8
  STATUS_OFFENSES = 1
20
9
  STATUS_ERROR = 2
@@ -41,14 +30,15 @@ module RuboCop
41
30
  # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
42
31
  def run(args = ARGV)
43
32
  @options, paths = Options.new.parse(args)
33
+ @env = Environment.new(@options, @config_store, paths)
44
34
 
45
35
  if @options[:init]
46
- init_dotfile
36
+ run_command(:init)
47
37
  else
48
38
  validate_options_vs_config
49
39
  act_on_options
50
40
  apply_default_formatter
51
- execute_runners(paths)
41
+ execute_runners
52
42
  end
53
43
  rescue ConfigNotFoundError, IncorrectCopNameError, OptionArgumentError => e
54
44
  warn e.message
@@ -71,110 +61,18 @@ module RuboCop
71
61
 
72
62
  private
73
63
 
74
- def execute_runners(paths)
75
- if @options[:auto_gen_config]
76
- reset_config_and_auto_gen_file
77
- line_length_contents = maybe_run_line_length_cop(paths)
78
- run_all_cops_auto_gen_config(line_length_contents, paths)
79
- else
80
- execute_runner(paths)
81
- end
82
- end
83
-
84
- def maybe_run_line_length_cop(paths)
85
- if !line_length_enabled?(@config_store.for(Dir.pwd))
86
- puts Rainbow("#{PHASE_1} #{PHASE_1_DISABLED}").yellow
87
- ''
88
- elsif !same_max_line_length?(
89
- @config_store.for(Dir.pwd), ConfigLoader.default_configuration
90
- )
91
- puts Rainbow("#{PHASE_1} #{PHASE_1_OVERRIDDEN}").yellow
92
- ''
93
- else
94
- run_line_length_cop_auto_gen_config(paths)
95
- end
96
- end
97
-
98
- def line_length_enabled?(config)
99
- line_length_cop(config)['Enabled']
100
- end
101
-
102
- def same_max_line_length?(config1, config2)
103
- max_line_length(config1) == max_line_length(config2)
104
- end
105
-
106
- def max_line_length(config)
107
- line_length_cop(config)['Max']
108
- end
109
-
110
- def line_length_cop(config)
111
- config.for_cop('Metrics/LineLength')
64
+ def run_command(name)
65
+ @env.run(name)
112
66
  end
113
67
 
114
- # Do an initial run with only Metrics/LineLength so that cops that depend
115
- # on Metrics/LineLength:Max get the correct value for that parameter.
116
- def run_line_length_cop_auto_gen_config(paths)
117
- puts Rainbow(PHASE_1).yellow
118
- @options[:only] = ['Metrics/LineLength']
119
- execute_runner(paths)
120
- @options.delete(:only)
121
- @config_store = ConfigStore.new
122
- # Save the todo configuration of the LineLength cop.
123
- IO.read(ConfigLoader::AUTO_GENERATED_FILE)
124
- .lines
125
- .drop_while { |line| line.start_with?('#') }
126
- .join
127
- end
128
-
129
- def run_all_cops_auto_gen_config(line_length_contents, paths)
130
- puts Rainbow(PHASE_2).yellow
131
- result = execute_runner(paths)
132
- # This run was made with the current maximum length allowed, so append
133
- # the saved setting for LineLength.
134
- File.open(ConfigLoader::AUTO_GENERATED_FILE, 'a') do |f|
135
- f.write(line_length_contents)
136
- end
137
- result
138
- end
139
-
140
- def init_dotfile
141
- path = File.expand_path(ConfigLoader::DOTFILE)
142
-
143
- if File.exist?(ConfigLoader::DOTFILE)
144
- warn Rainbow("#{ConfigLoader::DOTFILE} already exists at #{path}").red
145
-
146
- STATUS_ERROR
68
+ def execute_runners
69
+ if @options[:auto_gen_config]
70
+ run_command(:auto_gen_config)
147
71
  else
148
- description = <<~DESC
149
- # The behavior of RuboCop can be controlled via the .rubocop.yml
150
- # configuration file. It makes it possible to enable/disable
151
- # certain cops (checks) and to alter their behavior if they accept
152
- # any parameters. The file can be placed either in your home
153
- # directory or in some project directory.
154
- #
155
- # RuboCop will start looking for the configuration file in the directory
156
- # where the inspected file is and continue its way up to the root directory.
157
- #
158
- # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
159
- DESC
160
-
161
- File.open(ConfigLoader::DOTFILE, 'w') do |f|
162
- f.write(description)
163
- end
164
-
165
- puts "Writing new #{ConfigLoader::DOTFILE} to #{path}"
166
-
167
- STATUS_SUCCESS
72
+ run_command(:execute_runner)
168
73
  end
169
74
  end
170
75
 
171
- def reset_config_and_auto_gen_file
172
- @config_store = ConfigStore.new
173
- @config_store.options_config = @options[:config] if @options[:config]
174
- File.open(ConfigLoader::AUTO_GENERATED_FILE, 'w') {}
175
- ConfigLoader.add_inheritance_from_auto_generated_file
176
- end
177
-
178
76
  def validate_options_vs_config
179
77
  if @options[:parallel] &&
180
78
  !@config_store.for(Dir.pwd).for_all_cops['UseCache']
@@ -204,31 +102,11 @@ module RuboCop
204
102
  end
205
103
  end
206
104
 
207
- def execute_runner(paths)
208
- runner = Runner.new(@options, @config_store)
209
-
210
- all_passed = runner.run(paths)
211
- display_warning_summary(runner.warnings)
212
- display_error_summary(runner.errors)
213
- maybe_print_corrected_source
214
-
215
- all_pass_or_excluded = all_passed || @options[:auto_gen_config]
216
-
217
- if runner.aborting?
218
- STATUS_INTERRUPTED
219
- elsif all_pass_or_excluded && runner.errors.empty?
220
- STATUS_SUCCESS
221
- else
222
- STATUS_OFFENSES
223
- end
224
- end
225
-
226
105
  def handle_exiting_options
227
106
  return unless Options::EXITING_OPTIONS.any? { |o| @options.key? o }
228
107
 
229
- puts RuboCop::Version.version(false) if @options[:version]
230
- puts RuboCop::Version.version(true) if @options[:verbose_version]
231
- print_available_cops if @options[:show_cops]
108
+ run_command(:version) if @options[:version] || @options[:verbose_version]
109
+ run_command(:show_cops) if @options[:show_cops]
232
110
  raise Finished
233
111
  end
234
112
 
@@ -244,100 +122,6 @@ module RuboCop
244
122
  end
245
123
  [[formatter, @options[:output_path]]]
246
124
  end
247
-
248
- return unless @options[:auto_gen_config]
249
-
250
- @options[:formatters] << [Formatter::DisabledConfigFormatter,
251
- ConfigLoader::AUTO_GENERATED_FILE]
252
- end
253
-
254
- def print_available_cops
255
- # Load the configs so the require()s are done for custom cops
256
- @config_store.for(Dir.pwd)
257
- registry = Cop::Cop.registry
258
- show_all = @options[:show_cops].empty?
259
-
260
- if show_all
261
- puts "# Available cops (#{registry.length}) + config for #{Dir.pwd}: "
262
- end
263
-
264
- registry.departments.sort!.each do |department|
265
- print_cops_of_department(registry, department, show_all)
266
- end
267
- end
268
-
269
- def print_cops_of_department(registry, department, show_all)
270
- selected_cops = if show_all
271
- cops_of_department(registry, department)
272
- else
273
- selected_cops_of_department(registry, department)
274
- end
275
-
276
- puts "# Department '#{department}' (#{selected_cops.length}):" if show_all
277
-
278
- print_cop_details(selected_cops)
279
- end
280
-
281
- def print_cop_details(cops)
282
- cops.each do |cop|
283
- puts '# Supports --auto-correct' if cop.new.support_autocorrect?
284
- puts "#{cop.cop_name}:"
285
- puts config_lines(cop)
286
- puts
287
- end
288
- end
289
-
290
- def selected_cops_of_department(cops, department)
291
- cops_of_department(cops, department).select do |cop|
292
- @options[:show_cops].include?(cop.cop_name)
293
- end
294
- end
295
-
296
- def cops_of_department(cops, department)
297
- cops.with_department(department).sort!
298
- end
299
-
300
- def config_lines(cop)
301
- cnf = @config_store.for(Dir.pwd).for_cop(cop)
302
- cnf.to_yaml.lines.to_a.drop(1).map { |line| ' ' + line }
303
- end
304
-
305
- def display_warning_summary(warnings)
306
- return if warnings.empty?
307
-
308
- warn Rainbow("\n#{pluralize(warnings.size, 'warning')}:").yellow
309
-
310
- warnings.each { |warning| warn warning }
311
- end
312
-
313
- def display_error_summary(errors)
314
- return if errors.empty?
315
-
316
- warn Rainbow("\n#{pluralize(errors.size, 'error')} occurred:").red
317
-
318
- errors.each { |error| warn error }
319
-
320
- warn <<~WARNING
321
- Errors are usually caused by RuboCop bugs.
322
- Please, report your problems to RuboCop's issue tracker.
323
- #{Gem.loaded_specs['rubocop'].metadata['bug_tracker_uri']}
324
-
325
- Mention the following information in the issue report:
326
- #{RuboCop::Version.version(true)}
327
- WARNING
328
- end
329
-
330
- def maybe_print_corrected_source
331
- # If we are asked to autocorrect source code read from stdin, the only
332
- # reasonable place to write it is to stdout
333
- # Unfortunately, we also write other information to stdout
334
- # So a delimiter is needed for tools to easily identify where the
335
- # autocorrected source begins
336
- return unless @options[:stdin] && @options[:auto_correct]
337
-
338
- puts '=' * 20
339
- print @options[:stdin]
340
125
  end
341
126
  end
342
127
  end
343
- # rubocop:enable Metrics/ClassLength
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ class CLI
5
+ # Home of subcommands in the CLI.
6
+ module Command
7
+ class << self
8
+ # Find the command with a given name and run it in an environment.
9
+ def run(env, name)
10
+ class_for(name).new(env).run
11
+ end
12
+
13
+ private
14
+
15
+ def class_for(name)
16
+ Base.by_command_name(name)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ class CLI
5
+ module Command
6
+ # Generate a configuration file acting as a TODO list.
7
+ class AutoGenerateConfig < Base
8
+ self.command_name = :auto_gen_config
9
+
10
+ PHASE_1 = 'Phase 1 of 2: run Layout/LineLength cop'
11
+ PHASE_2 = 'Phase 2 of 2: run all cops'
12
+
13
+ PHASE_1_OVERRIDDEN =
14
+ '(skipped because the default Layout/LineLength:Max is overridden)'
15
+ PHASE_1_DISABLED =
16
+ '(skipped because Layout/LineLength is disabled)'
17
+
18
+ def run
19
+ add_formatter
20
+ reset_config_and_auto_gen_file
21
+ line_length_contents = maybe_run_line_length_cop
22
+ run_all_cops(line_length_contents)
23
+ end
24
+
25
+ private
26
+
27
+ def maybe_run_line_length_cop
28
+ if !line_length_enabled?(@config_store.for(Dir.pwd))
29
+ skip_line_length_cop(PHASE_1_DISABLED)
30
+ elsif !same_max_line_length?(
31
+ @config_store.for(Dir.pwd), ConfigLoader.default_configuration
32
+ )
33
+ skip_line_length_cop(PHASE_1_OVERRIDDEN)
34
+ else
35
+ run_line_length_cop
36
+ end
37
+ end
38
+
39
+ def skip_line_length_cop(reason)
40
+ puts Rainbow("#{PHASE_1} #{reason}").yellow
41
+ ''
42
+ end
43
+
44
+ def line_length_enabled?(config)
45
+ line_length_cop(config)['Enabled']
46
+ end
47
+
48
+ def same_max_line_length?(config1, config2)
49
+ max_line_length(config1) == max_line_length(config2)
50
+ end
51
+
52
+ def max_line_length(config)
53
+ line_length_cop(config)['Max']
54
+ end
55
+
56
+ def line_length_cop(config)
57
+ config.for_cop('Layout/LineLength')
58
+ end
59
+
60
+ # Do an initial run with only Layout/LineLength so that cops that
61
+ # depend on Layout/LineLength:Max get the correct value for that
62
+ # parameter.
63
+ def run_line_length_cop
64
+ puts Rainbow(PHASE_1).yellow
65
+ @options[:only] = ['Layout/LineLength']
66
+ execute_runner
67
+ @options.delete(:only)
68
+ @config_store = ConfigStore.new
69
+ # Save the todo configuration of the LineLength cop.
70
+ IO.read(ConfigLoader::AUTO_GENERATED_FILE)
71
+ .lines
72
+ .drop_while { |line| line.start_with?('#') }
73
+ .join
74
+ end
75
+
76
+ def run_all_cops(line_length_contents)
77
+ puts Rainbow(PHASE_2).yellow
78
+ result = execute_runner
79
+ # This run was made with the current maximum length allowed, so append
80
+ # the saved setting for LineLength.
81
+ File.open(ConfigLoader::AUTO_GENERATED_FILE, 'a') do |f|
82
+ f.write(line_length_contents)
83
+ end
84
+ result
85
+ end
86
+
87
+ def reset_config_and_auto_gen_file
88
+ @config_store = ConfigStore.new
89
+ @config_store.options_config = @options[:config] if @options[:config]
90
+ File.open(ConfigLoader::AUTO_GENERATED_FILE, 'w') {}
91
+ ConfigLoader.add_inheritance_from_auto_generated_file
92
+ end
93
+
94
+ def add_formatter
95
+ @options[:formatters] << [Formatter::DisabledConfigFormatter,
96
+ ConfigLoader::AUTO_GENERATED_FILE]
97
+ end
98
+
99
+ def execute_runner
100
+ Environment.new(@options, @config_store, @paths).run(:execute_runner)
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ class CLI
5
+ module Command
6
+ # A subcommand in the CLI.
7
+ class Base
8
+ attr_reader :env
9
+
10
+ @subclasses = []
11
+
12
+ class << self
13
+ attr_accessor :command_name
14
+
15
+ def inherited(subclass)
16
+ @subclasses << subclass
17
+ end
18
+
19
+ def by_command_name(name)
20
+ @subclasses.detect { |s| s.command_name == name }
21
+ end
22
+ end
23
+
24
+ def initialize(env)
25
+ @env = env
26
+ @options = env.options
27
+ @config_store = env.config_store
28
+ @paths = env.paths
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end