rubocop-rails 2.34.0 → 2.34.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffddf417706a66433be596f397b807c824a2593bea0a5175907be9d9be318970
4
- data.tar.gz: 40672bac8ab7a1961102c3f5306526fa98a360987c7095128313dd5bae7c5641
3
+ metadata.gz: 0d751c9891e698f0916a8e58e8cb6b30c4c267b1c069e7b57e01160bb121b49c
4
+ data.tar.gz: 5ae0f4177bd644cc89d683e1a3176eb6434fe69195c043fe5dc057be52ddff0d
5
5
  SHA512:
6
- metadata.gz: 16ba2fc1926397b8b5c413c207a9f45e80a30b53e1e73b215e18abf38218e3aea3ecd2090cc4858adfe4a9d32ebb527b83db57acb38cff48510d4eaf18dcdd9c
7
- data.tar.gz: 6541216ccafd3cd5287517989ea8a49dcb4b547ca98d0c363f58ac5a11ebbe6452329ea0964357e7f9777a5224d05d271b6bd21ee31f0d0bffcb37dfaa26dbd0
6
+ metadata.gz: 44b7345a990e2ccf25ed3c4e4f1de3ae8cfe46bf0ea6841c9d5f43a24e04f881729d9c9ce4cce15dc71430dcb30a6684f16c05a299a5b040b37f1ffd22f9ea4e
7
+ data.tar.gz: 3cf9f62438b49c1c3c2c075bebb3f7a21c61ebc53236be6b1498c1d07cc63e3c021c6c266e19b6080345023a1f6f985146b4119358908704573c49d2a9763111
@@ -45,7 +45,7 @@ module RuboCop
45
45
  return unless node.receiver&.const_name == 'Rails'
46
46
 
47
47
  parent = node.parent
48
- return unless parent&.predicate_method?
48
+ return unless parent.respond_to?(:predicate_method?) && parent.predicate_method?
49
49
 
50
50
  return if ALLOWED_LIST.include?(parent.method_name)
51
51
 
@@ -53,6 +53,16 @@ module RuboCop
53
53
  #
54
54
  # # good
55
55
  # a.presence&.foo
56
+ #
57
+ # # good
58
+ # a.present? ? a[1] : nil
59
+ #
60
+ # # good
61
+ # a[:key] = value if a.present?
62
+ #
63
+ # # good
64
+ # a.present? ? a > 1 : nil
65
+ # a <= 0 if a.present?
56
66
  class Presence < Base
57
67
  include RangeHelp
58
68
  extend AutoCorrector
@@ -130,7 +140,7 @@ module RuboCop
130
140
  end
131
141
 
132
142
  def ignore_chain_node?(node)
133
- node.method?('[]') || node.arithmetic_operation?
143
+ node.method?('[]') || node.method?('[]=') || node.arithmetic_operation? || node.comparison_method?
134
144
  end
135
145
 
136
146
  def message(node, replacement)
@@ -33,33 +33,38 @@ module RuboCop
33
33
 
34
34
  def_node_matcher :redirect_back_with_fallback_location, <<~PATTERN
35
35
  (send nil? :redirect_back
36
- (hash <$(pair (sym :fallback_location) $_) ...>)
36
+ (hash <$(pair (sym :fallback_location) $_) $...>)
37
37
  )
38
38
  PATTERN
39
39
 
40
40
  def on_send(node)
41
- redirect_back_with_fallback_location(node) do |fallback_pair, fallback_value|
41
+ redirect_back_with_fallback_location(node) do |fallback_pair, fallback_value, options|
42
42
  add_offense(node.loc.selector) do |corrector|
43
- correct_redirect_back(corrector, node, fallback_pair, fallback_value)
43
+ correct_redirect_back(corrector, node, fallback_pair, fallback_value, options)
44
44
  end
45
45
  end
46
46
  end
47
47
 
48
48
  private
49
49
 
50
- def correct_redirect_back(corrector, node, fallback_pair, fallback_value)
50
+ # rubocop:disable Metrics/AbcSize
51
+ def correct_redirect_back(corrector, node, fallback_pair, fallback_value, options)
51
52
  corrector.replace(node.loc.selector, 'redirect_back_or_to')
52
53
 
53
54
  hash_arg = node.first_argument
54
55
 
55
56
  if hash_arg.pairs.one?
56
- corrector.replace(hash_arg, fallback_value.source)
57
+ arguments = [fallback_value.source] + options.map(&:source)
58
+ corrector.replace(hash_arg, arguments.join(', '))
57
59
  else
58
60
  remove_fallback_location_pair(corrector, hash_arg, fallback_pair)
59
61
  first_pair = hash_arg.pairs.find { |pair| pair != fallback_pair }
60
62
  corrector.insert_before(first_pair, "#{fallback_value.source}, ")
61
63
  end
64
+
65
+ wrap_with_parentheses(node, corrector) unless node.parenthesized?
62
66
  end
67
+ # rubocop:enable Metrics/AbcSize
63
68
 
64
69
  def remove_fallback_location_pair(corrector, hash_node, fallback_pair)
65
70
  pairs = hash_node.pairs
@@ -74,6 +79,11 @@ module RuboCop
74
79
  end
75
80
  end
76
81
 
82
+ def wrap_with_parentheses(node, corrector)
83
+ corrector.replace(node.loc.selector.end.join(node.first_argument.source_range.begin), '(')
84
+ corrector.insert_after(node, ')')
85
+ end
86
+
77
87
  def remove_first_pair(corrector, fallback_pair, next_pair)
78
88
  range = fallback_pair.source_range.join(next_pair.source_range.begin)
79
89
  corrector.remove(range)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Rails
5
5
  # This module holds the RuboCop Rails version information.
6
6
  module Version
7
- STRING = '2.34.0'
7
+ STRING = '2.34.1'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.34.0
4
+ version: 2.34.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov