rubocop 1.3.0 → 1.5.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +75 -16
  4. data/lib/rubocop.rb +5 -0
  5. data/lib/rubocop/cli.rb +5 -1
  6. data/lib/rubocop/cli/command/execute_runner.rb +26 -11
  7. data/lib/rubocop/cli/command/suggest_extensions.rb +67 -0
  8. data/lib/rubocop/config_loader.rb +12 -4
  9. data/lib/rubocop/config_loader_resolver.rb +5 -1
  10. data/lib/rubocop/config_obsoletion.rb +21 -3
  11. data/lib/rubocop/config_regeneration.rb +1 -1
  12. data/lib/rubocop/config_validator.rb +8 -1
  13. data/lib/rubocop/cop/autocorrect_logic.rb +21 -6
  14. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  15. data/lib/rubocop/cop/generator.rb +2 -9
  16. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  17. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
  18. data/lib/rubocop/cop/layout/class_structure.rb +15 -3
  19. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +80 -10
  20. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +6 -1
  21. data/lib/rubocop/cop/layout/end_of_line.rb +5 -5
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +7 -2
  23. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  24. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -1
  25. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +4 -1
  26. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  27. data/lib/rubocop/cop/lint/missing_super.rb +7 -4
  28. data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +1 -1
  29. data/lib/rubocop/cop/lint/to_enum_arguments.rb +6 -15
  30. data/lib/rubocop/cop/lint/unexpected_block_arity.rb +85 -0
  31. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +20 -6
  32. data/lib/rubocop/cop/metrics/abc_size.rb +25 -1
  33. data/lib/rubocop/cop/metrics/block_length.rb +13 -7
  34. data/lib/rubocop/cop/metrics/method_length.rb +7 -2
  35. data/lib/rubocop/cop/metrics/parameter_lists.rb +64 -1
  36. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +20 -10
  37. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +146 -0
  38. data/lib/rubocop/cop/metrics/utils/repeated_csend_discount.rb +6 -1
  39. data/lib/rubocop/cop/mixin/configurable_numbering.rb +4 -3
  40. data/lib/rubocop/cop/mixin/enforce_superclass.rb +9 -1
  41. data/lib/rubocop/cop/mixin/ignored_methods.rb +36 -3
  42. data/lib/rubocop/cop/mixin/method_complexity.rb +6 -0
  43. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  44. data/lib/rubocop/cop/mixin/visibility_help.rb +1 -3
  45. data/lib/rubocop/cop/style/and_or.rb +10 -0
  46. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -3
  47. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +8 -1
  48. data/lib/rubocop/cop/style/documentation.rb +12 -1
  49. data/lib/rubocop/cop/style/format_string.rb +8 -3
  50. data/lib/rubocop/cop/style/if_with_semicolon.rb +39 -4
  51. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -2
  52. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +11 -2
  53. data/lib/rubocop/cop/style/negated_if_else_condition.rb +3 -1
  54. data/lib/rubocop/cop/style/numeric_literals.rb +14 -11
  55. data/lib/rubocop/cop/style/redundant_argument.rb +75 -0
  56. data/lib/rubocop/cop/style/redundant_condition.rb +2 -1
  57. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  58. data/lib/rubocop/cop/style/sole_nested_conditional.rb +49 -3
  59. data/lib/rubocop/cop/style/symbol_proc.rb +5 -3
  60. data/lib/rubocop/cop/util.rb +1 -1
  61. data/lib/rubocop/cop/variable_force/branch.rb +1 -1
  62. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  63. data/lib/rubocop/core_ext/hash.rb +20 -0
  64. data/lib/rubocop/ext/regexp_node.rb +5 -10
  65. data/lib/rubocop/ext/regexp_parser.rb +2 -9
  66. data/lib/rubocop/formatter/disabled_config_formatter.rb +21 -6
  67. data/lib/rubocop/options.rb +5 -0
  68. data/lib/rubocop/rake_task.rb +2 -2
  69. data/lib/rubocop/runner.rb +1 -1
  70. data/lib/rubocop/target_finder.rb +1 -1
  71. data/lib/rubocop/target_ruby.rb +12 -4
  72. data/lib/rubocop/version.rb +1 -1
  73. metadata +12 -10
  74. data/bin/console +0 -10
  75. data/bin/rubocop-profile +0 -32
  76. data/bin/setup +0 -7
@@ -20,18 +20,11 @@ module RuboCop
20
20
  module Expression
21
21
  # Add `expression` and `loc` to all `regexp_parser` nodes
22
22
  module Base
23
- attr_accessor :origin, :source
24
-
25
- def start_index
26
- # ts is a byte index; convert it to a character index
27
- @start_index ||= source.byteslice(0, ts).length
28
- end
23
+ attr_accessor :origin
29
24
 
30
25
  # Shortcut to `loc.expression`
31
26
  def expression
32
- @expression ||= begin
33
- origin.adjust(begin_pos: start_index, end_pos: start_index + full_length)
34
- end
27
+ @expression ||= origin.adjust(begin_pos: ts, end_pos: ts + full_length)
35
28
  end
36
29
 
37
30
  # @returns a location map like `parser` does, with:
@@ -5,6 +5,8 @@ module RuboCop
5
5
  # This formatter displays a YAML configuration file where all cops that
6
6
  # detected any offenses are configured to not detect the offense.
7
7
  class DisabledConfigFormatter < BaseFormatter
8
+ include PathUtil
9
+
8
10
  HEADING = <<~COMMENTS
9
11
  # This configuration was generated by
10
12
  # `%<command>s`
@@ -195,15 +197,28 @@ module RuboCop
195
197
  end
196
198
 
197
199
  def excludes(offending_files, cop_name, parent)
198
- # Exclude properties in .rubocop_todo.yml override default ones, as well
199
- # as any custom excludes in .rubocop.yml, so in order to retain those
200
- # excludes we must copy them.
201
- # There can be multiple .rubocop.yml files in subdirectories, but we
202
- # just look at the current working directory
200
+ # Exclude properties in .rubocop_todo.yml override default ones, as well as any custom
201
+ # excludes in .rubocop.yml, so in order to retain those excludes we must copy them.
202
+ # There can be multiple .rubocop.yml files in subdirectories, but we just look at the
203
+ # current working directory.
203
204
  config = ConfigStore.new.for(parent)
204
205
  cfg = config[cop_name] || {}
205
206
 
206
- ((cfg['Exclude'] || []) + offending_files).uniq
207
+ if merge_mode_for_exclude?(config) || merge_mode_for_exclude?(cfg)
208
+ offending_files
209
+ else
210
+ cop_exclude = cfg['Exclude']
211
+ if cop_exclude && cop_exclude != default_config(cop_name)['Exclude']
212
+ warn "`#{cop_name}: Exclude` in `#{smart_path(config.loaded_path)}` overrides a " \
213
+ 'generated `Exclude` in `.rubocop_todo.yml`. Either remove ' \
214
+ "`#{cop_name}: Exclude` or add `inherit_mode: merge: - Exclude`."
215
+ end
216
+ ((cop_exclude || []) + offending_files).uniq
217
+ end
218
+ end
219
+
220
+ def merge_mode_for_exclude?(cfg)
221
+ Array(cfg.to_h.dig('inherit_mode', 'merge')).include?('Exclude')
207
222
  end
208
223
 
209
224
  def output_exclude_path(output_buffer, exclude_path, parent)
@@ -5,6 +5,7 @@ require 'shellwords'
5
5
 
6
6
  module RuboCop
7
7
  class IncorrectCopNameError < StandardError; end
8
+
8
9
  class OptionArgumentError < StandardError; end
9
10
 
10
11
  # This class handles command line options.
@@ -195,6 +196,7 @@ module RuboCop
195
196
 
196
197
  option(opts, '--safe')
197
198
 
199
+ option(opts, '--stderr')
198
200
  option(opts, '--[no-]color')
199
201
 
200
202
  option(opts, '-v', '--version')
@@ -498,6 +500,9 @@ module RuboCop
498
500
  extra_details: 'Display extra details in offense messages.',
499
501
  lint: 'Run only lint cops.',
500
502
  safe: 'Run only safe cops.',
503
+ stderr: ['Write all output to stderr except for the',
504
+ 'autocorrected source. This is especially useful',
505
+ 'when combined with --auto-correct and --stdin.'],
501
506
  list_target_files: 'List all files RuboCop will inspect.',
502
507
  auto_correct: 'Auto-correct offenses (only when it\'s safe).',
503
508
  safe_auto_correct: '(same, deprecated)',
@@ -22,7 +22,7 @@ module RuboCop
22
22
 
23
23
  task(name, *args) do |_, task_args|
24
24
  RakeFileUtils.verbose(verbose) do
25
- yield(*[self, task_args].slice(0, task_block.arity)) if block_given?
25
+ yield(*[self, task_args].slice(0, task_block.arity)) if task_block
26
26
  run_cli(verbose, full_options)
27
27
  end
28
28
  end
@@ -66,7 +66,7 @@ module RuboCop
66
66
 
67
67
  task(:auto_correct, *args) do |_, task_args|
68
68
  RakeFileUtils.verbose(verbose) do
69
- yield(*[self, task_args].slice(0, task_block.arity)) if block_given?
69
+ yield(*[self, task_args].slice(0, task_block.arity)) if task_block
70
70
  options = full_options.unshift('--auto-correct-all')
71
71
  options.delete('--parallel')
72
72
  run_cli(verbose, options)
@@ -64,7 +64,7 @@ module RuboCop
64
64
  # instances that each inspects its allotted group of files.
65
65
  def warm_cache(target_files)
66
66
  puts 'Running parallel inspection' if @options[:debug]
67
- Parallel.each(target_files, &method(:file_offenses))
67
+ Parallel.each(target_files) { |target_file| file_offenses(target_file) }
68
68
  end
69
69
 
70
70
  def find_target_files(paths)
@@ -94,7 +94,7 @@ module RuboCop
94
94
  end
95
95
 
96
96
  def wanted_dir_patterns(base_dir, exclude_pattern, flags)
97
- dirs = Dir.glob(File.join(base_dir, '*/'), flags)
97
+ dirs = Dir.glob(File.join(base_dir.gsub('/**/', '/\**/'), '*/'), flags)
98
98
  .reject do |dir|
99
99
  dir.end_with?('/./', '/../') || File.fnmatch?(exclude_pattern, dir, flags)
100
100
  end
@@ -123,6 +123,10 @@ module RuboCop
123
123
  (send _ :required_ruby_version= $_)
124
124
  PATTERN
125
125
 
126
+ def_node_matcher :gem_requirement?, <<~PATTERN
127
+ (send (const(const _ :Gem):Requirement) :new $str)
128
+ PATTERN
129
+
126
130
  def name
127
131
  "`required_ruby_version` parameter (in #{gemspec_filename})"
128
132
  end
@@ -136,10 +140,9 @@ module RuboCop
136
140
  version = version_from_gemspec_file(file)
137
141
  return if version.nil?
138
142
 
139
- if version.array_type?
140
- versions = version.children.map { |v| version_from_str(v.str_content) }
141
- return versions.compact.min
142
- end
143
+ requirement = version.children.last
144
+ return version_from_array(version) if version.array_type?
145
+ return version_from_array(requirement) if gem_requirement? version
143
146
 
144
147
  version_from_str(version.str_content)
145
148
  end
@@ -161,6 +164,11 @@ module RuboCop
161
164
  required_ruby_version(processed_source.ast).first
162
165
  end
163
166
 
167
+ def version_from_array(array)
168
+ versions = array.children.map { |v| version_from_str(v.is_a?(String) ? v : v.str_content) }
169
+ versions.compact.min
170
+ end
171
+
164
172
  def version_from_str(str)
165
173
  str.match(/^(?:>=|<=)?\s*(?<version>\d+(?:\.\d+)*)/) do |md|
166
174
  md[:version].to_f
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.3.0'
6
+ STRING = '1.5.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: 1.3.0
4
+ version: 1.5.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-11-12 00:00:00.000000000 Z
13
+ date: 2020-12-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parallel
@@ -66,14 +66,14 @@ dependencies:
66
66
  requirements:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
- version: '1.8'
69
+ version: '2.0'
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - ">="
75
75
  - !ruby/object:Gem::Version
76
- version: '1.8'
76
+ version: '2.0'
77
77
  - !ruby/object:Gem::Dependency
78
78
  name: rexml
79
79
  requirement: !ruby/object:Gem::Requirement
@@ -94,14 +94,14 @@ dependencies:
94
94
  requirements:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
- version: 1.1.1
97
+ version: 1.2.0
98
98
  type: :runtime
99
99
  prerelease: false
100
100
  version_requirements: !ruby/object:Gem::Requirement
101
101
  requirements:
102
102
  - - ">="
103
103
  - !ruby/object:Gem::Version
104
- version: 1.1.1
104
+ version: 1.2.0
105
105
  - !ruby/object:Gem::Dependency
106
106
  name: ruby-progressbar
107
107
  requirement: !ruby/object:Gem::Requirement
@@ -171,9 +171,6 @@ files:
171
171
  - README.md
172
172
  - assets/logo.png
173
173
  - assets/output.html.erb
174
- - bin/console
175
- - bin/rubocop-profile
176
- - bin/setup
177
174
  - config/default.yml
178
175
  - exe/rubocop
179
176
  - lib/rubocop.rb
@@ -186,6 +183,7 @@ files:
186
183
  - lib/rubocop/cli/command/execute_runner.rb
187
184
  - lib/rubocop/cli/command/init_dotfile.rb
188
185
  - lib/rubocop/cli/command/show_cops.rb
186
+ - lib/rubocop/cli/command/suggest_extensions.rb
189
187
  - lib/rubocop/cli/command/version.rb
190
188
  - lib/rubocop/cli/environment.rb
191
189
  - lib/rubocop/comment_config.rb
@@ -429,6 +427,7 @@ files:
429
427
  - lib/rubocop/cop/lint/top_level_return_with_argument.rb
430
428
  - lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb
431
429
  - lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
430
+ - lib/rubocop/cop/lint/unexpected_block_arity.rb
432
431
  - lib/rubocop/cop/lint/unified_integer.rb
433
432
  - lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb
434
433
  - lib/rubocop/cop/lint/unreachable_code.rb
@@ -457,6 +456,7 @@ files:
457
456
  - lib/rubocop/cop/metrics/utils/abc_size_calculator.rb
458
457
  - lib/rubocop/cop/metrics/utils/code_length_calculator.rb
459
458
  - lib/rubocop/cop/metrics/utils/iterating_block.rb
459
+ - lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb
460
460
  - lib/rubocop/cop/metrics/utils/repeated_csend_discount.rb
461
461
  - lib/rubocop/cop/migration/department_name.rb
462
462
  - lib/rubocop/cop/mixin/alignment.rb
@@ -685,6 +685,7 @@ files:
685
685
  - lib/rubocop/cop/style/proc.rb
686
686
  - lib/rubocop/cop/style/raise_args.rb
687
687
  - lib/rubocop/cop/style/random_with_offset.rb
688
+ - lib/rubocop/cop/style/redundant_argument.rb
688
689
  - lib/rubocop/cop/style/redundant_assignment.rb
689
690
  - lib/rubocop/cop/style/redundant_begin.rb
690
691
  - lib/rubocop/cop/style/redundant_capital_w.rb
@@ -766,6 +767,7 @@ files:
766
767
  - lib/rubocop/cop/variable_force/variable.rb
767
768
  - lib/rubocop/cop/variable_force/variable_table.rb
768
769
  - lib/rubocop/cops_documentation_generator.rb
770
+ - lib/rubocop/core_ext/hash.rb
769
771
  - lib/rubocop/core_ext/string.rb
770
772
  - lib/rubocop/directive_comment.rb
771
773
  - lib/rubocop/error.rb
@@ -821,7 +823,7 @@ metadata:
821
823
  homepage_uri: https://rubocop.org/
822
824
  changelog_uri: https://github.com/rubocop-hq/rubocop/blob/master/CHANGELOG.md
823
825
  source_code_uri: https://github.com/rubocop-hq/rubocop/
824
- documentation_uri: https://docs.rubocop.org/rubocop/1.3/
826
+ documentation_uri: https://docs.rubocop.org/rubocop/1.5/
825
827
  bug_tracker_uri: https://github.com/rubocop-hq/rubocop/issues
826
828
  post_install_message:
827
829
  rdoc_options: []
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'pry'
6
- require 'rubocop'
7
-
8
- ARGV.clear
9
-
10
- RuboCop.pry
@@ -1,32 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- if ARGV.include?('-h') || ARGV.include?('--help')
5
- puts "Usage: same as main `rubocop` command but gathers profiling info"
6
- puts "Additional option: `--memory` to print memory usage"
7
- exit(0)
8
- end
9
- with_mem = ARGV.delete('--memory')
10
- ARGV.unshift '--cache', 'false' unless ARGV.include?('--cache')
11
-
12
- require 'stackprof'
13
- if with_mem
14
- require 'memory_profiler'
15
- MemoryProfiler.start
16
- end
17
- StackProf.start
18
- start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
19
- begin
20
- load "#{__dir__}/../exe/rubocop"
21
- ensure
22
- delta = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
23
- puts "Finished in #{delta.round(1)} seconds"
24
- StackProf.stop
25
- if with_mem
26
- puts "Building memory report..."
27
- report = MemoryProfiler.stop
28
- end
29
- Dir.mkdir('tmp') unless File.exist?('tmp')
30
- StackProf.results('tmp/stackprof.dump')
31
- report&.pretty_print(scale_bytes: true)
32
- end
data/bin/setup DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- set -euo pipefail
4
- IFS=$'\n\t'
5
- set -vx
6
-
7
- bundle install