rubocop-rails 2.33.2 → 2.33.4

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: bf236bb6f7b10ea743c3afd8b4c92f2563d5cd989f3c8820b8fb270c9bb72c59
4
- data.tar.gz: 356c8e006d8e4d1b4893cb263b9a67e0ecc3245556868678c5681800cc75cf36
3
+ metadata.gz: ddb8c79abf97afc451393bd9a309ac575a99057e1143d4e4632fa84568173e8b
4
+ data.tar.gz: b6cf48b796104788e4d2302f379328071dc5b5042f3cd6923eb625a3ec19128d
5
5
  SHA512:
6
- metadata.gz: 8fbcfbc453e83a19e6b9c1b173347c9dbef375650c4202ac7949fcec04ae8bc65307246d0630a106455cb44a66e3736d287894e4709b12692fd6e589ed574235
7
- data.tar.gz: 245d221771ce84010ff4195efbb6c231e28e0daac18288d52c201b347951475ef29168098d518fe7d75f7a33524b5a90f81b604720ab130629651d598e23cec5
6
+ metadata.gz: 2618f426daf94c51e8fceb989eca50055dae465b334a5ef83d5dc940959a123e1f3a2337246db86defd3f189970a1848a207b5b6b8afcf9917149877bd3fee3e
7
+ data.tar.gz: 164f84eeab842a88a39131e8c8949584487ac22893604de862ecc28cd984d6df2e5e244ab82e57295dbe9abba0c71830c018412325eccaa3a6ff376f3517ecfc
@@ -73,7 +73,7 @@ module RuboCop
73
73
  return false if use_redirect_to?(context)
74
74
 
75
75
  context = node
76
- elsif context.right_siblings.empty?
76
+ elsif context.right_siblings.empty? && !use_redirect_to?(context.parent)
77
77
  return true
78
78
  end
79
79
  context = context.right_siblings
@@ -46,7 +46,7 @@ module RuboCop
46
46
  private
47
47
 
48
48
  def where_method?(receiver)
49
- return false unless receiver
49
+ return false if !receiver || receiver.any_block_type?
50
50
 
51
51
  receiver.respond_to?(:method?) && receiver.method?(:where)
52
52
  end
@@ -13,7 +13,7 @@ module RuboCop
13
13
  # or the code may have a different purpose than memoization.
14
14
  #
15
15
  # @example
16
- # # bad
16
+ # # bad - exclusively doing memoization
17
17
  # def current_user
18
18
  # @current_user ||= User.find_by(id: session[:user_id])
19
19
  # end
@@ -24,6 +24,22 @@ module RuboCop
24
24
  #
25
25
  # @current_user = User.find_by(id: session[:user_id])
26
26
  # end
27
+ #
28
+ # # bad - method contains other code
29
+ # def current_user
30
+ # @current_user ||= User.find_by(id: session[:user_id])
31
+ # @current_user.do_something
32
+ # end
33
+ #
34
+ # # good
35
+ # def current_user
36
+ # if defined?(@current_user)
37
+ # @current_user
38
+ # else
39
+ # @current_user = User.find_by(id: session[:user_id])
40
+ # end
41
+ # @current_user.do_something
42
+ # end
27
43
  class FindByOrAssignmentMemoization < Base
28
44
  extend AutoCorrector
29
45
 
@@ -38,6 +54,24 @@ module RuboCop
38
54
  )
39
55
  PATTERN
40
56
 
57
+ # When a method body contains only memoization, the correction can be more succinct.
58
+ def on_def(node)
59
+ find_by_or_assignment_memoization(node.body) do |varible_name, find_by|
60
+ add_offense(node.body) do |corrector|
61
+ corrector.replace(
62
+ node.body,
63
+ <<~RUBY.rstrip
64
+ return #{varible_name} if defined?(#{varible_name})
65
+
66
+ #{varible_name} = #{find_by.source}
67
+ RUBY
68
+ )
69
+
70
+ correct_to_regular_method_definition(corrector, node) if node.endless?
71
+ end
72
+ end
73
+ end
74
+
41
75
  def on_send(node)
42
76
  assignment_node = node.parent
43
77
  find_by_or_assignment_memoization(assignment_node) do |varible_name, find_by|
@@ -47,14 +81,25 @@ module RuboCop
47
81
  corrector.replace(
48
82
  assignment_node,
49
83
  <<~RUBY.rstrip
50
- return #{varible_name} if defined?(#{varible_name})
51
-
52
- #{varible_name} = #{find_by.source}
84
+ if defined?(#{varible_name})
85
+ #{varible_name}
86
+ else
87
+ #{varible_name} = #{find_by.source}
88
+ end
53
89
  RUBY
54
90
  )
55
91
  end
56
92
  end
57
93
  end
94
+
95
+ private
96
+
97
+ def correct_to_regular_method_definition(corrector, node)
98
+ range = node.loc.assignment.join(node.body.source_range.begin)
99
+
100
+ corrector.replace(range, "\n")
101
+ corrector.insert_after(node, "\nend")
102
+ end
58
103
  end
59
104
  end
60
105
  end
@@ -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.33.2'
7
+ STRING = '2.33.4'
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.33.2
4
+ version: 2.33.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov