rubocop 1.64.0 → 1.65.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -2
  3. data/config/default.yml +11 -1
  4. data/lib/rubocop/config_loader.rb +1 -1
  5. data/lib/rubocop/config_loader_resolver.rb +9 -3
  6. data/lib/rubocop/cop/cop.rb +20 -2
  7. data/lib/rubocop/cop/force.rb +12 -0
  8. data/lib/rubocop/cop/gemspec/add_runtime_dependency.rb +38 -0
  9. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
  10. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -3
  11. data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
  12. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +1 -1
  13. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -2
  14. data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
  15. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  16. data/lib/rubocop/cop/layout/line_length.rb +20 -20
  17. data/lib/rubocop/cop/layout/space_around_operators.rb +3 -0
  18. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +3 -4
  19. data/lib/rubocop/cop/legacy/corrector.rb +12 -2
  20. data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
  21. data/lib/rubocop/cop/lint/empty_when.rb +1 -1
  22. data/lib/rubocop/cop/lint/erb_new_arguments.rb +21 -14
  23. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +14 -7
  24. data/lib/rubocop/cop/lint/literal_as_condition.rb +1 -1
  25. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  26. data/lib/rubocop/cop/lint/to_enum_arguments.rb +2 -9
  27. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
  28. data/lib/rubocop/cop/lint/void.rb +5 -0
  29. data/lib/rubocop/cop/metrics/block_nesting.rb +19 -7
  30. data/lib/rubocop/cop/mixin/alignment.rb +5 -1
  31. data/lib/rubocop/cop/mixin/allowed_methods.rb +7 -1
  32. data/lib/rubocop/cop/mixin/allowed_pattern.rb +15 -3
  33. data/lib/rubocop/cop/mixin/configurable_max.rb +5 -1
  34. data/lib/rubocop/cop/mixin/rescue_node.rb +4 -0
  35. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -1
  36. data/lib/rubocop/cop/style/copyright.rb +5 -2
  37. data/lib/rubocop/cop/style/documentation.rb +24 -24
  38. data/lib/rubocop/cop/style/hash_except.rb +8 -5
  39. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +77 -43
  40. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +5 -0
  41. data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
  42. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  43. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -1
  44. data/lib/rubocop/cop/style/redundant_line_continuation.rb +2 -1
  45. data/lib/rubocop/cop/style/send_with_literal_method_name.rb +23 -2
  46. data/lib/rubocop/cop/style/super_arguments.rb +52 -15
  47. data/lib/rubocop/cop/style/symbol_proc.rb +8 -1
  48. data/lib/rubocop/cop/style/zero_length_predicate.rb +28 -24
  49. data/lib/rubocop/cop/team.rb +10 -0
  50. data/lib/rubocop/cop/util.rb +7 -1
  51. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  52. data/lib/rubocop/ext/regexp_parser.rb +4 -21
  53. data/lib/rubocop/formatter/html_formatter.rb +3 -1
  54. data/lib/rubocop/lsp/routes.rb +1 -1
  55. data/lib/rubocop/rspec/shared_contexts.rb +20 -0
  56. data/lib/rubocop/rspec/support.rb +1 -0
  57. data/lib/rubocop/server/cache.rb +10 -0
  58. data/lib/rubocop/server/client_command/exec.rb +2 -2
  59. data/lib/rubocop/server/client_command/start.rb +1 -1
  60. data/lib/rubocop/server/core.rb +4 -0
  61. data/lib/rubocop/version.rb +1 -1
  62. data/lib/rubocop.rb +1 -0
  63. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b6dfe4310319b850aafab50e198152b52585d4e9da81d18b5a83dba9ecf73b62
4
- data.tar.gz: a25d711605d517c0b1a4ed89eea3f5e65be8803a74f4c92d0b94d43ba9f7a4dd
3
+ metadata.gz: 459d541c6d59f9d241213403a76f0ebe00f91e248167a7d5b7b3cc9970d7e92c
4
+ data.tar.gz: db90495c3208a80a2a41efd7daaa5fcf2b88560cb47ff0bfa861bc01cfa2cf19
5
5
  SHA512:
6
- metadata.gz: 10d92f3f642368e7126f5066662c4e725c5743847efc8fa17c123390534c47ab528ce6ec986ff93800f569046c86988b4ffaa84c1ae89f2c44365f7867fda6f4
7
- data.tar.gz: e27fdf438f351200f01a33fba1059d678c37d4759eba3600b9c0f7d1f9329a344ff82e23518d38a4959c06069a1a2693447d2a5df8b202f1b11dad2ff012bae5
6
+ metadata.gz: 8b92f5ce74858e5142fb49469a84550e70fb19d388943432ec46b861ab224704d3ebbea964a94db275653c863b2a0afb202211557e9c26d7f4f25fdba3a741d9
7
+ data.tar.gz: 9a631b9c9b68f3c1489add6fcbf5bdd80cc043fdfb5a8d0d6525b83780df734ce7ccdae46523d3b516c105599baa01ab5807ff9a12f0e939b55c1c313f80d8d2
data/README.md CHANGED
@@ -5,7 +5,6 @@
5
5
  ----------
6
6
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
7
7
  [![Gem Version](https://badge.fury.io/rb/rubocop.svg)](https://badge.fury.io/rb/rubocop)
8
- [![CircleCI Status](https://circleci.com/gh/rubocop/rubocop/tree/master.svg?style=svg)](https://circleci.com/gh/rubocop/rubocop/tree/master)
9
8
  [![Actions Status](https://github.com/rubocop/rubocop/workflows/CI/badge.svg?branch=master)](https://github.com/rubocop/rubocop/actions?query=workflow%3ACI)
10
9
  [![Test Coverage](https://api.codeclimate.com/v1/badges/d2d67f728e88ea84ac69/test_coverage)](https://codeclimate.com/github/rubocop/rubocop/test_coverage)
11
10
  [![Maintainability](https://api.codeclimate.com/v1/badges/d2d67f728e88ea84ac69/maintainability)](https://codeclimate.com/github/rubocop/rubocop/maintainability)
@@ -53,7 +52,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
53
52
  in your `Gemfile`:
54
53
 
55
54
  ```rb
56
- gem 'rubocop', '~> 1.64', require: false
55
+ gem 'rubocop', '~> 1.65', require: false
57
56
  ```
58
57
 
59
58
  See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
data/config/default.yml CHANGED
@@ -262,6 +262,15 @@ Bundler/OrderedGems:
262
262
 
263
263
  #################### Gemspec ###############################
264
264
 
265
+ Gemspec/AddRuntimeDependency:
266
+ Description: 'Prefer `add_dependency` over `add_runtime_dependency`.'
267
+ StyleGuide: '#add_dependency_vs_add_runtime_dependency'
268
+ Reference: https://github.com/rubygems/rubygems/issues/7799#issuecomment-2192720316
269
+ Enabled: pending
270
+ VersionAdded: '1.65'
271
+ Include:
272
+ - '**/*.gemspec'
273
+
265
274
  Gemspec/DependencyVersion:
266
275
  Description: 'Requires or forbids specifying gem dependency versions.'
267
276
  Enabled: false
@@ -2617,8 +2626,9 @@ Metrics/BlockNesting:
2617
2626
  StyleGuide: '#three-is-the-number-thou-shalt-count'
2618
2627
  Enabled: true
2619
2628
  VersionAdded: '0.25'
2620
- VersionChanged: '0.47'
2629
+ VersionChanged: '1.65'
2621
2630
  CountBlocks: false
2631
+ CountModifierForms: false
2622
2632
  Max: 3
2623
2633
 
2624
2634
  Metrics/ClassLength:
@@ -164,7 +164,7 @@ module RuboCop
164
164
  # searches will go past this directory.
165
165
  # @deprecated Use `RuboCop::ConfigFinder.project_root` instead.
166
166
  def project_root
167
- warn Rainbow(<<~WARNING).yellow
167
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
168
168
  `RuboCop::ConfigLoader.project_root` is deprecated and will be removed in RuboCop 2.0. \
169
169
  Use `RuboCop::ConfigFinder.project_root` instead.
170
170
  WARNING
@@ -6,7 +6,7 @@ require 'yaml'
6
6
  module RuboCop
7
7
  # A help class for ConfigLoader that handles configuration resolution.
8
8
  # @api private
9
- class ConfigLoaderResolver
9
+ class ConfigLoaderResolver # rubocop:disable Metrics/ClassLength
10
10
  def resolve_requires(path, hash)
11
11
  config_dir = File.dirname(path)
12
12
  hash.delete('require').tap do |loaded_features|
@@ -267,8 +267,14 @@ module RuboCop
267
267
 
268
268
  def gem_config_path(gem_name, relative_config_path)
269
269
  if defined?(Bundler)
270
- gem = Bundler.load.specs[gem_name].first
271
- gem_path = gem.full_gem_path if gem
270
+ begin
271
+ gem = Bundler.load.specs[gem_name].first
272
+ gem_path = gem.full_gem_path if gem
273
+ rescue Bundler::GemfileNotFound
274
+ # No Gemfile found. Bundler may be loaded manually
275
+ rescue Bundler::GitError
276
+ # The Gemfile exists but contains an uninstalled git source
277
+ end
272
278
  end
273
279
 
274
280
  gem_path ||= Gem::Specification.find_by_name(gem_name).gem_dir
@@ -37,16 +37,28 @@ module RuboCop
37
37
 
38
38
  # @deprecated Use Registry.global
39
39
  def self.registry
40
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
41
+ `Cop.registry` is deprecated. Use `Registry.global` instead.
42
+ WARNING
43
+
40
44
  Registry.global
41
45
  end
42
46
 
43
47
  # @deprecated Use Registry.all
44
48
  def self.all
49
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
50
+ `Cop.all` is deprecated. Use `Registry.all` instead.
51
+ WARNING
52
+
45
53
  Registry.all
46
54
  end
47
55
 
48
56
  # @deprecated Use Registry.qualified_cop_name
49
57
  def self.qualified_cop_name(name, origin)
58
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
59
+ `Cop.qualified_cop_name` is deprecated. Use `Registry.qualified_cop_name` instead.
60
+ WARNING
61
+
50
62
  Registry.qualified_cop_name(name, origin)
51
63
  end
52
64
 
@@ -74,13 +86,19 @@ module RuboCop
74
86
 
75
87
  # @deprecated Use class method
76
88
  def support_autocorrect?
77
- # warn 'deprecated, use cop.class.support_autocorrect?' TODO
89
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
90
+ `support_autocorrect?` is deprecated. Use `cop.class.support_autocorrect?`.
91
+ WARNING
92
+
78
93
  self.class.support_autocorrect?
79
94
  end
80
95
 
81
96
  # @deprecated
82
97
  def corrections
83
- # warn 'Cop#corrections is deprecated' TODO
98
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
99
+ `Cop#corrections` is deprecated.
100
+ WARNING
101
+
84
102
  return [] unless @last_corrector
85
103
 
86
104
  Legacy::CorrectionsProxy.new(@last_corrector)
@@ -4,6 +4,16 @@ module RuboCop
4
4
  module Cop
5
5
  # A scaffold for concrete forces.
6
6
  class Force
7
+ # @api private
8
+ class HookError < StandardError
9
+ attr_reader :joining_cop
10
+
11
+ def initialize(joining_cop)
12
+ super
13
+ @joining_cop = joining_cop
14
+ end
15
+ end
16
+
7
17
  attr_reader :cops
8
18
 
9
19
  def self.all
@@ -32,6 +42,8 @@ module RuboCop
32
42
  next unless cop.respond_to?(method_name)
33
43
 
34
44
  cop.public_send(method_name, *args)
45
+ rescue StandardError
46
+ raise HookError, cop
35
47
  end
36
48
  end
37
49
 
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Gemspec
6
+ # Prefer `add_dependency` over `add_runtime_dependency` as the latter is
7
+ # considered soft-deprecated.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # Gem::Specification.new do |spec|
13
+ # spec.add_runtime_dependency('rubocop')
14
+ # end
15
+ #
16
+ # # good
17
+ # Gem::Specification.new do |spec|
18
+ # spec.add_dependency('rubocop')
19
+ # end
20
+ #
21
+ class AddRuntimeDependency < Base
22
+ extend AutoCorrector
23
+
24
+ MSG = 'Use `add_dependency` instead of `add_runtime_dependency`.'
25
+
26
+ RESTRICT_ON_SEND = %i[add_runtime_dependency].freeze
27
+
28
+ def on_send(node)
29
+ return if !node.receiver || node.arguments.empty?
30
+
31
+ add_offense(node.loc.selector) do |corrector|
32
+ corrector.replace(node.loc.selector, 'add_dependency')
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -31,8 +31,8 @@ module RuboCop
31
31
  #
32
32
  # # good
33
33
  # Gem::Specification.new do |spec|
34
- # spec.add_runtime_dependency('parallel', '~> 1.10')
35
- # spec.add_runtime_dependency('parser', '>= 2.3.3.1', '< 3.0')
34
+ # spec.add_dependency('parallel', '~> 1.10')
35
+ # spec.add_dependency('parser', '>= 2.3.3.1', '< 3.0')
36
36
  # end
37
37
  class DuplicatedAssignment < Base
38
38
  include RangeHelp
@@ -14,15 +14,15 @@ module RuboCop
14
14
  # # bad
15
15
  # Gem::Specification.new do |spec|
16
16
  # if RUBY_VERSION >= '3.0'
17
- # spec.add_runtime_dependency 'gem_a'
17
+ # spec.add_dependency 'gem_a'
18
18
  # else
19
- # spec.add_runtime_dependency 'gem_b'
19
+ # spec.add_dependency 'gem_b'
20
20
  # end
21
21
  # end
22
22
  #
23
23
  # # good
24
24
  # Gem::Specification.new do |spec|
25
- # spec.add_runtime_dependency 'gem_a'
25
+ # spec.add_dependency 'gem_a'
26
26
  # end
27
27
  #
28
28
  class RubyVersionGlobalsUsage < Base
@@ -121,7 +121,7 @@ module RuboCop
121
121
  return if case_node.single_line?
122
122
  return if enforced_style_end? && end_and_last_conditional_same_line?(case_node)
123
123
 
124
- case_node.each_when { |when_node| check_when(when_node, 'when') }
124
+ case_node.when_branches.each { |when_node| check_when(when_node, 'when') }
125
125
  end
126
126
 
127
127
  def on_case_match(case_match_node)
@@ -80,7 +80,7 @@ module RuboCop
80
80
  alias on_until_post on_while_post
81
81
 
82
82
  def on_case(node)
83
- node.each_when do |when_node|
83
+ node.when_branches.each do |when_node|
84
84
  last_condition = when_node.conditions.last
85
85
 
86
86
  next if !multiline_when_condition?(when_node) ||
@@ -118,8 +118,8 @@ module RuboCop
118
118
  #
119
119
  # @example EnforcedStyle: special_for_inner_method_call
120
120
  # # The first argument should normally be indented one step more than
121
- # # the preceding line, but if it's a argument for a method call that
122
- # # is itself a argument in a method call, then the inner argument
121
+ # # the preceding line, but if it's an argument for a method call that
122
+ # # is itself an argument in a method call, then the inner argument
123
123
  # # should be indented relative to the inner method.
124
124
  #
125
125
  # # good
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Checks the indentation of the here document bodies. The bodies
7
7
  # are indented one step.
8
8
  #
9
- # Note: When ``Layout/LineLength``'s `AllowHeredoc` is false (not default),
9
+ # NOTE: When ``Layout/LineLength``'s `AllowHeredoc` is false (not default),
10
10
  # this cop does not add any offenses for long here documents to
11
11
  # avoid ``Layout/LineLength``'s offenses.
12
12
  #
@@ -136,7 +136,7 @@ module RuboCop
136
136
  alias on_until on_while
137
137
 
138
138
  def on_case(case_node)
139
- case_node.each_when do |when_node|
139
+ case_node.when_branches.each do |when_node|
140
140
  check_indentation(when_node.loc.keyword, when_node.body)
141
141
  end
142
142
 
@@ -17,29 +17,29 @@ module RuboCop
17
17
  # split across lines. These include arrays, hashes, and
18
18
  # method calls with argument lists.
19
19
  #
20
- # If autocorrection is enabled, the following Layout cops
20
+ # If autocorrection is enabled, the following cops
21
21
  # are recommended to further format the broken lines.
22
22
  # (Many of these are enabled by default.)
23
23
  #
24
- # * ArgumentAlignment
25
- # * ArrayAlignment
26
- # * BlockAlignment
27
- # * BlockDelimiters
28
- # * BlockEndNewline
29
- # * ClosingParenthesisIndentation
30
- # * FirstArgumentIndentation
31
- # * FirstArrayElementIndentation
32
- # * FirstHashElementIndentation
33
- # * FirstParameterIndentation
34
- # * HashAlignment
35
- # * IndentationWidth
36
- # * MultilineArrayLineBreaks
37
- # * MultilineBlockLayout
38
- # * MultilineHashBraceLayout
39
- # * MultilineHashKeyLineBreaks
40
- # * MultilineMethodArgumentLineBreaks
41
- # * MultilineMethodParameterLineBreaks
42
- # * ParameterAlignment
24
+ # * `Layout/ArgumentAlignment`
25
+ # * `Layout/ArrayAlignment`
26
+ # * `Layout/BlockAlignment`
27
+ # * `Layout/BlockEndNewline`
28
+ # * `LayoutClosingParenthesisIndentation`
29
+ # * `LayoutFirstArgumentIndentation`
30
+ # * `LayoutFirstArrayElementIndentation`
31
+ # * `LayoutFirstHashElementIndentation`
32
+ # * `LayoutFirstParameterIndentation`
33
+ # * `LayoutHashAlignment`
34
+ # * `LayoutIndentationWidth`
35
+ # * `LayoutMultilineArrayLineBreaks`
36
+ # * `LayoutMultilineBlockLayout`
37
+ # * `LayoutMultilineHashBraceLayout`
38
+ # * `LayoutMultilineHashKeyLineBreaks`
39
+ # * `LayoutMultilineMethodArgumentLineBreaks`
40
+ # * `LayoutMultilineMethodParameterLineBreaks`
41
+ # * `Layout/ParameterAlignment`
42
+ # * `Style/BlockDelimiters`
43
43
  #
44
44
  # Together, these cops will pretty print hashes, arrays,
45
45
  # method calls, etc. For example, let's say the max columns
@@ -180,6 +180,9 @@ module RuboCop
180
180
  with_space = range_with_surrounding_space(operator)
181
181
  return if with_space.source.start_with?("\n")
182
182
 
183
+ comment = processed_source.comment_at_line(operator.line)
184
+ return if comment && with_space.last_column == comment.loc.column
185
+
183
186
  offense(type, operator, with_space, right_operand) do |msg|
184
187
  add_offense(operator, message: msg) do |corrector|
185
188
  autocorrect(corrector, with_space, right_operand)
@@ -25,8 +25,7 @@ module RuboCop
25
25
  include RangeHelp
26
26
  extend AutoCorrector
27
27
 
28
- NO_SPACE_MSG = 'Space inside string interpolation detected.'
29
- SPACE_MSG = 'Missing space inside string interpolation detected.'
28
+ MSG = '%<command>s space inside string interpolation.'
30
29
 
31
30
  def on_interpolation(begin_node)
32
31
  return if begin_node.multiline?
@@ -36,9 +35,9 @@ module RuboCop
36
35
  return if empty_brackets?(left, right, tokens: tokens)
37
36
 
38
37
  if style == :no_space
39
- no_space_offenses(begin_node, left, right, NO_SPACE_MSG)
38
+ no_space_offenses(begin_node, left, right, MSG)
40
39
  else
41
- space_offenses(begin_node, left, right, SPACE_MSG)
40
+ space_offenses(begin_node, left, right, MSG)
42
41
  end
43
42
  end
44
43
 
@@ -12,13 +12,23 @@ module RuboCop
12
12
  if corr.is_a?(CorrectionsProxy)
13
13
  merge!(corr.send(:corrector))
14
14
  else
15
- # warn "Corrector.new with corrections is deprecated." unless corr.empty? TODO
15
+ unless corr.empty?
16
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
17
+ `Corrector.new` with corrections is deprecated.
18
+ See https://docs.rubocop.org/rubocop/v1_upgrade_notes.html
19
+ WARNING
20
+ end
21
+
16
22
  corr.each { |c| corrections << c }
17
23
  end
18
24
  end
19
25
 
20
26
  def corrections
21
- # warn "#corrections is deprecated. Open an issue if you have a valid usecase." TODO
27
+ warn Rainbow(<<~WARNING).yellow, uplevel: 1
28
+ `Corrector#corrections` is deprecated. Open an issue if you have a valid usecase.
29
+ See https://docs.rubocop.org/rubocop/v1_upgrade_notes.html
30
+ WARNING
31
+
22
32
  CorrectionsProxy.new(self)
23
33
  end
24
34
  end
@@ -32,7 +32,7 @@ module RuboCop
32
32
 
33
33
  def on_case(case_node)
34
34
  case_node.when_branches.each_with_object(Set.new) do |when_node, previous|
35
- when_node.each_condition do |condition|
35
+ when_node.conditions.each do |condition|
36
36
  add_offense(condition) unless previous.add?(condition)
37
37
  end
38
38
  end
@@ -50,7 +50,7 @@ module RuboCop
50
50
  MSG = 'Avoid `when` branches without a body.'
51
51
 
52
52
  def on_case(node)
53
- node.each_when do |when_node|
53
+ node.when_branches.each do |when_node|
54
54
  next if when_node.body
55
55
  next if cop_config['AllowComments'] && contains_comments?(when_node)
56
56
 
@@ -65,17 +65,15 @@ module RuboCop
65
65
 
66
66
  minimum_target_ruby_version 2.6
67
67
 
68
- MESSAGES = [
69
- 'Passing safe_level with the 2nd argument of `ERB.new` is ' \
70
- 'deprecated. Do not use it, and specify other arguments as ' \
71
- 'keyword arguments.',
72
- 'Passing trim_mode with the 3rd argument of `ERB.new` is ' \
73
- 'deprecated. Use keyword argument like ' \
74
- '`ERB.new(str, trim_mode: %<arg_value>s)` instead.',
75
- 'Passing eoutvar with the 4th argument of `ERB.new` is ' \
76
- 'deprecated. Use keyword argument like ' \
77
- '`ERB.new(str, eoutvar: %<arg_value>s)` instead.'
78
- ].freeze
68
+ MESSAGE_SAFE_LEVEL = 'Passing safe_level with the 2nd argument of `ERB.new` is ' \
69
+ 'deprecated. Do not use it, and specify other arguments as ' \
70
+ 'keyword arguments.'
71
+ MESSAGE_TRIM_MODE = 'Passing trim_mode with the 3rd argument of `ERB.new` is ' \
72
+ 'deprecated. Use keyword argument like ' \
73
+ '`ERB.new(str, trim_mode: %<arg_value>s)` instead.'
74
+ MESSAGE_EOUTVAR = 'Passing eoutvar with the 4th argument of `ERB.new` is ' \
75
+ 'deprecated. Use keyword argument like ' \
76
+ '`ERB.new(str, eoutvar: %<arg_value>s)` instead.'
79
77
 
80
78
  RESTRICT_ON_SEND = %i[new].freeze
81
79
 
@@ -92,10 +90,8 @@ module RuboCop
92
90
  arguments[1..3].each_with_index do |argument, i|
93
91
  next if !argument || argument.hash_type?
94
92
 
95
- message = format(MESSAGES[i], arg_value: argument.source)
96
-
97
93
  add_offense(
98
- argument.source_range, message: message
94
+ argument.source_range, message: message(i, argument.source)
99
95
  ) do |corrector|
100
96
  autocorrect(corrector, node)
101
97
  end
@@ -105,6 +101,17 @@ module RuboCop
105
101
 
106
102
  private
107
103
 
104
+ def message(positional_argument_index, arg_value)
105
+ case positional_argument_index
106
+ when 0
107
+ MESSAGE_SAFE_LEVEL
108
+ when 1
109
+ format(MESSAGE_TRIM_MODE, arg_value: arg_value)
110
+ when 2
111
+ format(MESSAGE_EOUTVAR, arg_value: arg_value)
112
+ end
113
+ end
114
+
108
115
  def autocorrect(corrector, node)
109
116
  str_arg = node.first_argument.source
110
117
 
@@ -23,27 +23,34 @@ module RuboCop
23
23
  # 'Item 2'
24
24
  # ]
25
25
  class ImplicitStringConcatenation < Base
26
- MSG = 'Combine %<string1>s and %<string2>s into a single string ' \
26
+ extend AutoCorrector
27
+
28
+ MSG = 'Combine %<lhs>s and %<rhs>s into a single string ' \
27
29
  'literal, rather than using implicit string concatenation.'
28
30
  FOR_ARRAY = ' Or, if they were intended to be separate array ' \
29
31
  'elements, separate them with a comma.'
30
32
  FOR_METHOD = ' Or, if they were intended to be separate method ' \
31
33
  'arguments, separate them with a comma.'
32
34
 
35
+ # rubocop:disable Metrics/AbcSize
33
36
  def on_dstr(node)
34
- each_bad_cons(node) do |child_node1, child_node2|
35
- range = child_node1.source_range.join(child_node2.source_range)
36
- message = format(MSG,
37
- string1: display_str(child_node1),
38
- string2: display_str(child_node2))
37
+ each_bad_cons(node) do |lhs_node, rhs_node|
38
+ range = lhs_node.source_range.join(rhs_node.source_range)
39
+ message = format(MSG, lhs: display_str(lhs_node), rhs: display_str(rhs_node))
39
40
  if node.parent&.array_type?
40
41
  message << FOR_ARRAY
41
42
  elsif node.parent&.send_type?
42
43
  message << FOR_METHOD
43
44
  end
44
- add_offense(range, message: message)
45
+
46
+ add_offense(range, message: message) do |corrector|
47
+ preferred = "#{lhs_node.source} + #{rhs_node.source}"
48
+
49
+ corrector.replace(range, preferred)
50
+ end
45
51
  end
46
52
  end
53
+ # rubocop:enable Metrics/AbcSize
47
54
 
48
55
  private
49
56
 
@@ -59,7 +59,7 @@ module RuboCop
59
59
  if case_node.condition
60
60
  check_case(case_node)
61
61
  else
62
- case_node.each_when do |when_node|
62
+ case_node.when_branches.each do |when_node|
63
63
  next unless when_node.conditions.all?(&:literal?)
64
64
 
65
65
  range = when_conditions_range(when_node)
@@ -102,7 +102,7 @@ module RuboCop
102
102
 
103
103
  def on_def(node)
104
104
  subject, = *node
105
- return if node.defs_type? && subject.lvar_type?
105
+ return if node.defs_type? && subject.variable?
106
106
 
107
107
  def_ancestor = node.each_ancestor(:def, :defs).first
108
108
  return unless def_ancestor
@@ -49,15 +49,8 @@ module RuboCop
49
49
  return unless def_node
50
50
 
51
51
  enum_conversion_call?(node) do |method_node, arguments|
52
- next if method_node.call_type? &&
53
- !method_node.method?(:__method__) && !method_node.method?(:__callee__)
54
-
55
- valid = if method_name?(method_node, def_node.method_name)
56
- arguments_match?(arguments, def_node)
57
- else
58
- def_node.arguments.empty?
59
- end
60
- return if valid
52
+ next if !method_name?(method_node, def_node.method_name) ||
53
+ arguments_match?(arguments, def_node)
61
54
 
62
55
  add_offense(node)
63
56
  end
@@ -113,6 +113,7 @@ module RuboCop
113
113
  PATTERN
114
114
 
115
115
  def on_block(node)
116
+ return unless node.body
116
117
  return unless reduce_with_block?(node)
117
118
  return unless node.argument_list.length >= 2
118
119
 
@@ -111,6 +111,8 @@ module RuboCop
111
111
  end
112
112
 
113
113
  def check_expression(expr)
114
+ expr = expr.body if expr.if_type? && expr.modifier_form?
115
+
114
116
  check_literal(expr)
115
117
  check_var(expr)
116
118
  check_self(expr)
@@ -121,6 +123,7 @@ module RuboCop
121
123
  end
122
124
 
123
125
  def check_void_op(node, &block)
126
+ node = node.children.first while node.begin_type?
124
127
  return unless node.send_type? && OPERATORS.include?(node.method_name)
125
128
  return if block && yield(node)
126
129
 
@@ -211,6 +214,8 @@ module RuboCop
211
214
  end
212
215
 
213
216
  def autocorrect_void_expression(corrector, node)
217
+ return if node.parent.if_type? && node.parent.modifier_form?
218
+
214
219
  corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
215
220
  end
216
221