rubocop 1.30.0 → 1.31.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 (143) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/config/default.yml +41 -8
  4. data/config/obsoletion.yml +2 -0
  5. data/exe/rubocop +15 -7
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
  7. data/lib/rubocop/cli/command/suggest_extensions.rb +3 -3
  8. data/lib/rubocop/config.rb +4 -0
  9. data/lib/rubocop/config_loader.rb +1 -0
  10. data/lib/rubocop/config_validator.rb +9 -5
  11. data/lib/rubocop/cop/base.rb +4 -0
  12. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
  13. data/lib/rubocop/cop/bundler/gem_filename.rb +4 -4
  14. data/lib/rubocop/cop/bundler/ordered_gems.rb +2 -2
  15. data/lib/rubocop/cop/corrector.rb +2 -2
  16. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +3 -3
  17. data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +1 -1
  18. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +31 -16
  19. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +1 -1
  20. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  21. data/lib/rubocop/cop/gemspec/require_mfa.rb +20 -20
  22. data/lib/rubocop/cop/generator.rb +1 -1
  23. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -5
  24. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  25. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
  26. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  27. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  28. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +25 -4
  29. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +4 -4
  30. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +9 -9
  31. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +9 -9
  32. data/lib/rubocop/cop/layout/first_argument_indentation.rb +27 -27
  33. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +17 -11
  34. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +49 -10
  35. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  36. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +68 -0
  37. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +120 -0
  38. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  39. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  40. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -2
  41. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  43. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  44. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  45. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +10 -10
  46. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +5 -3
  47. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +7 -7
  48. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  49. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +51 -0
  50. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  51. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +121 -0
  52. data/lib/rubocop/cop/lint/number_conversion.rb +3 -3
  53. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  54. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +5 -5
  55. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +1 -1
  56. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -1
  57. data/lib/rubocop/cop/lint/regexp_as_condition.rb +2 -2
  58. data/lib/rubocop/cop/lint/struct_new_override.rb +2 -2
  59. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
  60. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +44 -0
  61. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  62. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +36 -12
  63. data/lib/rubocop/cop/mixin/range_help.rb +7 -3
  64. data/lib/rubocop/cop/naming/accessor_method_name.rb +3 -1
  65. data/lib/rubocop/cop/naming/variable_number.rb +17 -17
  66. data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
  67. data/lib/rubocop/cop/style/and_or.rb +8 -8
  68. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -1
  69. data/lib/rubocop/cop/style/block_delimiters.rb +4 -2
  70. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  71. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -0
  72. data/lib/rubocop/cop/style/empty_else.rb +10 -10
  73. data/lib/rubocop/cop/style/empty_method.rb +16 -1
  74. data/lib/rubocop/cop/style/encoding.rb +1 -1
  75. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  76. data/lib/rubocop/cop/style/fetch_env_var.rb +9 -2
  77. data/lib/rubocop/cop/style/format_string_token.rb +48 -17
  78. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  79. data/lib/rubocop/cop/style/guard_clause.rb +8 -6
  80. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  81. data/lib/rubocop/cop/style/hash_except.rb +88 -8
  82. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  83. data/lib/rubocop/cop/style/implicit_runtime_error.rb +2 -2
  84. data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
  85. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  86. data/lib/rubocop/cop/style/map_to_hash.rb +1 -1
  87. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -2
  88. data/lib/rubocop/cop/style/missing_else.rb +24 -24
  89. data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -2
  90. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  91. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -3
  92. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  93. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  94. data/lib/rubocop/cop/style/multiline_when_then.rb +1 -3
  95. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
  96. data/lib/rubocop/cop/style/nested_ternary_operator.rb +19 -7
  97. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  98. data/lib/rubocop/cop/style/not.rb +1 -1
  99. data/lib/rubocop/cop/style/redundant_argument.rb +1 -1
  100. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  101. data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
  102. data/lib/rubocop/cop/style/rescue_standard_error.rb +10 -10
  103. data/lib/rubocop/cop/style/safe_navigation.rb +3 -0
  104. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  105. data/lib/rubocop/cop/style/string_concatenation.rb +5 -6
  106. data/lib/rubocop/cop/style/struct_inheritance.rb +2 -2
  107. data/lib/rubocop/cop/style/swap_values.rb +1 -1
  108. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  109. data/lib/rubocop/cop/style/unpack_first.rb +1 -1
  110. data/lib/rubocop/cop/util.rb +1 -1
  111. data/lib/rubocop/cops_documentation_generator.rb +18 -1
  112. data/lib/rubocop/formatter/disabled_config_formatter.rb +8 -5
  113. data/lib/rubocop/formatter/formatter_set.rb +20 -19
  114. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +15 -2
  115. data/lib/rubocop/formatter/html_formatter.rb +0 -1
  116. data/lib/rubocop/formatter/offense_count_formatter.rb +2 -0
  117. data/lib/rubocop/formatter/simple_text_formatter.rb +6 -9
  118. data/lib/rubocop/formatter.rb +31 -0
  119. data/lib/rubocop/options.rb +25 -2
  120. data/lib/rubocop/rake_task.rb +30 -9
  121. data/lib/rubocop/runner.rb +2 -2
  122. data/lib/rubocop/server/cache.rb +109 -0
  123. data/lib/rubocop/server/cli.rb +104 -0
  124. data/lib/rubocop/server/client_command/base.rb +44 -0
  125. data/lib/rubocop/server/client_command/exec.rb +59 -0
  126. data/lib/rubocop/server/client_command/restart.rb +25 -0
  127. data/lib/rubocop/server/client_command/start.rb +43 -0
  128. data/lib/rubocop/server/client_command/status.rb +28 -0
  129. data/lib/rubocop/server/client_command/stop.rb +31 -0
  130. data/lib/rubocop/server/client_command.rb +26 -0
  131. data/lib/rubocop/server/core.rb +79 -0
  132. data/lib/rubocop/server/errors.rb +23 -0
  133. data/lib/rubocop/server/helper.rb +34 -0
  134. data/lib/rubocop/server/server_command/base.rb +50 -0
  135. data/lib/rubocop/server/server_command/exec.rb +34 -0
  136. data/lib/rubocop/server/server_command/stop.rb +24 -0
  137. data/lib/rubocop/server/server_command.rb +21 -0
  138. data/lib/rubocop/server/socket_reader.rb +65 -0
  139. data/lib/rubocop/server.rb +53 -0
  140. data/lib/rubocop/version.rb +15 -8
  141. data/lib/rubocop.rb +7 -26
  142. metadata +27 -4
  143. data/lib/rubocop/cop/gemspec/date_assignment.rb +0 -49
@@ -10,25 +10,25 @@ module RuboCop
10
10
  #
11
11
  # Supported styles are: if, case, both.
12
12
  #
13
- # @example EnforcedStyle: if
14
- # # warn when an `if` expression is missing an `else` branch.
13
+ # @example EnforcedStyle: both (default)
14
+ # # warn when an `if` or `case` expression is missing an `else` branch.
15
15
  #
16
16
  # # bad
17
17
  # if condition
18
18
  # statement
19
19
  # end
20
20
  #
21
- # # good
22
- # if condition
21
+ # # bad
22
+ # case var
23
+ # when condition
23
24
  # statement
24
- # else
25
- # # the content of `else` branch will be determined by Style/EmptyElse
26
25
  # end
27
26
  #
28
27
  # # good
29
- # case var
30
- # when condition
28
+ # if condition
31
29
  # statement
30
+ # else
31
+ # # the content of `else` branch will be determined by Style/EmptyElse
32
32
  # end
33
33
  #
34
34
  # # good
@@ -39,59 +39,59 @@ module RuboCop
39
39
  # # the content of `else` branch will be determined by Style/EmptyElse
40
40
  # end
41
41
  #
42
- # @example EnforcedStyle: case
43
- # # warn when a `case` expression is missing an `else` branch.
42
+ # @example EnforcedStyle: if
43
+ # # warn when an `if` expression is missing an `else` branch.
44
44
  #
45
45
  # # bad
46
- # case var
47
- # when condition
46
+ # if condition
48
47
  # statement
49
48
  # end
50
49
  #
51
50
  # # good
52
- # case var
53
- # when condition
51
+ # if condition
54
52
  # statement
55
53
  # else
56
54
  # # the content of `else` branch will be determined by Style/EmptyElse
57
55
  # end
58
56
  #
59
57
  # # good
60
- # if condition
58
+ # case var
59
+ # when condition
61
60
  # statement
62
61
  # end
63
62
  #
64
63
  # # good
65
- # if condition
64
+ # case var
65
+ # when condition
66
66
  # statement
67
67
  # else
68
68
  # # the content of `else` branch will be determined by Style/EmptyElse
69
69
  # end
70
70
  #
71
- # @example EnforcedStyle: both (default)
72
- # # warn when an `if` or `case` expression is missing an `else` branch.
71
+ # @example EnforcedStyle: case
72
+ # # warn when a `case` expression is missing an `else` branch.
73
73
  #
74
74
  # # bad
75
- # if condition
75
+ # case var
76
+ # when condition
76
77
  # statement
77
78
  # end
78
79
  #
79
- # # bad
80
+ # # good
80
81
  # case var
81
82
  # when condition
82
83
  # statement
84
+ # else
85
+ # # the content of `else` branch will be determined by Style/EmptyElse
83
86
  # end
84
87
  #
85
88
  # # good
86
89
  # if condition
87
90
  # statement
88
- # else
89
- # # the content of `else` branch will be determined by Style/EmptyElse
90
91
  # end
91
92
  #
92
93
  # # good
93
- # case var
94
- # when condition
94
+ # if condition
95
95
  # statement
96
96
  # else
97
97
  # # the content of `else` branch will be determined by Style/EmptyElse
@@ -19,8 +19,8 @@ module RuboCop
19
19
  include Alignment
20
20
  extend AutoCorrector
21
21
 
22
- MSG = 'Favor a normal %<keyword>s-statement over a modifier' \
23
- ' clause in a multiline statement.'
22
+ MSG = 'Favor a normal %<keyword>s-statement over a modifier ' \
23
+ 'clause in a multiline statement.'
24
24
 
25
25
  def on_if(node)
26
26
  return unless node.modifier_form? && node.body.multiline?
@@ -29,7 +29,7 @@ module RuboCop
29
29
  return unless non_modifier_then?(node)
30
30
 
31
31
  add_offense(node.loc.begin, message: format(MSG, keyword: node.keyword)) do |corrector|
32
- corrector.remove(range_with_surrounding_space(range: node.loc.begin, side: :left))
32
+ corrector.remove(range_with_surrounding_space(node.loc.begin, side: :left))
33
33
  end
34
34
  end
35
35
 
@@ -41,9 +41,7 @@ module RuboCop
41
41
 
42
42
  range = node.loc.begin
43
43
  add_offense(range) do |corrector|
44
- corrector.remove(
45
- range_with_surrounding_space(range: range, side: :left, newlines: false)
46
- )
44
+ corrector.remove(range_with_surrounding_space(range, side: :left, newlines: false))
47
45
  end
48
46
  end
49
47
 
@@ -59,7 +59,7 @@ module RuboCop
59
59
  node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
60
60
  )
61
61
 
62
- range_with_surrounding_space(range: range, side: :left)
62
+ range_with_surrounding_space(range, side: :left)
63
63
  end
64
64
 
65
65
  def opening_line(node)
@@ -73,7 +73,7 @@ module RuboCop
73
73
  end
74
74
 
75
75
  def enforce_single_line_ternary_operator?(node)
76
- SINGLE_LINE_TYPES.include?(node.parent.type) && !use_assignment_method?(node.parent)
76
+ SINGLE_LINE_TYPES.include?(node.parent&.type) && !use_assignment_method?(node.parent)
77
77
  end
78
78
 
79
79
  def use_assignment_method?(node)
@@ -39,9 +39,7 @@ module RuboCop
39
39
 
40
40
  range = node.loc.begin
41
41
  add_offense(range) do |corrector|
42
- corrector.remove(
43
- range_with_surrounding_space(range: range, side: :left, newlines: false)
44
- )
42
+ corrector.remove(range_with_surrounding_space(range, side: :left, newlines: false))
45
43
  end
46
44
  end
47
45
 
@@ -44,7 +44,7 @@ module RuboCop
44
44
  last_arg = nested.last_argument.source_range
45
45
 
46
46
  leading_space =
47
- range_with_surrounding_space(range: first_arg.begin,
47
+ range_with_surrounding_space(first_arg.begin,
48
48
  side: :left,
49
49
  whitespace: true,
50
50
  continuations: true)
@@ -17,6 +17,8 @@ module RuboCop
17
17
  # end
18
18
  class NestedTernaryOperator < Base
19
19
  extend AutoCorrector
20
+ include RangeHelp
21
+ include IgnoredNode
20
22
 
21
23
  MSG = 'Ternary operators must not be nested. Prefer `if` or `else` constructs instead.'
22
24
 
@@ -26,14 +28,10 @@ module RuboCop
26
28
  node.each_descendant(:if).select(&:ternary?).each do |nested_ternary|
27
29
  add_offense(nested_ternary) do |corrector|
28
30
  if_node = if_node(nested_ternary)
31
+ next if part_of_ignored_node?(if_node)
29
32
 
30
- corrector.replace(if_node, <<~RUBY.chop)
31
- if #{if_node.condition.source}
32
- #{remove_parentheses(if_node.if_branch.source)}
33
- else
34
- #{if_node.else_branch.source}
35
- end
36
- RUBY
33
+ autocorrect(corrector, if_node)
34
+ ignore_node(if_node)
37
35
  end
38
36
  end
39
37
  end
@@ -47,11 +45,25 @@ module RuboCop
47
45
  if_node(node)
48
46
  end
49
47
 
48
+ def autocorrect(corrector, if_node)
49
+ replace_loc_and_whitespace(corrector, if_node.loc.question, "\n")
50
+ replace_loc_and_whitespace(corrector, if_node.loc.colon, "\nelse\n")
51
+ corrector.replace(if_node.if_branch, remove_parentheses(if_node.if_branch.source))
52
+ corrector.wrap(if_node, 'if ', "\nend")
53
+ end
54
+
50
55
  def remove_parentheses(source)
51
56
  return source unless source.start_with?('(')
52
57
 
53
58
  source.delete_prefix('(').delete_suffix(')')
54
59
  end
60
+
61
+ def replace_loc_and_whitespace(corrector, range, replacement)
62
+ corrector.replace(
63
+ range_with_surrounding_space(range: range, whitespace: true),
64
+ replacement
65
+ )
66
+ end
55
67
  end
56
68
  end
57
69
  end
@@ -57,7 +57,7 @@ module RuboCop
57
57
 
58
58
  def autocorrect(corrector, node)
59
59
  range = if node.single_line?
60
- range_with_surrounding_space(range: node.body.loc.expression)
60
+ range_with_surrounding_space(node.body.loc.expression)
61
61
  else
62
62
  range_by_whole_lines(node.body.loc.expression, include_final_newline: true)
63
63
  end
@@ -33,7 +33,7 @@ module RuboCop
33
33
  return unless node.prefix_not?
34
34
 
35
35
  add_offense(node.loc.selector) do |corrector|
36
- range = range_with_surrounding_space(range: node.loc.selector, side: :right)
36
+ range = range_with_surrounding_space(node.loc.selector, side: :right)
37
37
 
38
38
  if opposite_method?(node.receiver)
39
39
  correct_opposite_method(corrector, range, node.receiver)
@@ -86,7 +86,7 @@ module RuboCop
86
86
  if node.parenthesized?
87
87
  range_between(node.loc.begin.begin_pos, node.loc.end.end_pos)
88
88
  else
89
- range_with_surrounding_space(range: node.first_argument.source_range, newlines: false)
89
+ range_with_surrounding_space(node.first_argument.source_range, newlines: false)
90
90
  end
91
91
  end
92
92
  end
@@ -76,7 +76,7 @@ module RuboCop
76
76
  corrector.replace(first_argument, first_argument.source.delete_prefix('*'))
77
77
  end
78
78
 
79
- keyword = range_with_surrounding_space(range: return_node.loc.keyword, side: :right)
79
+ keyword = range_with_surrounding_space(return_node.loc.keyword, side: :right)
80
80
  corrector.remove(keyword)
81
81
  end
82
82
 
@@ -32,7 +32,7 @@ module RuboCop
32
32
  include RangeHelp
33
33
  extend AutoCorrector
34
34
 
35
- MSG = 'Redundant self assignment detected. '\
35
+ MSG = 'Redundant self assignment detected. ' \
36
36
  'Method `%<method_name>s` modifies its receiver in place.'
37
37
 
38
38
  METHODS_RETURNING_SELF = %i[
@@ -7,21 +7,21 @@ module RuboCop
7
7
  # styles `implicit` and `explicit`. This cop will not register an offense
8
8
  # if any error other than `StandardError` is specified.
9
9
  #
10
- # @example EnforcedStyle: implicit
11
- # # `implicit` will enforce using `rescue` instead of
12
- # # `rescue StandardError`.
10
+ # @example EnforcedStyle: explicit (default)
11
+ # # `explicit` will enforce using `rescue StandardError`
12
+ # # instead of `rescue`.
13
13
  #
14
14
  # # bad
15
15
  # begin
16
16
  # foo
17
- # rescue StandardError
17
+ # rescue
18
18
  # bar
19
19
  # end
20
20
  #
21
21
  # # good
22
22
  # begin
23
23
  # foo
24
- # rescue
24
+ # rescue StandardError
25
25
  # bar
26
26
  # end
27
27
  #
@@ -39,21 +39,21 @@ module RuboCop
39
39
  # bar
40
40
  # end
41
41
  #
42
- # @example EnforcedStyle: explicit (default)
43
- # # `explicit` will enforce using `rescue StandardError`
44
- # # instead of `rescue`.
42
+ # @example EnforcedStyle: implicit
43
+ # # `implicit` will enforce using `rescue` instead of
44
+ # # `rescue StandardError`.
45
45
  #
46
46
  # # bad
47
47
  # begin
48
48
  # foo
49
- # rescue
49
+ # rescue StandardError
50
50
  # bar
51
51
  # end
52
52
  #
53
53
  # # good
54
54
  # begin
55
55
  # foo
56
- # rescue StandardError
56
+ # rescue
57
57
  # bar
58
58
  # end
59
59
  #
@@ -80,11 +80,14 @@ module RuboCop
80
80
  include NilMethods
81
81
  include RangeHelp
82
82
  extend AutoCorrector
83
+ extend TargetRubyVersion
83
84
 
84
85
  MSG = 'Use safe navigation (`&.`) instead of checking if an object ' \
85
86
  'exists before calling the method.'
86
87
  LOGIC_JUMP_KEYWORDS = %i[break fail next raise return throw yield].freeze
87
88
 
89
+ minimum_target_ruby_version 2.3
90
+
88
91
  # if format: (if checked_variable body nil)
89
92
  # unless format: (if checked_variable nil body)
90
93
  # @!method modifier_if_safe_navigation_candidate(node)
@@ -138,7 +138,7 @@ module RuboCop
138
138
  corrector.insert_after(outer_condition, "#{and_operator}#{replace_condition(condition)}")
139
139
 
140
140
  range = range_between(if_branch.loc.keyword.begin_pos, condition.source_range.end_pos)
141
- corrector.remove(range_with_surrounding_space(range: range, newlines: false))
141
+ corrector.remove(range_with_surrounding_space(range, newlines: false))
142
142
  corrector.remove(if_branch.loc.keyword)
143
143
  end
144
144
 
@@ -157,7 +157,7 @@ module RuboCop
157
157
  "#{'!' if node.unless?}#{replace_condition(node.condition)} && ")
158
158
 
159
159
  corrector.remove(node.condition.loc.expression)
160
- corrector.remove(range_with_surrounding_space(range: node.loc.keyword, newlines: false))
160
+ corrector.remove(range_with_surrounding_space(node.loc.keyword, newlines: false))
161
161
  corrector.replace(if_branch.loc.keyword, 'if')
162
162
  end
163
163
 
@@ -76,7 +76,7 @@ module RuboCop
76
76
 
77
77
  topmost_plus_node = find_topmost_plus_node(node)
78
78
  parts = collect_parts(topmost_plus_node)
79
- return unless parts[0..-2].any? { |receiver_node| offensive_for_mode?(receiver_node) }
79
+ return if mode == :conservative && !parts.first.str_type?
80
80
 
81
81
  register_offense(topmost_plus_node, parts)
82
82
  end
@@ -95,11 +95,6 @@ module RuboCop
95
95
  end
96
96
  end
97
97
 
98
- def offensive_for_mode?(receiver_node)
99
- mode = cop_config['Mode'].to_sym
100
- mode == :aggressive || (mode == :conservative && receiver_node.str_type?)
101
- end
102
-
103
98
  def line_end_concatenation?(node)
104
99
  # If the concatenation happens at the end of the line,
105
100
  # and both the receiver and argument are strings, allow
@@ -173,6 +168,10 @@ module RuboCop
173
168
  def single_quoted?(str_node)
174
169
  str_node.source.start_with?("'")
175
170
  end
171
+
172
+ def mode
173
+ cop_config['Mode'].to_sym
174
+ end
176
175
  end
177
176
  end
178
177
  end
@@ -34,7 +34,7 @@ module RuboCop
34
34
  return unless struct_constructor?(node.parent_class)
35
35
 
36
36
  add_offense(node.parent_class.source_range) do |corrector|
37
- corrector.remove(range_with_surrounding_space(range: node.loc.keyword, newlines: false))
37
+ corrector.remove(range_with_surrounding_space(node.loc.keyword, newlines: false))
38
38
  corrector.replace(node.loc.operator, '=')
39
39
 
40
40
  correct_parent(node.parent_class, corrector)
@@ -51,7 +51,7 @@ module RuboCop
51
51
 
52
52
  def correct_parent(parent, corrector)
53
53
  if parent.block_type?
54
- corrector.remove(range_with_surrounding_space(range: parent.loc.end, newlines: false))
54
+ corrector.remove(range_with_surrounding_space(parent.loc.end, newlines: false))
55
55
  elsif (class_node = parent.parent).body.nil?
56
56
  corrector.remove(range_for_empty_class_body(class_node, parent))
57
57
  else
@@ -22,7 +22,7 @@ module RuboCop
22
22
  include RangeHelp
23
23
  extend AutoCorrector
24
24
 
25
- MSG = 'Replace this and assignments at lines %<x_line>d '\
25
+ MSG = 'Replace this and assignments at lines %<x_line>d ' \
26
26
  'and %<y_line>d with `%<replacement>s`.'
27
27
 
28
28
  SIMPLE_ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn casgn].to_set.freeze
@@ -152,7 +152,7 @@ module RuboCop
152
152
 
153
153
  def block_range_with_space(node)
154
154
  block_range = range_between(begin_pos_for_replacement(node), node.loc.end.end_pos)
155
- range_with_surrounding_space(range: block_range, side: :left)
155
+ range_with_surrounding_space(block_range, side: :left)
156
156
  end
157
157
 
158
158
  def begin_pos_for_replacement(node)
@@ -23,7 +23,7 @@ module RuboCop
23
23
 
24
24
  minimum_target_ruby_version 2.4
25
25
 
26
- MSG = 'Use `%<receiver>s.unpack1(%<format>s)` instead of '\
26
+ MSG = 'Use `%<receiver>s.unpack1(%<format>s)` instead of ' \
27
27
  '`%<receiver>s.unpack(%<format>s)%<method>s`.'
28
28
  RESTRICT_ON_SEND = %i[first [] slice at].freeze
29
29
 
@@ -34,7 +34,7 @@ module RuboCop
34
34
  def add_parentheses(node, corrector)
35
35
  if node.args_type?
36
36
  arguments_range = node.source_range
37
- args_with_space = range_with_surrounding_space(range: arguments_range, side: :left)
37
+ args_with_space = range_with_surrounding_space(arguments_range, side: :left)
38
38
  leading_space = range_between(args_with_space.begin_pos, arguments_range.begin_pos)
39
39
  corrector.replace(leading_space, '(')
40
40
  corrector.insert_after(arguments_range, ')')
@@ -34,17 +34,34 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
34
34
  end
35
35
 
36
36
  def cops_body(cop, description, examples_objects, safety_objects, pars) # rubocop:disable Metrics/AbcSize
37
+ check_examples_to_have_the_default_enforced_style!(examples_objects, cop)
38
+
37
39
  content = h2(cop.cop_name)
38
40
  content << required_ruby_version(cop)
39
41
  content << properties(cop)
40
42
  content << "#{description}\n"
41
43
  content << safety_object(safety_objects) if safety_objects.any? { |s| !s.text.blank? }
42
- content << examples(examples_objects) if examples_objects.count.positive?
44
+ content << examples(examples_objects) if examples_objects.any?
43
45
  content << configurations(pars)
44
46
  content << references(cop)
45
47
  content
46
48
  end
47
49
 
50
+ def check_examples_to_have_the_default_enforced_style!(examples_object, cop)
51
+ return if examples_object.none?
52
+
53
+ examples_describing_enforced_style = examples_object.map(&:name).grep(/EnforcedStyle:/)
54
+ return if examples_describing_enforced_style.none?
55
+
56
+ if examples_describing_enforced_style.index { |name| name.match?('default') }.nonzero?
57
+ raise "Put the example with the default EnforcedStyle on top for #{cop.cop_name}"
58
+ end
59
+
60
+ return if examples_describing_enforced_style.any? { |name| name.match?('default') }
61
+
62
+ raise "Specify the default EnforcedStyle for #{cop.cop_name}"
63
+ end
64
+
48
65
  def examples(examples_object)
49
66
  examples_object.each_with_object(h3('Examples').dup) do |example, content|
50
67
  content << "\n" unless content.end_with?("\n\n")
@@ -123,7 +123,7 @@ module RuboCop
123
123
  cop_class = Cop::Registry.global.find_by_cop_name(cop_name)
124
124
  default_cfg = default_config(cop_name)
125
125
 
126
- if supports_safe_auto_correct?(cop_class, default_cfg)
126
+ if supports_safe_autocorrect?(cop_class, default_cfg)
127
127
  output_buffer.puts '# This cop supports safe autocorrection (--autocorrect).'
128
128
  elsif supports_unsafe_autocorrect?(cop_class, default_cfg)
129
129
  output_buffer.puts '# This cop supports unsafe autocorrection (--autocorrect-all).'
@@ -137,13 +137,12 @@ module RuboCop
137
137
  output_cop_param_comments(output_buffer, params, default_cfg)
138
138
  end
139
139
 
140
- def supports_safe_auto_correct?(cop_class, default_cfg)
141
- cop_class&.support_autocorrect? &&
142
- (default_cfg.nil? || default_cfg['Safe'] || default_cfg['Safe'].nil?)
140
+ def supports_safe_autocorrect?(cop_class, default_cfg)
141
+ cop_class&.support_autocorrect? && (default_cfg.nil? || safe_autocorrect?(default_cfg))
143
142
  end
144
143
 
145
144
  def supports_unsafe_autocorrect?(cop_class, default_cfg)
146
- cop_class&.support_autocorrect? && !default_cfg.nil? && default_cfg['Safe'] == false
145
+ cop_class&.support_autocorrect? && !safe_autocorrect?(default_cfg)
147
146
  end
148
147
 
149
148
  def cop_config_params(default_cfg, cfg)
@@ -242,6 +241,10 @@ module RuboCop
242
241
  regexp = exclude_path
243
242
  output_buffer.puts " - !ruby/regexp /#{regexp.source}/"
244
243
  end
244
+
245
+ def safe_autocorrect?(config)
246
+ config.fetch('Safe', true) && config.fetch('SafeAutoCorrect', true)
247
+ end
245
248
  end
246
249
  end
247
250
  end
@@ -9,23 +9,23 @@ module RuboCop
9
9
  # which invoke same method of each formatters.
10
10
  class FormatterSet < Array
11
11
  BUILTIN_FORMATTERS_FOR_KEYS = {
12
- '[a]utogenconf' => AutoGenConfigFormatter,
13
- '[c]lang' => ClangStyleFormatter,
14
- '[e]macs' => EmacsStyleFormatter,
15
- '[fi]les' => FileListFormatter,
16
- '[fu]ubar' => FuubarStyleFormatter,
17
- '[g]ithub' => GitHubActionsFormatter,
18
- '[h]tml' => HTMLFormatter,
19
- '[j]son' => JSONFormatter,
20
- '[ju]nit' => JUnitFormatter,
21
- '[m]arkdown' => MarkdownFormatter,
22
- '[o]ffenses' => OffenseCountFormatter,
23
- '[pa]cman' => PacmanFormatter,
24
- '[p]rogress' => ProgressFormatter,
25
- '[q]uiet' => QuietFormatter,
26
- '[s]imple' => SimpleTextFormatter,
27
- '[t]ap' => TapFormatter,
28
- '[w]orst' => WorstOffendersFormatter
12
+ '[a]utogenconf' => 'AutoGenConfigFormatter',
13
+ '[c]lang' => 'ClangStyleFormatter',
14
+ '[e]macs' => 'EmacsStyleFormatter',
15
+ '[fi]les' => 'FileListFormatter',
16
+ '[fu]ubar' => 'FuubarStyleFormatter',
17
+ '[g]ithub' => 'GitHubActionsFormatter',
18
+ '[h]tml' => 'HTMLFormatter',
19
+ '[j]son' => 'JSONFormatter',
20
+ '[ju]nit' => 'JUnitFormatter',
21
+ '[m]arkdown' => 'MarkdownFormatter',
22
+ '[o]ffenses' => 'OffenseCountFormatter',
23
+ '[pa]cman' => 'PacmanFormatter',
24
+ '[p]rogress' => 'ProgressFormatter',
25
+ '[q]uiet' => 'QuietFormatter',
26
+ '[s]imple' => 'SimpleTextFormatter',
27
+ '[t]ap' => 'TapFormatter',
28
+ '[w]orst' => 'WorstOffendersFormatter'
29
29
  }.freeze
30
30
 
31
31
  FORMATTER_APIS = %i[started finished].freeze
@@ -55,7 +55,7 @@ module RuboCop
55
55
  def add_formatter(formatter_type, output_path = nil)
56
56
  if output_path
57
57
  dir_path = File.dirname(output_path)
58
- FileUtils.mkdir_p(dir_path) unless File.exist?(dir_path)
58
+ FileUtils.mkdir_p(dir_path)
59
59
  output = File.open(output_path, 'w')
60
60
  else
61
61
  output = $stdout
@@ -92,7 +92,8 @@ module RuboCop
92
92
 
93
93
  raise %(Cannot determine formatter for "#{specified_key}") if matching_keys.size > 1
94
94
 
95
- BUILTIN_FORMATTERS_FOR_KEYS[matching_keys.first]
95
+ formatter_name = BUILTIN_FORMATTERS_FOR_KEYS[matching_keys.first]
96
+ RuboCop::Formatter.const_get(formatter_name)
96
97
  end
97
98
 
98
99
  def custom_formatter_class(specified_class_name)
@@ -7,8 +7,21 @@ module RuboCop
7
7
  class GitHubActionsFormatter < BaseFormatter
8
8
  ESCAPE_MAP = { '%' => '%25', "\n" => '%0A', "\r" => '%0D' }.freeze
9
9
 
10
+ def started(_target_files)
11
+ @offenses_for_files = {}
12
+ end
13
+
10
14
  def file_finished(file, offenses)
11
- offenses.each { |offense| report_offense(file, offense) }
15
+ @offenses_for_files[file] = offenses unless offenses.empty?
16
+ end
17
+
18
+ def finished(_inspected_files)
19
+ @offenses_for_files.each do |file, offenses|
20
+ offenses.each do |offense|
21
+ report_offense(file, offense)
22
+ end
23
+ end
24
+ output.puts
12
25
  end
13
26
 
14
27
  private
@@ -31,7 +44,7 @@ module RuboCop
31
44
 
32
45
  def report_offense(file, offense)
33
46
  output.printf(
34
- "\n::%<severity>s file=%<file>s,line=%<line>d,col=%<column>d::%<message>s\n",
47
+ "\n::%<severity>s file=%<file>s,line=%<line>d,col=%<column>d::%<message>s",
35
48
  severity: github_severity(offense),
36
49
  file: PathUtil.smart_path(file),
37
50
  line: offense.line,
@@ -4,7 +4,6 @@ require 'cgi'
4
4
  require 'erb'
5
5
  require 'ostruct'
6
6
  require 'base64'
7
- require_relative 'text_util'
8
7
 
9
8
  module RuboCop
10
9
  module Formatter
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ruby-progressbar'
4
+
3
5
  module RuboCop
4
6
  module Formatter
5
7
  # This formatter displays the list of offended cops with a count of how