rubocop 1.6.1 → 1.9.1

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 (120) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -3
  4. data/config/default.yml +145 -19
  5. data/lib/rubocop.rb +16 -1
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +5 -4
  7. data/lib/rubocop/comment_config.rb +6 -6
  8. data/lib/rubocop/config.rb +13 -7
  9. data/lib/rubocop/config_loader.rb +11 -14
  10. data/lib/rubocop/config_loader_resolver.rb +21 -4
  11. data/lib/rubocop/config_obsoletion.rb +5 -3
  12. data/lib/rubocop/config_store.rb +12 -1
  13. data/lib/rubocop/cop/base.rb +2 -1
  14. data/lib/rubocop/cop/exclude_limit.rb +26 -0
  15. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +3 -2
  16. data/lib/rubocop/cop/generator.rb +1 -3
  17. data/lib/rubocop/cop/internal_affairs.rb +6 -1
  18. data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +68 -0
  19. data/lib/rubocop/cop/internal_affairs/example_description.rb +89 -0
  20. data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +61 -0
  21. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +64 -0
  22. data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +145 -0
  23. data/lib/rubocop/cop/layout/class_structure.rb +7 -2
  24. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +56 -20
  25. data/lib/rubocop/cop/layout/first_argument_indentation.rb +16 -2
  26. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +14 -0
  27. data/lib/rubocop/cop/layout/line_length.rb +2 -1
  28. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -2
  29. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -10
  30. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +1 -0
  31. data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
  32. data/lib/rubocop/cop/layout/space_before_brackets.rb +67 -0
  33. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +13 -10
  34. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  35. data/lib/rubocop/cop/lint/ambiguous_assignment.rb +59 -0
  36. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +7 -2
  37. data/lib/rubocop/cop/lint/deprecated_constants.rb +80 -0
  38. data/lib/rubocop/cop/lint/duplicate_branch.rb +64 -2
  39. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +44 -0
  40. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +10 -6
  41. data/lib/rubocop/cop/lint/number_conversion.rb +41 -6
  42. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +47 -0
  43. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +39 -0
  44. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +2 -1
  45. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +50 -0
  46. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +50 -17
  47. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -11
  48. data/lib/rubocop/cop/lint/symbol_conversion.rb +103 -0
  49. data/lib/rubocop/cop/lint/triple_quotes.rb +71 -0
  50. data/lib/rubocop/cop/lint/unreachable_loop.rb +17 -0
  51. data/lib/rubocop/cop/message_annotator.rb +4 -1
  52. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
  53. data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
  54. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  55. data/lib/rubocop/cop/mixin/allowed_identifiers.rb +18 -0
  56. data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -0
  57. data/lib/rubocop/cop/mixin/code_length.rb +3 -1
  58. data/lib/rubocop/cop/mixin/comments_help.rb +1 -11
  59. data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
  60. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  61. data/lib/rubocop/cop/mixin/method_complexity.rb +3 -1
  62. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +5 -1
  63. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +59 -5
  64. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +38 -5
  65. data/lib/rubocop/cop/naming/variable_name.rb +2 -0
  66. data/lib/rubocop/cop/naming/variable_number.rb +2 -9
  67. data/lib/rubocop/cop/registry.rb +10 -0
  68. data/lib/rubocop/cop/severity.rb +3 -3
  69. data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -1
  70. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  71. data/lib/rubocop/cop/style/collection_methods.rb +14 -1
  72. data/lib/rubocop/cop/style/commented_keyword.rb +22 -5
  73. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -9
  74. data/lib/rubocop/cop/style/empty_literal.rb +6 -2
  75. data/lib/rubocop/cop/style/endless_method.rb +102 -0
  76. data/lib/rubocop/cop/style/eval_with_location.rb +63 -34
  77. data/lib/rubocop/cop/style/explicit_block_argument.rb +10 -0
  78. data/lib/rubocop/cop/style/float_division.rb +3 -0
  79. data/lib/rubocop/cop/style/for.rb +2 -0
  80. data/lib/rubocop/cop/style/format_string_token.rb +18 -2
  81. data/lib/rubocop/cop/style/hash_except.rb +95 -0
  82. data/lib/rubocop/cop/style/hash_like_case.rb +2 -1
  83. data/lib/rubocop/cop/style/if_inside_else.rb +22 -10
  84. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +120 -0
  85. data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -2
  86. data/lib/rubocop/cop/style/lambda_call.rb +2 -1
  87. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -0
  88. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +16 -6
  89. data/lib/rubocop/cop/style/method_def_parentheses.rb +7 -0
  90. data/lib/rubocop/cop/style/multiline_method_signature.rb +26 -1
  91. data/lib/rubocop/cop/style/multiline_when_then.rb +3 -1
  92. data/lib/rubocop/cop/style/mutable_constant.rb +13 -3
  93. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -0
  94. data/lib/rubocop/cop/style/nil_comparison.rb +3 -0
  95. data/lib/rubocop/cop/style/non_nil_check.rb +23 -13
  96. data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
  97. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  98. data/lib/rubocop/cop/style/raise_args.rb +5 -2
  99. data/lib/rubocop/cop/style/redundant_argument.rb +7 -1
  100. data/lib/rubocop/cop/style/redundant_freeze.rb +8 -4
  101. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  102. data/lib/rubocop/cop/style/single_line_methods.rb +36 -2
  103. data/lib/rubocop/cop/style/sole_nested_conditional.rb +29 -5
  104. data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
  105. data/lib/rubocop/cop/style/symbol_proc.rb +5 -4
  106. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  107. data/lib/rubocop/cop/style/while_until_modifier.rb +2 -4
  108. data/lib/rubocop/cop/util.rb +3 -1
  109. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -0
  110. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
  111. data/lib/rubocop/magic_comment.rb +30 -1
  112. data/lib/rubocop/options.rb +10 -10
  113. data/lib/rubocop/rspec/cop_helper.rb +0 -4
  114. data/lib/rubocop/rspec/expect_offense.rb +37 -22
  115. data/lib/rubocop/runner.rb +17 -1
  116. data/lib/rubocop/target_finder.rb +4 -2
  117. data/lib/rubocop/target_ruby.rb +47 -11
  118. data/lib/rubocop/util.rb +16 -0
  119. data/lib/rubocop/version.rb +8 -2
  120. metadata +27 -7
@@ -3,7 +3,9 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # This cop check for uses of Object#freeze on immutable objects.
6
+ # This cop check for uses of `Object#freeze` on immutable objects.
7
+ #
8
+ # NOTE: Regexp and Range literals are frozen objects since Ruby 3.0.
7
9
  #
8
10
  # @example
9
11
  # # bad
@@ -37,8 +39,10 @@ module RuboCop
37
39
 
38
40
  return true if node.immutable_literal?
39
41
 
40
- FROZEN_STRING_LITERAL_TYPES.include?(node.type) &&
41
- frozen_string_literals_enabled?
42
+ return true if FROZEN_STRING_LITERAL_TYPES.include?(node.type) &&
43
+ frozen_string_literals_enabled?
44
+
45
+ target_ruby_version >= 3.0 && (node.regexp_type? || node.range_type?)
42
46
  end
43
47
 
44
48
  def strip_parenthesis(node)
@@ -52,7 +56,7 @@ module RuboCop
52
56
  def_node_matcher :operation_produces_immutable_object?, <<~PATTERN
53
57
  {
54
58
  (begin (send {float int} {:+ :- :* :** :/ :% :<<} _))
55
- (begin (send !(str _) {:+ :- :* :** :/ :%} {float int}))
59
+ (begin (send !{(str _) array} {:+ :- :* :** :/ :%} {float int}))
56
60
  (begin (send _ {:== :=== :!= :<= :>= :< :>} _))
57
61
  (send (const {nil? cbase} :ENV) :[] _)
58
62
  (send _ {:count :length :size} ...)
@@ -66,7 +66,7 @@ module RuboCop
66
66
  end
67
67
 
68
68
  def correct_with_arguments(return_node, corrector)
69
- if return_node.arguments.size > 1
69
+ if return_node.children.size > 1
70
70
  add_brackets(corrector, return_node)
71
71
  elsif hash_without_braces?(return_node.first_argument)
72
72
  add_braces(corrector, return_node.first_argument)
@@ -6,6 +6,12 @@ module RuboCop
6
6
  # This cop checks for single-line method definitions that contain a body.
7
7
  # It will accept single-line methods with no body.
8
8
  #
9
+ # Endless methods added in Ruby 3.0 are also accepted by this cop.
10
+ #
11
+ # If `Style/EndlessMethod` is enabled with `EnforcedStyle: allow_single_line` or
12
+ # `allow_always`, single-line methods will be auto-corrected to endless
13
+ # methods if there is only one statement in the body.
14
+ #
9
15
  # @example
10
16
  # # bad
11
17
  # def some_method; body end
@@ -15,6 +21,7 @@ module RuboCop
15
21
  # # good
16
22
  # def self.resource_class=(klass); end
17
23
  # def @table.columns; end
24
+ # def some_method() = body
18
25
  #
19
26
  # @example AllowIfMethodIsEmpty: true (default)
20
27
  # # good
@@ -32,6 +39,7 @@ module RuboCop
32
39
 
33
40
  def on_def(node)
34
41
  return unless node.single_line?
42
+ return if node.endless?
35
43
  return if allow_empty? && !node.body
36
44
 
37
45
  add_offense(node) do |corrector|
@@ -43,6 +51,30 @@ module RuboCop
43
51
  private
44
52
 
45
53
  def autocorrect(corrector, node)
54
+ if correct_to_endless?(node.body)
55
+ correct_to_endless(corrector, node)
56
+ else
57
+ correct_to_multiline(corrector, node)
58
+ end
59
+ end
60
+
61
+ def allow_empty?
62
+ cop_config['AllowIfMethodIsEmpty']
63
+ end
64
+
65
+ def correct_to_endless?(body_node)
66
+ return false if target_ruby_version < 3.0
67
+
68
+ endless_method_config = config.for_cop('Style/EndlessMethod')
69
+
70
+ return false unless endless_method_config['Enabled']
71
+ return false if endless_method_config['EnforcedStyle'] == 'disallow'
72
+ return false unless body_node
73
+
74
+ !(body_node.begin_type? || body_node.kwbegin_type?)
75
+ end
76
+
77
+ def correct_to_multiline(corrector, node)
46
78
  each_part(node.body) do |part|
47
79
  LineBreakCorrector.break_line_before(
48
80
  range: part, node: node, corrector: corrector,
@@ -58,8 +90,10 @@ module RuboCop
58
90
  move_comment(node, corrector)
59
91
  end
60
92
 
61
- def allow_empty?
62
- cop_config['AllowIfMethodIsEmpty']
93
+ def correct_to_endless(corrector, node)
94
+ arguments = node.arguments.any? ? node.arguments.source : '()'
95
+ replacement = "def #{node.method_name}#{arguments} = #{node.body.source}"
96
+ corrector.replace(node, replacement)
63
97
  end
64
98
 
65
99
  def each_part(body)
@@ -63,13 +63,13 @@ module RuboCop
63
63
  end
64
64
 
65
65
  def autocorrect(corrector, node, if_branch)
66
+ corrector.wrap(node.condition, '(', ')') if node.condition.or_type?
67
+
66
68
  if node.unless?
67
69
  corrector.replace(node.loc.keyword, 'if')
68
70
  corrector.insert_before(node.condition, '!')
69
71
  end
70
72
 
71
- corrector.wrap(node.condition, '(', ')') if node.condition.or_type?
72
-
73
73
  and_operator = if_branch.unless? ? ' && !' : ' && '
74
74
  if if_branch.modifier_form?
75
75
  correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
@@ -80,8 +80,11 @@ module RuboCop
80
80
  end
81
81
 
82
82
  def correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
83
+ outer_condition = node.condition
84
+ correct_outer_condition(corrector, outer_condition)
85
+
83
86
  condition = if_branch.condition
84
- corrector.insert_after(node.condition, replacement_condition(and_operator, condition))
87
+ corrector.insert_after(outer_condition, replacement_condition(and_operator, condition))
85
88
 
86
89
  range = range_between(if_branch.loc.keyword.begin_pos, condition.source_range.end_pos)
87
90
  corrector.remove(range_with_surrounding_space(range: range, newlines: false))
@@ -100,14 +103,35 @@ module RuboCop
100
103
  def correct_for_comment(corrector, node, if_branch)
101
104
  return if config.for_cop('Style/IfUnlessModifier')['Enabled']
102
105
 
103
- comments = processed_source.comments_before_line(if_branch.source_range.line)
106
+ comments = processed_source.ast_with_comments[if_branch]
104
107
  comment_text = comments.map(&:text).join("\n") << "\n"
105
108
 
106
109
  corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
107
110
  end
108
111
 
112
+ def correct_outer_condition(corrector, condition)
113
+ return unless requrie_parentheses?(condition)
114
+
115
+ end_pos = condition.loc.selector.end_pos
116
+ begin_pos = condition.first_argument.source_range.begin_pos
117
+ return if end_pos > begin_pos
118
+
119
+ corrector.replace(range_between(end_pos, begin_pos), '(')
120
+ corrector.insert_after(condition.last_argument.source_range, ')')
121
+ end
122
+
123
+ def requrie_parentheses?(condition)
124
+ condition.send_type? && !condition.arguments.empty? && !condition.parenthesized?
125
+ end
126
+
127
+ def arguments_range(node)
128
+ range_between(
129
+ node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
130
+ )
131
+ end
132
+
109
133
  def wrap_condition?(node)
110
- node.or_type? ||
134
+ node.and_type? || node.or_type? ||
111
135
  (node.send_type? && node.arguments.any? && !node.parenthesized?)
112
136
  end
113
137
 
@@ -116,7 +116,7 @@ module RuboCop
116
116
  parts.map do |part|
117
117
  if part.str_type?
118
118
  if single_quoted?(part)
119
- part.value.gsub('\\') { '\\\\' }
119
+ part.value.gsub(/(\\|")/, '\\\\\&')
120
120
  else
121
121
  part.value.inspect[1..-2]
122
122
  end
@@ -22,11 +22,12 @@ module RuboCop
22
22
  SUPER_TYPES = %i[super zsuper].freeze
23
23
 
24
24
  def_node_matcher :proc_node?, '(send (const {nil? cbase} :Proc) :new)'
25
+ def_node_matcher :symbol_proc_receiver?, '{(send ...) (super ...) zsuper}'
25
26
  def_node_matcher :symbol_proc?, <<~PATTERN
26
- ({block numblock}
27
- ${(send ...) (super ...) zsuper}
28
- ${(args (arg _)) %Integer}
29
- (send (lvar _var) $_))
27
+ {
28
+ (block $#symbol_proc_receiver? $(args (arg _var)) (send (lvar _var) $_))
29
+ (numblock $#symbol_proc_receiver? $1 (send (lvar :_1) $_))
30
+ }
30
31
  PATTERN
31
32
 
32
33
  def self.autocorrect_incompatible_with
@@ -120,7 +120,7 @@ module RuboCop
120
120
  if condition.begin_type?
121
121
  condition.to_a.any? { |x| complex_condition?(x) }
122
122
  else
123
- non_complex_expression?(condition) ? false : true
123
+ !non_complex_expression?(condition)
124
124
  end
125
125
  end
126
126
 
@@ -41,12 +41,10 @@ module RuboCop
41
41
  'having a single-line body.'
42
42
 
43
43
  def on_while(node)
44
- return unless node.multiline? && single_line_as_modifier?(node)
44
+ return unless single_line_as_modifier?(node)
45
45
 
46
46
  add_offense(node.loc.keyword, message: format(MSG, keyword: node.keyword)) do |corrector|
47
- oneline = "#{node.body.source} #{node.keyword} #{node.condition.source}"
48
-
49
- corrector.replace(node, oneline)
47
+ corrector.replace(node, to_modifier_form(node))
50
48
  end
51
49
  end
52
50
  alias on_until on_while
@@ -33,7 +33,9 @@ module RuboCop
33
33
  end
34
34
 
35
35
  def add_parentheses(node, corrector)
36
- if node.arguments.empty?
36
+ if !node.respond_to?(:arguments)
37
+ corrector.wrap(node, '(', ')')
38
+ elsif node.arguments.empty?
37
39
  corrector.insert_after(node, '()')
38
40
  else
39
41
  corrector.replace(args_begin(node), '(')
@@ -23,6 +23,7 @@ module RuboCop
23
23
 
24
24
  def minimum_severity_to_fail
25
25
  @minimum_severity_to_fail ||= begin
26
+ # Unless given explicitly as `fail_level`, `:info` severity offenses do not fail
26
27
  name = options.fetch(:fail_level, :refactor)
27
28
  RuboCop::Cop::Severity.new(name)
28
29
  end
@@ -13,6 +13,7 @@ module RuboCop
13
13
  include PathUtil
14
14
 
15
15
  COLOR_FOR_SEVERITY = {
16
+ info: :gray,
16
17
  refactor: :yellow,
17
18
  convention: :yellow,
18
19
  warning: :magenta,
@@ -76,7 +77,7 @@ module RuboCop
76
77
  end
77
78
 
78
79
  def colored_severity_code(offense)
79
- color = COLOR_FOR_SEVERITY[offense.severity.name]
80
+ color = COLOR_FOR_SEVERITY.fetch(offense.severity.name)
80
81
  colorize(offense.severity.code, color)
81
82
  end
82
83
 
@@ -27,7 +27,7 @@ module RuboCop
27
27
  end
28
28
 
29
29
  def any?
30
- frozen_string_literal_specified? || encoding_specified?
30
+ frozen_string_literal_specified? || encoding_specified? || shareable_constant_value_specified?
31
31
  end
32
32
 
33
33
  # Does the magic comment enable the frozen string literal feature.
@@ -46,6 +46,10 @@ module RuboCop
46
46
  [true, false].include?(frozen_string_literal)
47
47
  end
48
48
 
49
+ def valid_shareable_constant_value?
50
+ %w[none literal experimental_everything experimental_copy].include?(shareable_constant_values)
51
+ end
52
+
49
53
  # Was a magic comment for the frozen string literal found?
50
54
  #
51
55
  # @return [Boolean]
@@ -53,6 +57,13 @@ module RuboCop
53
57
  specified?(frozen_string_literal)
54
58
  end
55
59
 
60
+ # Was a shareable_constant_value specified?
61
+ #
62
+ # @return [Boolean]
63
+ def shareable_constant_value_specified?
64
+ specified?(shareable_constant_value)
65
+ end
66
+
56
67
  # Expose the `frozen_string_literal` value coerced to a boolean if possible.
57
68
  #
58
69
  # @return [Boolean] if value is `true` or `false`
@@ -69,6 +80,13 @@ module RuboCop
69
80
  end
70
81
  end
71
82
 
83
+ # Expose the `shareable_constant_value` value coerced to a boolean if possible.
84
+ #
85
+ # @return [String] for shareable_constant_value config
86
+ def shareable_constant_value
87
+ extract_shareable_constant_value
88
+ end
89
+
72
90
  def encoding_specified?
73
91
  specified?(encoding)
74
92
  end
@@ -146,6 +164,10 @@ module RuboCop
146
164
  def extract_frozen_string_literal
147
165
  match('frozen[_-]string[_-]literal')
148
166
  end
167
+
168
+ def extract_shareable_constant_value
169
+ match('shareable[_-]constant[_-]values')
170
+ end
149
171
  end
150
172
 
151
173
  # Wrapper for Vim style magic comments.
@@ -176,6 +198,9 @@ module RuboCop
176
198
 
177
199
  # Vim comments cannot specify frozen string literal behavior.
178
200
  def frozen_string_literal; end
201
+
202
+ # Vim comments cannot specify shareable constant values behavior.
203
+ def shareable_constant_value; end
179
204
  end
180
205
 
181
206
  # Wrapper for regular magic comments not bound to an editor.
@@ -209,6 +234,10 @@ module RuboCop
209
234
  def extract_frozen_string_literal
210
235
  extract(/\A\s*#\s*frozen[_-]string[_-]literal:\s*(#{TOKEN})\s*\z/io)
211
236
  end
237
+
238
+ def extract_shareable_constant_value
239
+ extract(/\A\s*#\s*shareable[_-]constant[_-]value:\s*(#{TOKEN})\s*\z/io)
240
+ end
212
241
  end
213
242
  end
214
243
  end
@@ -66,7 +66,7 @@ module RuboCop
66
66
  add_configuration_options(opts)
67
67
  add_formatting_options(opts)
68
68
 
69
- option(opts, '-r', '--require FILE') { |f| require f }
69
+ option(opts, '-r', '--require FILE') { |f| require_feature(f) }
70
70
 
71
71
  add_severity_option(opts)
72
72
  add_flags_with_optional_args(opts)
@@ -92,14 +92,7 @@ module RuboCop
92
92
  raise OptionArgumentError, message
93
93
  end
94
94
 
95
- @options[:"#{option}"] =
96
- if list.empty?
97
- ['']
98
- else
99
- list.split(',').map do |c|
100
- Cop::Registry.qualified_cop_name(c, "--#{option} option")
101
- end
102
- end
95
+ @options[:"#{option}"] = list.empty? ? [''] : list.split(',')
103
96
  end
104
97
  end
105
98
 
@@ -239,6 +232,13 @@ module RuboCop
239
232
  long_opt[2..-1].sub('[no-]', '').sub(/ .*/, '')
240
233
  .tr('-', '_').gsub(/[\[\]]/, '').to_sym
241
234
  end
235
+
236
+ def require_feature(file)
237
+ # If any features were added on the CLI from `--require`,
238
+ # add them to the config.
239
+ ConfigLoader.add_loaded_features(file)
240
+ require file
241
+ end
242
242
  end
243
243
 
244
244
  # Validates option arguments and the options' compatibility with each other.
@@ -470,7 +470,7 @@ module RuboCop
470
470
  'This option applies to the previously',
471
471
  'specified --format, or the default format',
472
472
  'if no format is specified.'],
473
- fail_level: ['Minimum severity (A/R/C/W/E/F) for exit',
473
+ fail_level: ['Minimum severity (A/I/R/C/W/E/F) for exit',
474
474
  'with error code.'],
475
475
  display_time: 'Display elapsed time in seconds.',
476
476
  display_only_failed: ['Only output offense messages. Omit passing',
@@ -9,10 +9,6 @@ module CopHelper
9
9
  let(:ruby_version) { 2.4 }
10
10
  let(:rails_version) { false }
11
11
 
12
- def inspect_source_file(source)
13
- Tempfile.open('tmp') { |f| inspect_source(source, f) }
14
- end
15
-
16
12
  def inspect_source(source, file = nil)
17
13
  RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
18
14
  RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
@@ -111,28 +111,12 @@ module RuboCop
111
111
  source
112
112
  end
113
113
 
114
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
115
- def expect_offense(source, file = nil, severity: nil, **replacements)
116
- source = format_offense(source, **replacements)
117
- RuboCop::Formatter::DisabledConfigFormatter
118
- .config_to_allow_offenses = {}
119
- RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
120
- cop.instance_variable_get(:@options)[:auto_correct] = true
121
-
122
- expected_annotations = AnnotatedSource.parse(source)
123
-
124
- if expected_annotations.plain_source == source
125
- raise 'Use `expect_no_offenses` to assert that no offenses are found'
126
- end
127
-
128
- @processed_source = parse_source(expected_annotations.plain_source,
129
- file)
130
-
131
- unless @processed_source.valid_syntax?
132
- raise 'Error parsing example code: ' \
133
- "#{@processed_source.diagnostics.map(&:render).join("\n")}"
134
- end
114
+ def expect_offense(source, file = nil, severity: nil, chomp: false, **replacements)
115
+ expected_annotations = parse_annotations(source, **replacements)
116
+ source = expected_annotations.plain_source
117
+ source = source.chomp if chomp
135
118
 
119
+ @processed_source = parse_processed_source(source, file)
136
120
  @offenses = _investigate(cop, @processed_source)
137
121
  actual_annotations =
138
122
  expected_annotations.with_offense_annotations(@offenses)
@@ -143,7 +127,14 @@ module RuboCop
143
127
  @offenses
144
128
  end
145
129
 
146
- def expect_correction(correction, loop: true)
130
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
131
+ def expect_correction(correction, loop: true, source: nil)
132
+ if source
133
+ expected_annotations = parse_annotations(source, raise_error: false)
134
+ @processed_source = parse_processed_source(expected_annotations.plain_source)
135
+ _investigate(cop, @processed_source)
136
+ end
137
+
147
138
  raise '`expect_correction` must follow `expect_offense`' unless @processed_source
148
139
 
149
140
  iteration = 0
@@ -192,6 +183,30 @@ module RuboCop
192
183
  expect(actual_annotations.to_s).to eq(source)
193
184
  end
194
185
 
186
+ def parse_annotations(source, raise_error: true, **replacements)
187
+ set_formatter_options
188
+
189
+ source = format_offense(source, **replacements)
190
+ annotations = AnnotatedSource.parse(source)
191
+ return annotations unless raise_error && annotations.plain_source == source
192
+
193
+ raise 'Use `expect_no_offenses` to assert that no offenses are found'
194
+ end
195
+
196
+ def parse_processed_source(source, file = nil)
197
+ processed_source = parse_source(source, file)
198
+ return processed_source if processed_source.valid_syntax?
199
+
200
+ raise 'Error parsing example code: ' \
201
+ "#{processed_source.diagnostics.map(&:render).join("\n")}"
202
+ end
203
+
204
+ def set_formatter_options
205
+ RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
206
+ RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
207
+ cop.instance_variable_get(:@options)[:auto_correct] = true
208
+ end
209
+
195
210
  # Parsed representation of code annotated with the `^^^ Message` style
196
211
  class AnnotatedSource
197
212
  ANNOTATION_PATTERN = /\A\s*(\^+|\^{}) /.freeze