rubocop 0.75.1 → 0.80.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 (199) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +374 -335
  5. data/lib/rubocop.rb +53 -32
  6. data/lib/rubocop/ast/builder.rb +43 -41
  7. data/lib/rubocop/ast/node.rb +5 -13
  8. data/lib/rubocop/ast/node/block_node.rb +2 -0
  9. data/lib/rubocop/ast/node/def_node.rb +11 -0
  10. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  11. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  12. data/lib/rubocop/ast/node/return_node.rb +24 -0
  13. data/lib/rubocop/ast/traversal.rb +20 -3
  14. data/lib/rubocop/cli.rb +11 -227
  15. data/lib/rubocop/cli/command.rb +21 -0
  16. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  17. data/lib/rubocop/cli/command/base.rb +33 -0
  18. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  19. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  20. data/lib/rubocop/cli/command/show_cops.rb +80 -0
  21. data/lib/rubocop/cli/command/version.rb +17 -0
  22. data/lib/rubocop/cli/environment.rb +21 -0
  23. data/lib/rubocop/comment_config.rb +8 -3
  24. data/lib/rubocop/config.rb +8 -1
  25. data/lib/rubocop/config_loader.rb +20 -20
  26. data/lib/rubocop/config_loader_resolver.rb +2 -1
  27. data/lib/rubocop/config_obsoletion.rb +73 -10
  28. data/lib/rubocop/config_validator.rb +77 -110
  29. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  30. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  31. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  32. data/lib/rubocop/cop/commissioner.rb +15 -7
  33. data/lib/rubocop/cop/cop.rb +31 -6
  34. data/lib/rubocop/cop/corrector.rb +8 -7
  35. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  36. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  37. data/lib/rubocop/cop/generator.rb +3 -4
  38. data/lib/rubocop/cop/generator/configuration_injector.rb +2 -2
  39. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  40. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  41. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  42. data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
  43. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +1 -1
  44. data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
  45. data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
  46. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  47. data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
  48. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  49. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +14 -12
  50. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  51. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
  52. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  53. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +16 -8
  54. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
  55. data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -2
  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 +68 -112
  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_hash_brace_layout.rb +0 -4
  61. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  62. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  63. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
  64. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  65. data/lib/rubocop/cop/layout/space_around_operators.rb +50 -7
  66. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  67. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  68. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
  69. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
  70. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  71. data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
  72. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  73. data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
  74. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  75. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  76. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  77. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  78. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
  79. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  80. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
  81. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  82. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  83. data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +26 -26
  84. data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +18 -15
  85. data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
  86. data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +6 -6
  87. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  88. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  89. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  90. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
  91. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  92. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  93. data/lib/rubocop/cop/lint/useless_setter_call.rb +5 -1
  94. data/lib/rubocop/cop/lint/void.rb +4 -4
  95. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  96. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  97. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
  98. data/lib/rubocop/cop/migration/department_name.rb +30 -2
  99. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  100. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  101. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
  102. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  103. data/lib/rubocop/cop/mixin/hash_transform_method.rb +172 -0
  104. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  105. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  106. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  107. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  108. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -4
  109. data/lib/rubocop/cop/mixin/trailing_comma.rb +16 -18
  110. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  111. data/lib/rubocop/cop/naming/file_name.rb +12 -5
  112. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  113. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  114. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
  115. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  116. data/lib/rubocop/cop/offense.rb +11 -0
  117. data/lib/rubocop/cop/registry.rb +8 -3
  118. data/lib/rubocop/cop/style/alias.rb +1 -1
  119. data/lib/rubocop/cop/style/array_join.rb +1 -1
  120. data/lib/rubocop/cop/style/attr.rb +10 -2
  121. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  122. data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
  123. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  124. data/lib/rubocop/cop/style/copyright.rb +11 -7
  125. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
  126. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
  127. data/lib/rubocop/cop/style/empty_literal.rb +2 -2
  128. data/lib/rubocop/cop/style/empty_method.rb +5 -5
  129. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  130. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  131. data/lib/rubocop/cop/style/format_string.rb +10 -7
  132. data/lib/rubocop/cop/style/format_string_token.rb +2 -0
  133. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +99 -11
  134. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  135. data/lib/rubocop/cop/style/hash_each_methods.rb +87 -0
  136. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  137. data/lib/rubocop/cop/style/hash_transform_keys.rb +79 -0
  138. data/lib/rubocop/cop/style/hash_transform_values.rb +79 -0
  139. data/lib/rubocop/cop/style/if_unless_modifier.rb +45 -3
  140. data/lib/rubocop/cop/style/infinite_loop.rb +5 -4
  141. data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
  142. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  143. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
  144. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
  145. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
  146. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  147. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
  148. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  149. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  150. data/lib/rubocop/cop/style/multiline_when_then.rb +6 -2
  151. data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
  152. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
  153. data/lib/rubocop/cop/style/next.rb +5 -5
  154. data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
  155. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  156. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  157. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  158. data/lib/rubocop/cop/style/or_assignment.rb +3 -2
  159. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  160. data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
  161. data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
  162. data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
  163. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  164. data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
  165. data/lib/rubocop/cop/style/redundant_return.rb +27 -29
  166. data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
  167. data/lib/rubocop/cop/style/safe_navigation.rb +13 -10
  168. data/lib/rubocop/cop/style/semicolon.rb +2 -2
  169. data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
  170. data/lib/rubocop/cop/style/symbol_array.rb +2 -2
  171. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  172. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -22
  173. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  174. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  175. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  176. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  177. data/lib/rubocop/cop/team.rb +5 -0
  178. data/lib/rubocop/cop/util.rb +1 -1
  179. data/lib/rubocop/cop/utils/format_string.rb +10 -18
  180. data/lib/rubocop/cop/variable_force.rb +11 -6
  181. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  182. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -3
  183. data/lib/rubocop/formatter/formatter_set.rb +1 -0
  184. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  185. data/lib/rubocop/formatter/junit_formatter.rb +63 -0
  186. data/lib/rubocop/formatter/tap_formatter.rb +1 -3
  187. data/lib/rubocop/node_pattern.rb +100 -12
  188. data/lib/rubocop/options.rb +17 -11
  189. data/lib/rubocop/processed_source.rb +1 -1
  190. data/lib/rubocop/rake_task.rb +1 -0
  191. data/lib/rubocop/result_cache.rb +24 -8
  192. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  193. data/lib/rubocop/runner.rb +50 -29
  194. data/lib/rubocop/target_finder.rb +12 -6
  195. data/lib/rubocop/target_ruby.rb +151 -0
  196. data/lib/rubocop/version.rb +1 -1
  197. metadata +69 -35
  198. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
  199. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `forward-args` nodes. This will be used in place
6
+ # of a plain node when the builder constructs the AST, making its methods
7
+ # available to all `forward-args` nodes within RuboCop.
8
+ class ForwardArgsNode < Node
9
+ include CollectionNode
10
+
11
+ # Node wraps itself in an array to be compatible with other
12
+ # enumerable argument types.
13
+ def to_a
14
+ [self]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -21,14 +21,12 @@ module RuboCop
21
21
 
22
22
  # @return [RuboCop::AST::Node] a regopt node
23
23
  def regopt
24
- first, second = *self
25
- first.regopt_type? ? first : second
24
+ children.last
26
25
  end
27
26
 
28
27
  # @return [String] a string of regexp content
29
28
  def content
30
- str = children.first
31
- str.str_content || ''
29
+ children.select(&:str_type?).map(&:str_content).join
32
30
  end
33
31
  end
34
32
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `return` nodes. This will be used in place of a
6
+ # plain node when the builder constructs the AST, making its methods
7
+ # available to all `return` nodes within RuboCop.
8
+ class ReturnNode < Node
9
+ include MethodDispatchNode
10
+ include ParameterizedNode
11
+
12
+ # Returns the arguments of the `return`.
13
+ #
14
+ # @return [Array] The arguments of the `return`.
15
+ def arguments
16
+ if node_parts.one? && node_parts.first.begin_type?
17
+ node_parts.first.children
18
+ else
19
+ node_parts
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # rubocop:disable Metrics/ModuleLength
3
4
  module RuboCop
4
5
  module AST
5
6
  # Provides methods for traversing an AST.
@@ -18,15 +19,21 @@ module RuboCop
18
19
  rational str sym regopt self lvar
19
20
  ivar cvar gvar nth_ref back_ref cbase
20
21
  arg restarg blockarg shadowarg
21
- kwrestarg zsuper lambda redo retry].freeze
22
+ kwrestarg zsuper lambda redo retry
23
+ forward_args forwarded_args
24
+ match_var match_nil_pattern].freeze
22
25
  ONE_CHILD_NODE = %i[splat kwsplat block_pass not break next
23
26
  preexe postexe match_current_line defined?
24
- arg_expr].freeze
27
+ arg_expr pin match_rest if_guard unless_guard
28
+ match_with_trailing_comma].freeze
25
29
  MANY_CHILD_NODES = %i[dstr dsym xstr regexp array hash pair
26
30
  mlhs masgn or_asgn and_asgn
27
31
  undef alias args super yield or and
28
32
  while_post until_post iflipflop eflipflop
29
- match_with_lvasgn begin kwbegin return].freeze
33
+ match_with_lvasgn begin kwbegin return
34
+ in_match case_match in_pattern match_alt
35
+ match_as array_pattern array_pattern_with_tail
36
+ hash_pattern const_pattern].freeze
30
37
  SECOND_CHILD_ONLY = %i[lvasgn ivasgn cvasgn gvasgn optarg kwarg
31
38
  kwoptarg].freeze
32
39
 
@@ -178,6 +185,16 @@ module RuboCop
178
185
  alias on_when on_case
179
186
  alias on_irange on_case
180
187
  alias on_erange on_case
188
+
189
+ def on_numblock(node)
190
+ children = node.children
191
+ child = children[0]
192
+ send(:"on_#{child.type}", child)
193
+ return unless (child = children[2])
194
+
195
+ send(:"on_#{child.type}", child)
196
+ end
181
197
  end
182
198
  end
183
199
  end
200
+ # rubocop:enable Metrics/ModuleLength
@@ -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