rubocop-rails 2.15.2 → 2.17.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +122 -2
  3. data/config/obsoletion.yml +10 -0
  4. data/lib/rubocop/cop/mixin/active_record_helper.rb +3 -6
  5. data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +1 -3
  6. data/lib/rubocop/cop/mixin/index_method.rb +6 -16
  7. data/lib/rubocop/cop/mixin/migrations_helper.rb +1 -1
  8. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +104 -0
  9. data/lib/rubocop/cop/rails/action_controller_test_case.rb +2 -2
  10. data/lib/rubocop/cop/rails/action_filter.rb +1 -1
  11. data/lib/rubocop/cop/rails/action_order.rb +117 -0
  12. data/lib/rubocop/cop/rails/active_record_aliases.rb +1 -4
  13. data/lib/rubocop/cop/rails/active_record_override.rb +2 -5
  14. data/lib/rubocop/cop/rails/active_support_on_load.rb +70 -0
  15. data/lib/rubocop/cop/rails/add_column_index.rb +1 -4
  16. data/lib/rubocop/cop/rails/application_controller.rb +1 -1
  17. data/lib/rubocop/cop/rails/application_job.rb +1 -1
  18. data/lib/rubocop/cop/rails/application_mailer.rb +1 -1
  19. data/lib/rubocop/cop/rails/application_record.rb +1 -1
  20. data/lib/rubocop/cop/rails/blank.rb +1 -2
  21. data/lib/rubocop/cop/rails/bulk_change_table.rb +6 -20
  22. data/lib/rubocop/cop/rails/compact_blank.rb +5 -1
  23. data/lib/rubocop/cop/rails/content_tag.rb +4 -5
  24. data/lib/rubocop/cop/rails/date.rb +4 -9
  25. data/lib/rubocop/cop/rails/delegate.rb +2 -5
  26. data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +17 -13
  27. data/lib/rubocop/cop/rails/dot_separated_keys.rb +2 -2
  28. data/lib/rubocop/cop/rails/duration_arithmetic.rb +2 -2
  29. data/lib/rubocop/cop/rails/dynamic_find_by.rb +24 -12
  30. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +4 -0
  31. data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -5
  32. data/lib/rubocop/cop/rails/environment_comparison.rb +1 -2
  33. data/lib/rubocop/cop/rails/file_path.rb +5 -7
  34. data/lib/rubocop/cop/rails/find_each.rb +8 -2
  35. data/lib/rubocop/cop/rails/freeze_time.rb +76 -0
  36. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +2 -4
  37. data/lib/rubocop/cop/rails/helper_instance_variable.rb +1 -1
  38. data/lib/rubocop/cop/rails/http_positional_arguments.rb +4 -9
  39. data/lib/rubocop/cop/rails/http_status.rb +5 -10
  40. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +2 -0
  41. data/lib/rubocop/cop/rails/ignored_columns_assignment.rb +50 -0
  42. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +3 -10
  43. data/lib/rubocop/cop/rails/index_by.rb +1 -1
  44. data/lib/rubocop/cop/rails/index_with.rb +1 -1
  45. data/lib/rubocop/cop/rails/inverse_of.rb +3 -6
  46. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +2 -6
  47. data/lib/rubocop/cop/rails/link_to_blank.rb +1 -4
  48. data/lib/rubocop/cop/rails/mailer_name.rb +3 -3
  49. data/lib/rubocop/cop/rails/migration_class_name.rb +1 -1
  50. data/lib/rubocop/cop/rails/output.rb +3 -6
  51. data/lib/rubocop/cop/rails/pluck.rb +32 -12
  52. data/lib/rubocop/cop/rails/pluralization_grammar.rb +1 -2
  53. data/lib/rubocop/cop/rails/presence.rb +21 -12
  54. data/lib/rubocop/cop/rails/present.rb +3 -6
  55. data/lib/rubocop/cop/rails/rake_environment.rb +1 -1
  56. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +2 -4
  57. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +1 -1
  58. data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +3 -3
  59. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +30 -26
  60. data/lib/rubocop/cop/rails/reflection_class_name.rb +17 -0
  61. data/lib/rubocop/cop/rails/refute_methods.rb +1 -5
  62. data/lib/rubocop/cop/rails/relative_date_constant.rb +2 -5
  63. data/lib/rubocop/cop/rails/request_referer.rb +1 -2
  64. data/lib/rubocop/cop/rails/require_dependency.rb +1 -1
  65. data/lib/rubocop/cop/rails/reversible_migration.rb +10 -33
  66. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +1 -2
  67. data/lib/rubocop/cop/rails/root_pathname_methods.rb +225 -0
  68. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +1 -3
  69. data/lib/rubocop/cop/rails/save_bang.rb +10 -22
  70. data/lib/rubocop/cop/rails/short_i18n.rb +2 -5
  71. data/lib/rubocop/cop/rails/skips_model_validations.rb +2 -3
  72. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +1 -5
  73. data/lib/rubocop/cop/rails/time_zone.rb +10 -21
  74. data/lib/rubocop/cop/rails/time_zone_assignment.rb +1 -1
  75. data/lib/rubocop/cop/rails/to_s_with_argument.rb +78 -0
  76. data/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb +49 -0
  77. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -6
  78. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +1 -3
  79. data/lib/rubocop/cop/rails/unknown_env.rb +2 -4
  80. data/lib/rubocop/cop/rails/validation.rb +4 -12
  81. data/lib/rubocop/cop/rails/where_missing.rb +111 -0
  82. data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
  83. data/lib/rubocop/cop/rails_cops.rb +10 -0
  84. data/lib/rubocop/rails/version.rb +1 -1
  85. data/lib/rubocop-rails.rb +10 -0
  86. metadata +16 -8
  87. data/bin/console +0 -11
  88. data/bin/setup +0 -7
@@ -37,7 +37,7 @@ module RuboCop
37
37
  RESTRICT_ON_SEND = %i[has_many has_one].freeze
38
38
 
39
39
  def_node_search :active_resource_class?, <<~PATTERN
40
- (const (const nil? :ActiveResource) :Base)
40
+ (const (const {nil? cbase} :ActiveResource) :Base)
41
41
  PATTERN
42
42
 
43
43
  def_node_matcher :association_without_options?, <<~PATTERN
@@ -105,9 +105,7 @@ module RuboCop
105
105
 
106
106
  return false unless node.parent
107
107
 
108
- return true if contain_valid_options_in_with_options_block?(
109
- node.parent.parent
110
- )
108
+ return true if contain_valid_options_in_with_options_block?(node.parent.parent)
111
109
  end
112
110
 
113
111
  false
@@ -37,7 +37,7 @@ module RuboCop
37
37
  def_node_matcher :form_builder_class?, <<~PATTERN
38
38
  (const
39
39
  (const
40
- (const nil? :ActionView) :Helpers) :FormBuilder)
40
+ (const {nil? cbase} :ActionView) :Helpers) :FormBuilder)
41
41
  PATTERN
42
42
 
43
43
  def on_ivar(node)
@@ -22,11 +22,8 @@ module RuboCop
22
22
  extend AutoCorrector
23
23
  extend TargetRailsVersion
24
24
 
25
- MSG = 'Use keyword arguments instead of ' \
26
- 'positional arguments for http call: `%<verb>s`.'
27
- KEYWORD_ARGS = %i[
28
- method params session body flash xhr as headers env to
29
- ].freeze
25
+ MSG = 'Use keyword arguments instead of positional arguments for http call: `%<verb>s`.'
26
+ KEYWORD_ARGS = %i[method params session body flash xhr as headers env to].freeze
30
27
  ROUTING_METHODS = %i[draw routes].freeze
31
28
  RESTRICT_ON_SEND = %i[get post put patch delete head].freeze
32
29
 
@@ -75,8 +72,7 @@ module RuboCop
75
72
  return false if kwsplat_hash?(data)
76
73
 
77
74
  data.each_pair.none? do |pair|
78
- special_keyword_arg?(pair.key) ||
79
- (format_arg?(pair.key) && data.pairs.one?)
75
+ special_keyword_arg?(pair.key) || (format_arg?(pair.key) && data.pairs.one?)
80
76
  end
81
77
  end
82
78
 
@@ -98,8 +94,7 @@ module RuboCop
98
94
  return '' if data.hash_type? && data.empty?
99
95
 
100
96
  hash_data = if data.hash_type?
101
- format('{ %<data>s }',
102
- data: data.pairs.map(&:source).join(', '))
97
+ format('{ %<data>s }', data: data.pairs.map(&:source).join(', '))
103
98
  else
104
99
  # user supplies an object,
105
100
  # no need to surround with braces
@@ -84,10 +84,8 @@ module RuboCop
84
84
 
85
85
  # :nodoc:
86
86
  class SymbolicStyleChecker
87
- MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
88
- 'to define HTTP status code.'
89
- DEFAULT_MSG = 'Prefer `symbolic` over `numeric` ' \
90
- 'to define HTTP status code.'
87
+ MSG = 'Prefer `%<prefer>s` over `%<current>s` to define HTTP status code.'
88
+ DEFAULT_MSG = 'Prefer `symbolic` over `numeric` to define HTTP status code.'
91
89
 
92
90
  attr_reader :node
93
91
 
@@ -118,17 +116,14 @@ module RuboCop
118
116
  end
119
117
 
120
118
  def custom_http_status_code?
121
- node.int_type? &&
122
- !::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(number)
119
+ node.int_type? && !::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(number)
123
120
  end
124
121
  end
125
122
 
126
123
  # :nodoc:
127
124
  class NumericStyleChecker
128
- MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
129
- 'to define HTTP status code.'
130
- DEFAULT_MSG = 'Prefer `numeric` over `symbolic` ' \
131
- 'to define HTTP status code.'
125
+ MSG = 'Prefer `%<prefer>s` over `%<current>s` to define HTTP status code.'
126
+ DEFAULT_MSG = 'Prefer `numeric` over `symbolic` to define HTTP status code.'
132
127
  PERMITTED_STATUS = %i[error success missing redirect].freeze
133
128
 
134
129
  attr_reader :node
@@ -34,6 +34,8 @@ module RuboCop
34
34
 
35
35
  MSG = 'Use "lazy" lookup for the text used in controllers.'
36
36
 
37
+ RESTRICT_ON_SEND = %i[translate t].freeze
38
+
37
39
  def_node_matcher :translate_call?, <<~PATTERN
38
40
  (send nil? {:translate :t} ${sym_type? str_type?} ...)
39
41
  PATTERN
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Looks for assignments of `ignored_columns` that may override previous
7
+ # assignments.
8
+ #
9
+ # Overwriting previous assignments is usually a mistake, since it will
10
+ # un-ignore the first set of columns. Since duplicate column names is not
11
+ # a problem, it is better to simply append to the list.
12
+ #
13
+ # @example
14
+ #
15
+ # # bad
16
+ # class User < ActiveRecord::Base
17
+ # self.ignored_columns = [:one]
18
+ # end
19
+ #
20
+ # # bad
21
+ # class User < ActiveRecord::Base
22
+ # self.ignored_columns = [:one, :two]
23
+ # end
24
+ #
25
+ # # good
26
+ # class User < ActiveRecord::Base
27
+ # self.ignored_columns += [:one, :two]
28
+ # end
29
+ #
30
+ # # good
31
+ # class User < ActiveRecord::Base
32
+ # self.ignored_columns += [:one]
33
+ # self.ignored_columns += [:two]
34
+ # end
35
+ #
36
+ class IgnoredColumnsAssignment < Base
37
+ extend AutoCorrector
38
+
39
+ MSG = 'Use `+=` instead of `=`.'
40
+ RESTRICT_ON_SEND = %i[ignored_columns=].freeze
41
+
42
+ def on_send(node)
43
+ add_offense(node.loc.operator) do |corrector|
44
+ corrector.replace(node.loc.operator, '+=')
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -42,12 +42,7 @@ module RuboCop
42
42
  `%<ignore>s` option will be ignored when `%<prefer>s` and `%<ignore>s` are used together.
43
43
  MSG
44
44
 
45
- RESTRICT_ON_SEND = %i[
46
- skip_after_action
47
- skip_around_action
48
- skip_before_action
49
- skip_action_callback
50
- ].freeze
45
+ RESTRICT_ON_SEND = %i[skip_after_action skip_around_action skip_before_action skip_action_callback].freeze
51
46
 
52
47
  FILTERS = RESTRICT_ON_SEND.map { |method_name| ":#{method_name}" }
53
48
 
@@ -67,11 +62,9 @@ module RuboCop
67
62
  options = options_hash(options)
68
63
 
69
64
  if if_and_only?(options)
70
- add_offense(options[:if],
71
- message: format(MSG, prefer: :only, ignore: :if))
65
+ add_offense(options[:if], message: format(MSG, prefer: :only, ignore: :if))
72
66
  elsif if_and_except?(options)
73
- add_offense(options[:except],
74
- message: format(MSG, prefer: :if, ignore: :except))
67
+ add_offense(options[:except], message: format(MSG, prefer: :if, ignore: :except))
75
68
  end
76
69
  end
77
70
 
@@ -46,7 +46,7 @@ module RuboCop
46
46
 
47
47
  def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
48
48
  (send
49
- (const _ :Hash)
49
+ (const {nil? cbase} :Hash)
50
50
  :[]
51
51
  (block
52
52
  (call _ {:map :collect})
@@ -49,7 +49,7 @@ module RuboCop
49
49
 
50
50
  def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
51
51
  (send
52
- (const _ :Hash)
52
+ (const {nil? cbase} :Hash)
53
53
  :[]
54
54
  (block
55
55
  (call _ {:map :collect})
@@ -192,8 +192,7 @@ module RuboCop
192
192
  end
193
193
  return if options_ignoring_inverse_of?(options)
194
194
 
195
- return unless scope?(arguments) ||
196
- options_requiring_inverse_of?(options)
195
+ return unless scope?(arguments) || options_requiring_inverse_of?(options)
197
196
 
198
197
  return if options_contain_inverse_of?(options)
199
198
 
@@ -206,8 +205,7 @@ module RuboCop
206
205
 
207
206
  def options_requiring_inverse_of?(options)
208
207
  required = options.any? do |opt|
209
- conditions_option?(opt) ||
210
- foreign_key_option?(opt)
208
+ conditions_option?(opt) || foreign_key_option?(opt)
211
209
  end
212
210
 
213
211
  return required if target_rails_version >= 5.2
@@ -227,8 +225,7 @@ module RuboCop
227
225
 
228
226
  def with_options_arguments(recv, node)
229
227
  blocks = node.each_ancestor(:block).select do |block|
230
- block.send_node.command?(:with_options) &&
231
- same_context_in_with_options?(block.arguments.first, recv)
228
+ block.send_node.command?(:with_options) && same_context_in_with_options?(block.arguments.first, recv)
232
229
  end
233
230
  blocks.flat_map { |n| n.send_node.arguments }
234
231
  end
@@ -184,13 +184,9 @@ module RuboCop
184
184
  # @return [String]
185
185
  def message(methods, parent)
186
186
  if methods.size == 1
187
- format(MSG,
188
- action: "`#{methods[0]}` is",
189
- type: parent.type)
187
+ format(MSG, action: "`#{methods[0]}` is", type: parent.type)
190
188
  else
191
- format(MSG,
192
- action: "`#{methods.join('`, `')}` are",
193
- type: parent.type)
189
+ format(MSG, action: "`#{methods.join('`, `')}` are", type: parent.type)
194
190
  end
195
191
  end
196
192
  end
@@ -68,10 +68,7 @@ module RuboCop
68
68
 
69
69
  def append_to_rel(rel_node, corrector)
70
70
  existing_rel = rel_node.children.last.value
71
- str_range = rel_node.children.last.loc.expression.adjust(
72
- begin_pos: 1,
73
- end_pos: -1
74
- )
71
+ str_range = rel_node.children.last.loc.expression.adjust(begin_pos: 1, end_pos: -1)
75
72
  corrector.replace(str_range, "#{existing_rel} noopener")
76
73
  end
77
74
 
@@ -34,8 +34,8 @@ module RuboCop
34
34
 
35
35
  def_node_matcher :mailer_base_class?, <<~PATTERN
36
36
  {
37
- (const (const nil? :ActionMailer) :Base)
38
- (const nil? :ApplicationMailer)
37
+ (const (const {nil? cbase} :ActionMailer) :Base)
38
+ (const {nil? cbase} :ApplicationMailer)
39
39
  }
40
40
  PATTERN
41
41
 
@@ -44,7 +44,7 @@ module RuboCop
44
44
  PATTERN
45
45
 
46
46
  def_node_matcher :class_new_definition?, <<~PATTERN
47
- (send (const nil? :Class) :new #mailer_base_class?)
47
+ (send (const {nil? cbase} :Class) :new #mailer_base_class?)
48
48
  PATTERN
49
49
 
50
50
  def on_class(node)
@@ -29,7 +29,7 @@ module RuboCop
29
29
 
30
30
  basename = basename_without_timestamp_and_suffix(processed_source.file_path)
31
31
 
32
- class_identifier = node.identifier
32
+ class_identifier = node.identifier.location.name
33
33
  camelized_basename = camelize(basename)
34
34
  return if class_identifier.source.casecmp(camelized_basename).zero?
35
35
 
@@ -21,11 +21,8 @@ module RuboCop
21
21
  include RangeHelp
22
22
  extend AutoCorrector
23
23
 
24
- MSG = 'Do not write to stdout. ' \
25
- "Use Rails's logger if you want to log."
26
- RESTRICT_ON_SEND = %i[
27
- ap p pp pretty_print print puts binwrite syswrite write write_nonblock
28
- ].freeze
24
+ MSG = "Do not write to stdout. Use Rails's logger if you want to log."
25
+ RESTRICT_ON_SEND = %i[ap p pp pretty_print print puts binwrite syswrite write write_nonblock].freeze
29
26
 
30
27
  def_node_matcher :output?, <<~PATTERN
31
28
  (send nil? {:ap :p :pp :pretty_print :print :puts} ...)
@@ -35,7 +32,7 @@ module RuboCop
35
32
  (send
36
33
  {
37
34
  (gvar #match_gvar?)
38
- {(const nil? :STDOUT) (const nil? :STDERR)}
35
+ (const {nil? cbase} {:STDOUT :STDERR})
39
36
  }
40
37
  {:binwrite :syswrite :write :write_nonblock}
41
38
  ...)
@@ -21,42 +21,62 @@ module RuboCop
21
21
  extend AutoCorrector
22
22
  extend TargetRailsVersion
23
23
 
24
- MSG = 'Prefer `pluck(:%<value>s)` over `%<current>s`.'
24
+ MSG = 'Prefer `%<replacement>s` over `%<current>s`.'
25
25
 
26
26
  minimum_target_rails_version 5.0
27
27
 
28
28
  def_node_matcher :pluck_candidate?, <<~PATTERN
29
- ({block numblock} (send _ {:map :collect}) $_argument (send (lvar $_element) :[] (sym $_value)))
29
+ ({block numblock} (send _ {:map :collect}) $_argument (send lvar :[] $_key))
30
30
  PATTERN
31
31
 
32
32
  def on_block(node)
33
- pluck_candidate?(node) do |argument, element, value|
33
+ pluck_candidate?(node) do |argument, key|
34
+ next unless use_one_block_argument?(argument)
35
+
34
36
  match = if node.block_type?
35
- argument.children.first.source.to_sym == element
37
+ block_argument = argument.children.first.source
38
+ use_block_argument_in_key?(block_argument, key)
36
39
  else # numblock
37
- argument == 1 && element == :_1
40
+ argument == 1 && use_block_argument_in_key?('_1', key)
38
41
  end
39
42
  next unless match
40
43
 
41
- message = message(value, node)
42
-
43
- add_offense(offense_range(node), message: message) do |corrector|
44
- corrector.replace(offense_range(node), "pluck(:#{value})")
45
- end
44
+ register_offense(node, key)
46
45
  end
47
46
  end
48
47
  alias on_numblock on_block
49
48
 
50
49
  private
51
50
 
51
+ def use_one_block_argument?(argument)
52
+ return true if argument == 1 # Checks for numbered argument `_1`.
53
+
54
+ argument.respond_to?(:one?) && argument.one?
55
+ end
56
+
57
+ def use_block_argument_in_key?(block_argument, key)
58
+ return false if block_argument == key.source
59
+
60
+ key.each_descendant(:lvar).none? { |lvar| block_argument == lvar.source }
61
+ end
62
+
52
63
  def offense_range(node)
53
64
  node.send_node.loc.selector.join(node.loc.end)
54
65
  end
55
66
 
56
- def message(value, node)
67
+ def register_offense(node, key)
68
+ replacement = "pluck(#{key.source})"
69
+ message = message(replacement, node)
70
+
71
+ add_offense(offense_range(node), message: message) do |corrector|
72
+ corrector.replace(offense_range(node), replacement)
73
+ end
74
+ end
75
+
76
+ def message(replacement, node)
57
77
  current = offense_range(node).source
58
78
 
59
- format(MSG, value: value, current: current)
79
+ format(MSG, replacement: replacement, current: current)
60
80
  end
61
81
  end
62
82
  end
@@ -94,8 +94,7 @@ module RuboCop
94
94
  end
95
95
 
96
96
  def duration_method?(method_name)
97
- SINGULAR_DURATION_METHODS.key?(method_name) ||
98
- PLURAL_DURATION_METHODS.key?(method_name)
97
+ SINGULAR_DURATION_METHODS.key?(method_name) || PLURAL_DURATION_METHODS.key?(method_name)
99
98
  end
100
99
  end
101
100
  end
@@ -93,7 +93,7 @@ module RuboCop
93
93
 
94
94
  def register_offense(node, receiver, other)
95
95
  add_offense(node, message: message(node, receiver, other)) do |corrector|
96
- corrector.replace(node.source_range, replacement(receiver, other))
96
+ corrector.replace(node.source_range, replacement(receiver, other, node.left_sibling))
97
97
  end
98
98
  end
99
99
 
@@ -106,12 +106,20 @@ module RuboCop
106
106
  end
107
107
 
108
108
  def message(node, receiver, other)
109
- format(MSG,
110
- prefer: replacement(receiver, other),
111
- current: node.source)
109
+ prefer = replacement(receiver, other, node.left_sibling).gsub(/^\s*|\n/, '')
110
+ current = current(node).gsub(/^\s*|\n/, '')
111
+ format(MSG, prefer: prefer, current: current)
112
112
  end
113
113
 
114
- def replacement(receiver, other)
114
+ def current(node)
115
+ if node.source.include?("\n")
116
+ "#{node.loc.keyword.with(end_pos: node.condition.loc.selector.end_pos).source} ... end"
117
+ else
118
+ node.source
119
+ end
120
+ end
121
+
122
+ def replacement(receiver, other, left_sibling)
115
123
  or_source = if other&.send_type?
116
124
  build_source_for_or_method(other)
117
125
  elsif other.nil? || other.nil_type?
@@ -120,23 +128,24 @@ module RuboCop
120
128
  " || #{other.source}"
121
129
  end
122
130
 
123
- "#{receiver.source}.presence" + or_source
131
+ replaced = "#{receiver.source}.presence#{or_source}"
132
+ left_sibling ? "(#{replaced})" : replaced
124
133
  end
125
134
 
126
135
  def build_source_for_or_method(other)
127
- if other.parenthesized? || other.method?('[]') || !other.arguments?
136
+ if other.parenthesized? || other.method?('[]') || other.arithmetic_operation? || !other.arguments?
128
137
  " || #{other.source}"
129
138
  else
130
- method = range_between(
131
- other.source_range.begin_pos,
132
- other.first_argument.source_range.begin_pos - 1
133
- ).source
134
-
139
+ method = method_range(other).source
135
140
  arguments = other.arguments.map(&:source).join(', ')
136
141
 
137
142
  " || #{method}(#{arguments})"
138
143
  end
139
144
  end
145
+
146
+ def method_range(node)
147
+ range_between(node.source_range.begin_pos, node.first_argument.source_range.begin_pos - 1)
148
+ end
140
149
  end
141
150
  end
142
151
  end
@@ -47,10 +47,8 @@ module RuboCop
47
47
  extend AutoCorrector
48
48
 
49
49
  MSG_NOT_BLANK = 'Use `%<prefer>s` instead of `%<current>s`.'
50
- MSG_EXISTS_AND_NOT_EMPTY = 'Use `%<prefer>s` instead of ' \
51
- '`%<current>s`.'
52
- MSG_UNLESS_BLANK = 'Use `if %<prefer>s` instead of ' \
53
- '`%<current>s`.'
50
+ MSG_EXISTS_AND_NOT_EMPTY = 'Use `%<prefer>s` instead of `%<current>s`.'
51
+ MSG_UNLESS_BLANK = 'Use `if %<prefer>s` instead of `%<current>s`.'
54
52
  RESTRICT_ON_SEND = %i[!].freeze
55
53
 
56
54
  def_node_matcher :exists_and_not_empty?, <<~PATTERN
@@ -118,8 +116,7 @@ module RuboCop
118
116
 
119
117
  unless_blank?(node) do |method_call, receiver|
120
118
  range = unless_condition(node, method_call)
121
- msg = format(MSG_UNLESS_BLANK, prefer: replacement(receiver),
122
- current: range.source)
119
+ msg = format(MSG_UNLESS_BLANK, prefer: replacement(receiver), current: range.source)
123
120
  add_offense(range, message: msg) do |corrector|
124
121
  autocorrect(corrector, node)
125
122
  end
@@ -39,7 +39,7 @@ module RuboCop
39
39
  (block $(send nil? :task ...) ...)
40
40
  PATTERN
41
41
 
42
- def on_block(node)
42
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
43
43
  task_definition?(node) do |task_method|
44
44
  return if task_name(task_method) == :default
45
45
  return if with_dependencies?(task_method)
@@ -30,11 +30,9 @@ module RuboCop
30
30
  include RangeHelp
31
31
  extend AutoCorrector
32
32
 
33
- MSG_SAME =
34
- '`allow_nil` is redundant when `allow_blank` has the same value.'
33
+ MSG_SAME = '`allow_nil` is redundant when `allow_blank` has the same value.'
35
34
 
36
- MSG_ALLOW_NIL_FALSE =
37
- '`allow_nil: false` is redundant when `allow_blank` is true.'
35
+ MSG_ALLOW_NIL_FALSE = '`allow_nil: false` is redundant when `allow_blank` is true.'
38
36
 
39
37
  RESTRICT_ON_SEND = %i[validates].freeze
40
38
 
@@ -41,7 +41,7 @@ module RuboCop
41
41
  association_with_foreign_key(node) do |type, name, options, foreign_key_pair, foreign_key|
42
42
  if redundant?(node, type, name, options, foreign_key)
43
43
  add_offense(foreign_key_pair.loc.expression) do |corrector|
44
- range = range_with_surrounding_space(range: foreign_key_pair.source_range, side: :left)
44
+ range = range_with_surrounding_space(foreign_key_pair.source_range, side: :left)
45
45
  range = range_with_surrounding_comma(range, :left)
46
46
 
47
47
  corrector.remove(range)
@@ -143,7 +143,7 @@ module RuboCop
143
143
  def_node_matcher :belongs_to_without_fk?, <<~PATTERN
144
144
  {
145
145
  (send nil? :belongs_to (sym %1)) # belongs_to :user
146
- (send nil? :belongs_to (sym %1) !hash) # belongs_to :user, -> { not_deleted }
146
+ (send nil? :belongs_to (sym %1) !hash ...) # belongs_to :user, -> { not_deleted }
147
147
  (send nil? :belongs_to (sym %1) !(hash <(pair (sym :foreign_key) _) ...>))
148
148
  }
149
149
  PATTERN
@@ -217,7 +217,7 @@ module RuboCop
217
217
  keys.each do |key|
218
218
  key_node = node.arguments.find { |arg| arg.value == key }
219
219
  key_range = range_with_surrounding_space(
220
- range: range_with_surrounding_comma(key_node.source_range, :right),
220
+ range_with_surrounding_comma(key_node.source_range, :right),
221
221
  side: :right
222
222
  )
223
223
  corrector.remove(key_range)
@@ -226,7 +226,7 @@ module RuboCop
226
226
 
227
227
  def remove_presence_option(corrector, presence)
228
228
  range = range_with_surrounding_comma(
229
- range_with_surrounding_space(range: presence.source_range, side: :left),
229
+ range_with_surrounding_space(presence.source_range, side: :left),
230
230
  :left
231
231
  )
232
232
  corrector.remove(range)
@@ -60,15 +60,6 @@ module RuboCop
60
60
 
61
61
  MSG = 'Redundant receiver in `with_options`.'
62
62
 
63
- def_node_matcher :with_options?, <<~PATTERN
64
- (block
65
- (send nil? :with_options
66
- (...))
67
- (args
68
- $_arg)
69
- $_body)
70
- PATTERN
71
-
72
63
  def_node_search :all_block_nodes_in, <<~PATTERN
73
64
  (block ...)
74
65
  PATTERN
@@ -78,29 +69,42 @@ module RuboCop
78
69
  PATTERN
79
70
 
80
71
  def on_block(node)
81
- with_options?(node) do |arg, body|
82
- return if body.nil?
83
- return unless all_block_nodes_in(body).count.zero?
84
-
85
- send_nodes = all_send_nodes_in(body)
86
-
87
- if send_nodes.all? { |n| same_value?(arg, n.receiver) }
88
- send_nodes.each do |send_node|
89
- receiver = send_node.receiver
90
- add_offense(receiver.source_range) do |corrector|
91
- autocorrect(corrector, send_node)
92
- end
93
- end
72
+ return unless node.method?(:with_options)
73
+ return unless (body = node.body)
74
+ return unless all_block_nodes_in(body).count.zero?
75
+
76
+ send_nodes = all_send_nodes_in(body)
77
+ return unless redundant_receiver?(send_nodes, node)
78
+
79
+ send_nodes.each do |send_node|
80
+ receiver = send_node.receiver
81
+ add_offense(receiver.source_range) do |corrector|
82
+ autocorrect(corrector, send_node, node)
94
83
  end
95
84
  end
96
85
  end
97
86
 
87
+ alias on_numblock on_block
88
+
98
89
  private
99
90
 
100
- def autocorrect(corrector, node)
101
- corrector.remove(node.receiver.source_range)
102
- corrector.remove(node.loc.dot)
103
- corrector.remove(block_argument_range(node))
91
+ def autocorrect(corrector, send_node, node)
92
+ corrector.remove(send_node.receiver.source_range)
93
+ corrector.remove(send_node.loc.dot)
94
+ corrector.remove(block_argument_range(send_node)) unless node.numblock_type?
95
+ end
96
+
97
+ def redundant_receiver?(send_nodes, node)
98
+ proc = if node.numblock_type?
99
+ ->(n) { n.receiver.lvar_type? && n.receiver.source == '_1' }
100
+ else
101
+ return false if node.arguments.empty?
102
+
103
+ arg = node.arguments.first
104
+ ->(n) { same_value?(arg, n.receiver) }
105
+ end
106
+
107
+ send_nodes.all?(&proc)
104
108
  end
105
109
 
106
110
  def block_argument_range(node)