rubocop 0.74.0 → 0.78.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -2
  3. data/config/default.yml +366 -318
  4. data/lib/rubocop.rb +48 -32
  5. data/lib/rubocop/ast/builder.rb +1 -0
  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/mixin/method_dispatch_node.rb +1 -12
  9. data/lib/rubocop/ast/node/return_node.rb +24 -0
  10. data/lib/rubocop/cli.rb +11 -227
  11. data/lib/rubocop/cli/command.rb +21 -0
  12. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  13. data/lib/rubocop/cli/command/base.rb +33 -0
  14. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  15. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  16. data/lib/rubocop/cli/command/show_cops.rb +73 -0
  17. data/lib/rubocop/cli/command/version.rb +17 -0
  18. data/lib/rubocop/cli/environment.rb +21 -0
  19. data/lib/rubocop/comment_config.rb +5 -4
  20. data/lib/rubocop/config.rb +12 -1
  21. data/lib/rubocop/config_loader.rb +21 -3
  22. data/lib/rubocop/config_loader_resolver.rb +4 -3
  23. data/lib/rubocop/config_obsoletion.rb +85 -11
  24. data/lib/rubocop/config_validator.rb +28 -19
  25. data/lib/rubocop/cop/autocorrect_logic.rb +3 -3
  26. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  27. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  28. data/lib/rubocop/cop/commissioner.rb +15 -7
  29. data/lib/rubocop/cop/cop.rb +35 -9
  30. data/lib/rubocop/cop/corrector.rb +8 -7
  31. data/lib/rubocop/cop/correctors/alignment_corrector.rb +43 -17
  32. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +2 -2
  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/generator.rb +3 -3
  36. data/lib/rubocop/cop/generator/configuration_injector.rb +9 -4
  37. data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
  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} +11 -2
  43. data/lib/rubocop/cop/layout/block_alignment.rb +2 -2
  44. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -1
  45. data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
  46. data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
  47. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +22 -7
  48. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
  49. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
  50. data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
  51. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -7
  52. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +14 -12
  53. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  54. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
  55. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  56. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +8 -4
  57. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +6 -6
  58. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  59. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +41 -114
  60. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  61. data/lib/rubocop/cop/layout/multiline_block_layout.rb +24 -2
  62. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  63. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  64. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
  65. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +5 -1
  66. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  67. data/lib/rubocop/cop/layout/space_around_operators.rb +32 -7
  68. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  69. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
  70. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +8 -5
  71. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +7 -0
  72. data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
  73. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +2 -0
  74. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  75. data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
  76. data/lib/rubocop/cop/lint/assignment_in_condition.rb +17 -4
  77. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  78. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  79. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  80. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  81. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
  82. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +10 -36
  83. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  84. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
  85. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  86. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  87. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  88. data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +26 -26
  89. data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +10 -12
  90. data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
  91. data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +6 -6
  92. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  93. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  94. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  95. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
  96. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +91 -0
  97. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  98. data/lib/rubocop/cop/lint/unused_block_argument.rb +22 -6
  99. data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
  100. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  101. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  102. data/lib/rubocop/cop/lint/void.rb +7 -26
  103. data/lib/rubocop/cop/message_annotator.rb +16 -7
  104. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  105. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  106. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
  107. data/lib/rubocop/cop/migration/department_name.rb +44 -0
  108. data/lib/rubocop/cop/mixin/alignment.rb +2 -2
  109. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
  110. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  111. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  112. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  113. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  114. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  115. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -4
  116. data/lib/rubocop/cop/mixin/trailing_comma.rb +14 -9
  117. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  118. data/lib/rubocop/cop/naming/file_name.rb +12 -5
  119. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  120. data/lib/rubocop/cop/naming/method_name.rb +12 -1
  121. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +3 -3
  122. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  123. data/lib/rubocop/cop/naming/variable_name.rb +1 -0
  124. data/lib/rubocop/cop/offense.rb +29 -7
  125. data/lib/rubocop/cop/registry.rb +22 -1
  126. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
  127. data/lib/rubocop/cop/style/alias.rb +1 -1
  128. data/lib/rubocop/cop/style/array_join.rb +1 -1
  129. data/lib/rubocop/cop/style/attr.rb +10 -2
  130. data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
  131. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +35 -16
  132. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  133. data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
  134. data/lib/rubocop/cop/style/commented_keyword.rb +8 -2
  135. data/lib/rubocop/cop/style/conditional_assignment.rb +6 -6
  136. data/lib/rubocop/cop/style/copyright.rb +11 -7
  137. data/lib/rubocop/cop/style/documentation_method.rb +44 -0
  138. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +10 -4
  139. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
  140. data/lib/rubocop/cop/style/empty_literal.rb +2 -2
  141. data/lib/rubocop/cop/style/empty_method.rb +5 -5
  142. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  143. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  144. data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
  145. data/lib/rubocop/cop/style/format_string.rb +10 -7
  146. data/lib/rubocop/cop/style/format_string_token.rb +19 -68
  147. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +28 -33
  148. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  149. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  150. data/lib/rubocop/cop/style/if_unless_modifier.rb +93 -15
  151. data/lib/rubocop/cop/style/infinite_loop.rb +6 -5
  152. data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
  153. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  154. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
  155. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +25 -25
  156. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
  157. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  158. data/lib/rubocop/cop/style/mixin_usage.rb +11 -1
  159. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  160. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  161. data/lib/rubocop/cop/style/multiline_when_then.rb +1 -1
  162. data/lib/rubocop/cop/style/nested_modifier.rb +22 -4
  163. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
  164. data/lib/rubocop/cop/style/next.rb +5 -5
  165. data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
  166. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  167. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  168. data/lib/rubocop/cop/style/or_assignment.rb +6 -1
  169. data/lib/rubocop/cop/style/parentheses_around_condition.rb +14 -0
  170. data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
  171. data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
  172. data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
  173. data/lib/rubocop/cop/style/redundant_parentheses.rb +16 -7
  174. data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
  175. data/lib/rubocop/cop/style/redundant_return.rb +39 -29
  176. data/lib/rubocop/cop/style/redundant_self.rb +18 -1
  177. data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
  178. data/lib/rubocop/cop/style/rescue_modifier.rb +24 -0
  179. data/lib/rubocop/cop/style/safe_navigation.rb +23 -3
  180. data/lib/rubocop/cop/style/semicolon.rb +13 -2
  181. data/lib/rubocop/cop/style/single_line_methods.rb +8 -1
  182. data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
  183. data/lib/rubocop/cop/style/ternary_parentheses.rb +19 -0
  184. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  185. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  186. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  187. data/lib/rubocop/cop/team.rb +5 -0
  188. data/lib/rubocop/cop/util.rb +1 -1
  189. data/lib/rubocop/cop/utils/format_string.rb +120 -0
  190. data/lib/rubocop/cop/variable_force.rb +7 -5
  191. data/lib/rubocop/cop/variable_force/variable.rb +15 -2
  192. data/lib/rubocop/core_ext/string.rb +0 -24
  193. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  194. data/lib/rubocop/formatter/clang_style_formatter.rb +9 -6
  195. data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -9
  196. data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
  197. data/lib/rubocop/formatter/formatter_set.rb +16 -15
  198. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  199. data/lib/rubocop/formatter/pacman_formatter.rb +80 -0
  200. data/lib/rubocop/formatter/simple_text_formatter.rb +16 -4
  201. data/lib/rubocop/formatter/tap_formatter.rb +18 -7
  202. data/lib/rubocop/magic_comment.rb +4 -0
  203. data/lib/rubocop/node_pattern.rb +4 -2
  204. data/lib/rubocop/options.rb +21 -26
  205. data/lib/rubocop/processed_source.rb +1 -1
  206. data/lib/rubocop/rake_task.rb +1 -0
  207. data/lib/rubocop/result_cache.rb +24 -8
  208. data/lib/rubocop/runner.rb +60 -33
  209. data/lib/rubocop/target_finder.rb +12 -6
  210. data/lib/rubocop/version.rb +1 -1
  211. metadata +48 -33
  212. data/lib/rubocop/cop/mixin/ignored_method_patterns.rb +0 -19
  213. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -22
@@ -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
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ class CLI
5
+ module Command
6
+ # Run all the selected cops and report the result.
7
+ class ExecuteRunner < Base
8
+ include Formatter::TextUtil
9
+
10
+ self.command_name = :execute_runner
11
+
12
+ def run
13
+ execute_runner(@paths)
14
+ end
15
+
16
+ private
17
+
18
+ def execute_runner(paths)
19
+ runner = Runner.new(@options, @config_store)
20
+
21
+ all_passed = runner.run(paths)
22
+ display_warning_summary(runner.warnings)
23
+ display_error_summary(runner.errors)
24
+ maybe_print_corrected_source
25
+
26
+ all_pass_or_excluded = all_passed || @options[:auto_gen_config]
27
+
28
+ if runner.aborting?
29
+ STATUS_INTERRUPTED
30
+ elsif all_pass_or_excluded && runner.errors.empty?
31
+ STATUS_SUCCESS
32
+ else
33
+ STATUS_OFFENSES
34
+ end
35
+ end
36
+
37
+ def display_warning_summary(warnings)
38
+ return if warnings.empty?
39
+
40
+ warn Rainbow("\n#{pluralize(warnings.size, 'warning')}:").yellow
41
+
42
+ warnings.each { |warning| warn warning }
43
+ end
44
+
45
+ def display_error_summary(errors)
46
+ return if errors.empty?
47
+
48
+ warn Rainbow("\n#{pluralize(errors.size, 'error')} occurred:").red
49
+
50
+ errors.each { |error| warn error }
51
+
52
+ warn <<~WARNING
53
+ Errors are usually caused by RuboCop bugs.
54
+ Please, report your problems to RuboCop's issue tracker.
55
+ #{Gem.loaded_specs['rubocop'].metadata['bug_tracker_uri']}
56
+
57
+ Mention the following information in the issue report:
58
+ #{RuboCop::Version.version(true)}
59
+ WARNING
60
+ end
61
+
62
+ def maybe_print_corrected_source
63
+ # If we are asked to autocorrect source code read from stdin, the only
64
+ # reasonable place to write it is to stdout
65
+ # Unfortunately, we also write other information to stdout
66
+ # So a delimiter is needed for tools to easily identify where the
67
+ # autocorrected source begins
68
+ return unless @options[:stdin] && @options[:auto_correct]
69
+
70
+ puts '=' * 20
71
+ print @options[:stdin]
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ class CLI
5
+ module Command
6
+ # Generate a .rubocop.yml file in the current directory.
7
+ class InitDotfile < Base
8
+ DOTFILE = ConfigLoader::DOTFILE
9
+
10
+ self.command_name = :init
11
+
12
+ def run
13
+ path = File.expand_path(DOTFILE)
14
+
15
+ if File.exist?(DOTFILE)
16
+ warn Rainbow("#{DOTFILE} already exists at #{path}").red
17
+
18
+ STATUS_ERROR
19
+ else
20
+ description = <<~DESC
21
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
22
+ # configuration file. It makes it possible to enable/disable
23
+ # certain cops (checks) and to alter their behavior if they accept
24
+ # any parameters. The file can be placed either in your home
25
+ # directory or in some project directory.
26
+ #
27
+ # RuboCop will start looking for the configuration file in the directory
28
+ # where the inspected file is and continue its way up to the root directory.
29
+ #
30
+ # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
31
+ DESC
32
+
33
+ File.open(DOTFILE, 'w') do |f|
34
+ f.write(description)
35
+ end
36
+
37
+ puts "Writing new #{DOTFILE} to #{path}"
38
+
39
+ STATUS_SUCCESS
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ class CLI
5
+ module Command
6
+ # Shows the given cops, or all cops by default, and their configurations
7
+ # for the current directory.
8
+ class ShowCops < Base
9
+ self.command_name = :show_cops
10
+
11
+ def run
12
+ print_available_cops
13
+ end
14
+
15
+ private
16
+
17
+ def print_available_cops
18
+ # Load the configs so the require()s are done for custom cops
19
+ @config_store.for(Dir.pwd)
20
+ registry = Cop::Cop.registry
21
+ show_all = @options[:show_cops].empty?
22
+
23
+ if show_all
24
+ puts "# Available cops (#{registry.length}) " \
25
+ "+ config for #{Dir.pwd}: "
26
+ end
27
+
28
+ registry.departments.sort!.each do |department|
29
+ print_cops_of_department(registry, department, show_all)
30
+ end
31
+ end
32
+
33
+ def print_cops_of_department(registry, department, show_all)
34
+ selected_cops = if show_all
35
+ cops_of_department(registry, department)
36
+ else
37
+ selected_cops_of_department(registry, department)
38
+ end
39
+
40
+ if show_all
41
+ puts "# Department '#{department}' (#{selected_cops.length}):"
42
+ end
43
+
44
+ print_cop_details(selected_cops)
45
+ end
46
+
47
+ def print_cop_details(cops)
48
+ cops.each do |cop|
49
+ puts '# Supports --auto-correct' if cop.new.support_autocorrect?
50
+ puts "#{cop.cop_name}:"
51
+ puts config_lines(cop)
52
+ puts
53
+ end
54
+ end
55
+
56
+ def selected_cops_of_department(cops, department)
57
+ cops_of_department(cops, department).select do |cop|
58
+ @options[:show_cops].include?(cop.cop_name)
59
+ end
60
+ end
61
+
62
+ def cops_of_department(cops, department)
63
+ cops.with_department(department).sort!
64
+ end
65
+
66
+ def config_lines(cop)
67
+ cnf = @config_store.for(Dir.pwd).for_cop(cop)
68
+ cnf.to_yaml.lines.to_a.drop(1).map { |line| ' ' + line }
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ class CLI
5
+ module Command
6
+ # Display version.
7
+ class Version < Base
8
+ self.command_name = :version
9
+
10
+ def run
11
+ puts RuboCop::Version.version(false) if @options[:version]
12
+ puts RuboCop::Version.version(true) if @options[:verbose_version]
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ class CLI
5
+ # Execution environment for a CLI command.
6
+ class Environment
7
+ attr_reader :options, :config_store, :paths
8
+
9
+ def initialize(options, config_store, paths)
10
+ @options = options
11
+ @config_store = config_store
12
+ @paths = paths
13
+ end
14
+
15
+ # Run a command in this environment.
16
+ def run(name)
17
+ Command.run(self, name)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,14 +4,15 @@ module RuboCop
4
4
  # This class parses the special `rubocop:disable` comments in a source
5
5
  # and provides a way to check if each cop is enabled at arbitrary line.
6
6
  class CommentConfig
7
- UNNEEDED_DISABLE = 'Lint/UnneededCopDisableDirective'
7
+ REDUNDANT_DISABLE = 'Lint/RedundantCopDisableDirective'
8
8
 
9
9
  COP_NAME_PATTERN = '([A-Z]\w+/)?(?:[A-Z]\w+)'
10
10
  COP_NAMES_PATTERN = "(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
11
11
  COPS_PATTERN = "(all|#{COP_NAMES_PATTERN})"
12
12
 
13
13
  COMMENT_DIRECTIVE_REGEXP = Regexp.new(
14
- ('# rubocop : ((?:dis|en)able)\b ' + COPS_PATTERN).gsub(' ', '\s*')
14
+ ('# rubocop : ((?:disable|enable|todo))\b ' + COPS_PATTERN)
15
+ .gsub(' ', '\s*')
15
16
  )
16
17
 
17
18
  CopAnalysis = Struct.new(:line_ranges, :start_line_number)
@@ -141,7 +142,7 @@ module RuboCop
141
142
  cop_names =
142
143
  cops_string == 'all' ? all_cop_names : cops_string.split(/,\s*/)
143
144
 
144
- disabled = (switch == 'disable')
145
+ disabled = %w[disable todo].include?(switch)
145
146
 
146
147
  [cop_names, disabled]
147
148
  end
@@ -151,7 +152,7 @@ module RuboCop
151
152
  end
152
153
 
153
154
  def all_cop_names
154
- @all_cop_names ||= Cop::Cop.registry.names - [UNNEEDED_DISABLE]
155
+ @all_cop_names ||= Cop::Cop.registry.names - [REDUNDANT_DISABLE]
155
156
  end
156
157
 
157
158
  def comment_only_line?(line_number)
@@ -53,6 +53,13 @@ module RuboCop
53
53
  @signature ||= Digest::SHA1.hexdigest(to_s)
54
54
  end
55
55
 
56
+ # True if this is a config file that is shipped with RuboCop
57
+ def internal?
58
+ base_config_path = File.expand_path(File.join(ConfigLoader::RUBOCOP_HOME,
59
+ 'config'))
60
+ File.expand_path(loaded_path).start_with?(base_config_path)
61
+ end
62
+
56
63
  def make_excludes_absolute
57
64
  each_key do |key|
58
65
  @validator.validate_section_presence(key)
@@ -96,6 +103,10 @@ module RuboCop
96
103
  @for_cop[cop.respond_to?(:cop_name) ? cop.cop_name : cop]
97
104
  end
98
105
 
106
+ def for_department(department_name)
107
+ @for_cop[department_name]
108
+ end
109
+
99
110
  def for_all_cops
100
111
  @for_all_cops ||= self['AllCops'] || {}
101
112
  end
@@ -104,7 +115,7 @@ module RuboCop
104
115
  relative_file_path = path_relative_to_config(file)
105
116
 
106
117
  # Optimization to quickly decide if the given file is hidden (on the top
107
- # level) and can not be matched by any pattern.
118
+ # level) and cannot be matched by any pattern.
108
119
  is_hidden = relative_file_path.start_with?('.') &&
109
120
  !relative_file_path.start_with?('..')
110
121
  return false if is_hidden && !possibly_include_hidden?