rubocop 0.84.0 → 0.85.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +33 -15
  4. data/lib/rubocop.rb +6 -0
  5. data/lib/rubocop/cli.rb +2 -2
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +2 -2
  7. data/lib/rubocop/comment_config.rb +1 -1
  8. data/lib/rubocop/config.rb +3 -1
  9. data/lib/rubocop/config_loader.rb +1 -1
  10. data/lib/rubocop/config_loader_resolver.rb +18 -2
  11. data/lib/rubocop/config_store.rb +12 -2
  12. data/lib/rubocop/cop/bundler/gem_comment.rb +70 -1
  13. data/lib/rubocop/cop/commissioner.rb +0 -21
  14. data/lib/rubocop/cop/cop.rb +14 -6
  15. data/lib/rubocop/cop/corrector.rb +3 -1
  16. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +1 -1
  17. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +3 -1
  18. data/lib/rubocop/cop/layout/case_indentation.rb +3 -3
  19. data/lib/rubocop/cop/layout/class_structure.rb +19 -16
  20. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -2
  21. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  23. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
  24. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  25. data/lib/rubocop/cop/layout/hash_alignment.rb +6 -6
  26. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  27. data/lib/rubocop/cop/layout/heredoc_indentation.rb +19 -102
  28. data/lib/rubocop/cop/layout/line_length.rb +17 -17
  29. data/lib/rubocop/cop/layout/multiline_block_layout.rb +3 -1
  30. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  31. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +3 -1
  32. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +3 -1
  33. data/lib/rubocop/cop/lint/ambiguous_operator.rb +2 -1
  34. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +8 -4
  35. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  36. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -1
  37. data/lib/rubocop/cop/lint/loop.rb +1 -1
  38. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +62 -0
  39. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  40. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +7 -7
  41. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +3 -1
  42. data/lib/rubocop/cop/lint/redundant_require_statement.rb +3 -3
  43. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  44. data/lib/rubocop/cop/lint/suppressed_exception.rb +4 -2
  45. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  46. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +1 -1
  47. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  48. data/lib/rubocop/cop/migration/department_name.rb +9 -5
  49. data/lib/rubocop/cop/mixin/array_min_size.rb +3 -1
  50. data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -1
  51. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
  52. data/lib/rubocop/cop/mixin/ignored_pattern.rb +1 -1
  53. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  54. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +3 -1
  55. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +16 -0
  56. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -1
  57. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +1 -1
  58. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +11 -1
  59. data/lib/rubocop/cop/naming/file_name.rb +26 -11
  60. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  61. data/lib/rubocop/cop/registry.rb +3 -1
  62. data/lib/rubocop/cop/style/array_join.rb +1 -1
  63. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  64. data/lib/rubocop/cop/style/copyright.rb +2 -2
  65. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  66. data/lib/rubocop/cop/style/exponential_notation.rb +3 -3
  67. data/lib/rubocop/cop/style/format_string_token.rb +2 -3
  68. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -2
  69. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -1
  70. data/lib/rubocop/cop/style/hash_syntax.rb +5 -3
  71. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  72. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  73. data/lib/rubocop/cop/style/negated_if.rb +3 -3
  74. data/lib/rubocop/cop/style/negated_unless.rb +3 -3
  75. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  76. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  77. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +89 -0
  78. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +130 -0
  79. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  80. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +3 -3
  81. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +3 -3
  82. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +13 -13
  83. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +3 -3
  84. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +3 -1
  85. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  86. data/lib/rubocop/cop/style/when_then.rb +1 -1
  87. data/lib/rubocop/cop/team.rb +61 -25
  88. data/lib/rubocop/cop/util.rb +1 -1
  89. data/lib/rubocop/cop/variable_force/branch.rb +3 -1
  90. data/lib/rubocop/formatter/junit_formatter.rb +14 -4
  91. data/lib/rubocop/magic_comment.rb +1 -1
  92. data/lib/rubocop/options.rb +17 -3
  93. data/lib/rubocop/result_cache.rb +4 -4
  94. data/lib/rubocop/rspec/cop_helper.rb +2 -23
  95. data/lib/rubocop/rspec/expect_offense.rb +42 -6
  96. data/lib/rubocop/rspec/shared_contexts.rb +2 -2
  97. data/lib/rubocop/runner.rb +14 -10
  98. data/lib/rubocop/target_finder.rb +3 -1
  99. data/lib/rubocop/target_ruby.rb +4 -1
  100. data/lib/rubocop/version.rb +1 -1
  101. metadata +21 -3
@@ -71,9 +71,27 @@ module RuboCop
71
71
  # RUBY
72
72
  #
73
73
  # expect_no_corrections
74
+ #
75
+ # If your code has variables of different lengths, you can use `%{foo}`
76
+ # and `^{foo}` to format your template:
77
+ #
78
+ # %w[raise fail].each do |keyword|
79
+ # expect_offense(<<~RUBY, keyword: keyword)
80
+ # %{keyword}(RuntimeError, msg)
81
+ # ^{keyword}^^^^^^^^^^^^^^^^^^^ Redundant `RuntimeError` argument can be removed.
82
+ # RUBY
74
83
  module ExpectOffense
84
+ def format_offense(source, **replacements)
85
+ replacements.each do |keyword, value|
86
+ source = source.gsub("%{#{keyword}}", value)
87
+ .gsub("^{#{keyword}}", '^' * value.size)
88
+ end
89
+ source
90
+ end
91
+
75
92
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
76
- def expect_offense(source, file = nil)
93
+ def expect_offense(source, file = nil, **replacements)
94
+ source = format_offense(source, **replacements)
77
95
  RuboCop::Formatter::DisabledConfigFormatter
78
96
  .config_to_allow_offenses = {}
79
97
  RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
@@ -96,17 +114,35 @@ module RuboCop
96
114
 
97
115
  expect(actual_annotations.to_s).to eq(expected_annotations.to_s)
98
116
  end
99
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
100
117
 
101
- def expect_correction(correction)
118
+ def expect_correction(correction, loop: false)
102
119
  raise '`expect_correction` must follow `expect_offense`' unless @processed_source
103
120
 
104
- corrector =
105
- RuboCop::Cop::Corrector.new(@processed_source.buffer, cop.corrections)
106
- new_source = corrector.rewrite
121
+ iteration = 0
122
+ new_source = loop do
123
+ iteration += 1
124
+
125
+ corrector =
126
+ RuboCop::Cop::Corrector.new(@processed_source.buffer, cop.corrections)
127
+ corrected_source = corrector.rewrite
128
+
129
+ break corrected_source unless loop
130
+ break corrected_source if cop.corrections.empty?
131
+
132
+ if iteration > RuboCop::Runner::MAX_ITERATIONS
133
+ raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, [])
134
+ end
135
+
136
+ # Prepare for next loop
137
+ cop.instance_variable_set(:@corrections, [])
138
+ @processed_source = parse_source(corrected_source,
139
+ @processed_source.path)
140
+ _investigate(cop, @processed_source)
141
+ end
107
142
 
108
143
  expect(new_source).to eq(correction)
109
144
  end
145
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
110
146
 
111
147
  def expect_no_corrections
112
148
  raise '`expect_no_corrections` must follow `expect_offense`' unless @processed_source
@@ -40,7 +40,7 @@ end
40
40
 
41
41
  # This context assumes nothing and defines `cop`, among others.
42
42
  RSpec.shared_context 'config', :config do # rubocop:disable Metrics/BlockLength
43
- ### Meant to be overriden at will
43
+ ### Meant to be overridden at will
44
44
 
45
45
  let(:source) { 'code = {some: :ruby}' }
46
46
 
@@ -65,7 +65,7 @@ RSpec.shared_context 'config', :config do # rubocop:disable Metrics/BlockLength
65
65
  range.exclude_end? ? range.end : range.end + 1)
66
66
  end
67
67
 
68
- ### Usefull intermediary steps (less likely to be overriden)
68
+ ### Useful intermediary steps (less likely to be overridden)
69
69
 
70
70
  let(:processed_source) { parse_source(source, 'test') }
71
71
 
@@ -129,7 +129,7 @@ module RuboCop
129
129
  end
130
130
 
131
131
  def file_offense_cache(file)
132
- config = @config_store.for(file)
132
+ config = @config_store.for_file(file)
133
133
  cache = cached_result(file, standby_team(config)) if cached_run?
134
134
 
135
135
  if cache&.valid?
@@ -168,7 +168,7 @@ module RuboCop
168
168
  end
169
169
 
170
170
  def redundant_cop_disable_directive(file)
171
- config = @config_store.for(file)
171
+ config = @config_store.for_file(file)
172
172
  if config.for_cop(Cop::Lint::RedundantCopDisableDirective)
173
173
  .fetch('Enabled')
174
174
  cop = Cop::Lint::RedundantCopDisableDirective.new(config, @options)
@@ -183,7 +183,7 @@ module RuboCop
183
183
  def autocorrect_redundant_disables(file, source, cop, offenses)
184
184
  cop.processed_source = source
185
185
 
186
- team = Cop::Team.new(RuboCop::Cop::Registry.new, nil, @options)
186
+ team = Cop::Team.mobilize(RuboCop::Cop::Registry.new, nil, @options)
187
187
  team.autocorrect(source.buffer, [cop])
188
188
 
189
189
  return [] unless team.updated_source_file?
@@ -204,7 +204,9 @@ module RuboCop
204
204
  end
205
205
 
206
206
  def file_finished(file, offenses)
207
- offenses = offenses.select { |o| considered_failure?(o) } if @options[:display_only_fail_level_offenses]
207
+ if @options[:display_only_fail_level_offenses]
208
+ offenses = offenses.select { |o| considered_failure?(o) }
209
+ end
208
210
  formatter_set.file_finished(file, offenses)
209
211
  end
210
212
 
@@ -212,7 +214,7 @@ module RuboCop
212
214
  @cached_run ||=
213
215
  (@options[:cache] == 'true' ||
214
216
  @options[:cache] != 'false' &&
215
- @config_store.for(Dir.pwd).for_all_cops['UseCache']) &&
217
+ @config_store.for_dir(Dir.pwd).for_all_cops['UseCache']) &&
216
218
  # When running --auto-gen-config, there's some processing done in the
217
219
  # cops related to calculating the Max parameters for Metrics cops. We
218
220
  # need to do that processing and cannot use caching.
@@ -284,14 +286,16 @@ module RuboCop
284
286
  def check_for_infinite_loop(processed_source, offenses)
285
287
  checksum = processed_source.checksum
286
288
 
287
- raise InfiniteCorrectionLoop.new(processed_source.path, offenses) if @processed_sources.include?(checksum)
289
+ if @processed_sources.include?(checksum)
290
+ raise InfiniteCorrectionLoop.new(processed_source.path, offenses)
291
+ end
288
292
 
289
293
  @processed_sources << checksum
290
294
  end
291
295
 
292
296
  def inspect_file(processed_source)
293
- config = @config_store.for(processed_source.path)
294
- team = Cop::Team.new(mobilized_cop_classes(config), config, @options)
297
+ config = @config_store.for_file(processed_source.path)
298
+ team = Cop::Team.mobilize(mobilized_cop_classes(config), config, @options)
295
299
  offenses = team.inspect_file(processed_source)
296
300
  @errors.concat(team.errors)
297
301
  @warnings.concat(team.warnings)
@@ -356,7 +360,7 @@ module RuboCop
356
360
  end
357
361
 
358
362
  def get_processed_source(file)
359
- ruby_version = @config_store.for(file).target_ruby_version
363
+ ruby_version = @config_store.for_file(file).target_ruby_version
360
364
 
361
365
  if @options[:stdin]
362
366
  ProcessedSource.new(@options[:stdin], ruby_version, file)
@@ -376,7 +380,7 @@ module RuboCop
376
380
  def standby_team(config)
377
381
  @team_by_config ||= {}
378
382
  @team_by_config[config.object_id] ||=
379
- Cop::Team.new(mobilized_cop_classes(config), config, @options)
383
+ Cop::Team.mobilize(mobilized_cop_classes(config), config, @options)
380
384
  end
381
385
  end
382
386
  end
@@ -170,7 +170,9 @@ module RuboCop
170
170
  def process_explicit_path(path, mode)
171
171
  files = path.include?('*') ? Dir[path] : [path]
172
172
 
173
- files.select! { |file| included_file?(file) } if mode == :only_recognized_file_types || force_exclusion?
173
+ if mode == :only_recognized_file_types || force_exclusion?
174
+ files.select! { |file| included_file?(file) }
175
+ end
174
176
 
175
177
  return files unless force_exclusion?
176
178
 
@@ -7,7 +7,7 @@ module RuboCop
7
7
  DEFAULT_VERSION = KNOWN_RUBIES.first
8
8
 
9
9
  OBSOLETE_RUBIES = {
10
- 1.9 => '0.50', 2.0 => '0.50', 2.1 => '0.58', 2.2 => '0.69', 2.3 => '0.82'
10
+ 1.9 => '0.50', 2.0 => '0.50', 2.1 => '0.58', 2.2 => '0.69', 2.3 => '0.81'
11
11
  }.freeze
12
12
  private_constant :KNOWN_RUBIES, :OBSOLETE_RUBIES
13
13
 
@@ -52,7 +52,10 @@ module RuboCop
52
52
  file = ruby_version_file
53
53
  return unless file && File.file?(file)
54
54
 
55
+ # rubocop:disable Lint/MixedRegexpCaptureTypes
56
+ # `(ruby-)` is not a capture type.
55
57
  File.read(file).match(/\A(ruby-)?(?<version>\d+\.\d+)/) do |md|
58
+ # rubocop:enable Lint/MixedRegexpCaptureTypes
56
59
  md[:version].to_f
57
60
  end
58
61
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '0.84.0'
6
+ STRING = '0.85.0'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, '\
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.84.0
4
+ version: 0.85.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2020-05-21 00:00:00.000000000 Z
13
+ date: 2020-06-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parallel
@@ -60,6 +60,20 @@ dependencies:
60
60
  - - "<"
61
61
  - !ruby/object:Gem::Version
62
62
  version: '4.0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: regexp_parser
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '1.7'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '1.7'
63
77
  - !ruby/object:Gem::Dependency
64
78
  name: rexml
65
79
  requirement: !ruby/object:Gem::Requirement
@@ -345,6 +359,7 @@ files:
345
359
  - lib/rubocop/cop/lint/literal_in_interpolation.rb
346
360
  - lib/rubocop/cop/lint/loop.rb
347
361
  - lib/rubocop/cop/lint/missing_cop_enable_directive.rb
362
+ - lib/rubocop/cop/lint/mixed_regexp_capture_types.rb
348
363
  - lib/rubocop/cop/lint/multiple_comparison.rb
349
364
  - lib/rubocop/cop/lint/nested_method_definition.rb
350
365
  - lib/rubocop/cop/lint/nested_percent_literal.rb
@@ -457,6 +472,7 @@ files:
457
472
  - lib/rubocop/cop/mixin/preferred_delimiters.rb
458
473
  - lib/rubocop/cop/mixin/range_help.rb
459
474
  - lib/rubocop/cop/mixin/rational_literal.rb
475
+ - lib/rubocop/cop/mixin/regexp_literal_help.rb
460
476
  - lib/rubocop/cop/mixin/rescue_node.rb
461
477
  - lib/rubocop/cop/mixin/safe_assignment.rb
462
478
  - lib/rubocop/cop/mixin/space_after_punctuation.rb
@@ -624,6 +640,8 @@ files:
624
640
  - lib/rubocop/cop/style/redundant_interpolation.rb
625
641
  - lib/rubocop/cop/style/redundant_parentheses.rb
626
642
  - lib/rubocop/cop/style/redundant_percent_q.rb
643
+ - lib/rubocop/cop/style/redundant_regexp_character_class.rb
644
+ - lib/rubocop/cop/style/redundant_regexp_escape.rb
627
645
  - lib/rubocop/cop/style/redundant_return.rb
628
646
  - lib/rubocop/cop/style/redundant_self.rb
629
647
  - lib/rubocop/cop/style/redundant_sort.rb
@@ -732,7 +750,7 @@ homepage: https://github.com/rubocop-hq/rubocop
732
750
  licenses:
733
751
  - MIT
734
752
  metadata:
735
- homepage_uri: https://www.rubocop.org/
753
+ homepage_uri: https://rubocop.org/
736
754
  changelog_uri: https://github.com/rubocop-hq/rubocop/blob/master/CHANGELOG.md
737
755
  source_code_uri: https://github.com/rubocop-hq/rubocop/
738
756
  documentation_uri: https://docs.rubocop.org/