rubocop-rails 2.17.0 → 2.17.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a7a516c82d0953f3f1995b190ddc427aa251583287a9232229e82a218c93ba6
4
- data.tar.gz: f224a35df6de69c04c7c6f6314f9cfa713cc5accfba1bbce9d5554c7fd50a121
3
+ metadata.gz: 97069bbada22491ece697f32f933ce8dc5274c18ad2782bb832cd83b168874a3
4
+ data.tar.gz: 4a691068f93c7be89e9cf695c3be16fe183cb23be4b187e822a9bf9d896c9af7
5
5
  SHA512:
6
- metadata.gz: 5b3277765f3819855d3e29d3226c81ebc6231b1b1aaf583e1ae86db9e18b7b450bbc2b3f9bccaaa85623ca57e0ac139abde604c35bccc81c3081a80c4392f35d
7
- data.tar.gz: a3d7b79f10c2090b04a05d1b30cc3c639645aa6b3c2ad33247fecfa7597907238d8f6e727a8e0dd088b92d69842c488489e9883f9acfd4551e08a9b8f3dd1f8c
6
+ metadata.gz: 213e3da3d5e23338c53ee9428d8d44a57123fa8a239c3974e5eb0181797555639da23ed7e09336e906e14ba3c99c46e33efba18db44646f0efe86913dc0b7847
7
+ data.tar.gz: ed27cd2b9106a650b01fc700e9c2511971853412f49aa93876589dfa67e77c7a246fa364dd4682747fc97ffe8121d286a4096e6ef0b257483668c36f99fec9e1
data/config/default.yml CHANGED
@@ -73,7 +73,7 @@ Rails/ActionControllerFlashBeforeRender:
73
73
  StyleGuide: 'https://rails.rubystyle.guide/#flash-before-render'
74
74
  Reference: 'https://api.rubyonrails.org/classes/ActionController/FlashBeforeRender.html'
75
75
  Enabled: 'pending'
76
- SafeAutocorrect: false
76
+ SafeAutoCorrect: false
77
77
  VersionAdded: '2.16'
78
78
 
79
79
  Rails/ActionControllerTestCase:
@@ -81,7 +81,7 @@ Rails/ActionControllerTestCase:
81
81
  StyleGuide: 'https://rails.rubystyle.guide/#integration-testing'
82
82
  Reference: 'https://api.rubyonrails.org/classes/ActionController/TestCase.html'
83
83
  Enabled: 'pending'
84
- SafeAutocorrect: false
84
+ SafeAutoCorrect: false
85
85
  VersionAdded: '2.14'
86
86
  Include:
87
87
  - '**/test/**/*.rb'
@@ -873,7 +873,7 @@ Rails/RootJoinChain:
873
873
  Rails/RootPathnameMethods:
874
874
  Description: 'Use `Rails.root` IO methods instead of passing it to `File`.'
875
875
  Enabled: pending
876
- SafeAutocorrect: false
876
+ SafeAutoCorrect: false
877
877
  VersionAdded: '2.16'
878
878
 
879
879
  Rails/RootPublicPath:
@@ -37,8 +37,8 @@ module RuboCop
37
37
  ^(send (send nil? :flash) :[]= ...)
38
38
  PATTERN
39
39
 
40
- def_node_search :redirect_to?, <<~PATTERN
41
- (send nil? :redirect_to ...)
40
+ def_node_search :render?, <<~PATTERN
41
+ (send nil? :render ...)
42
42
  PATTERN
43
43
 
44
44
  def_node_search :action_controller?, <<~PATTERN
@@ -53,7 +53,7 @@ module RuboCop
53
53
  def on_send(flash_node)
54
54
  return unless flash_assignment?(flash_node)
55
55
 
56
- return if followed_by_redirect_to?(flash_node)
56
+ return unless followed_by_render?(flash_node)
57
57
 
58
58
  return unless instance_method_or_block?(flash_node)
59
59
 
@@ -66,13 +66,15 @@ module RuboCop
66
66
 
67
67
  private
68
68
 
69
- def followed_by_redirect_to?(flash_node)
69
+ def followed_by_render?(flash_node)
70
70
  flash_assigment_node = find_ancestor(flash_node, type: :send)
71
- context = flash_assigment_node.parent
71
+ context = flash_assigment_node
72
+ context = context.parent if context.parent.if_type?
73
+ context = context.right_siblings
74
+ return true if context.empty?
72
75
 
73
- flash_index = context.children.index(flash_assigment_node)
74
- context.each_child_node.with_index.any? do |node, index|
75
- index > flash_index && redirect_to?(node)
76
+ context.compact.any? do |node|
77
+ render?(node)
76
78
  end
77
79
  end
78
80
 
@@ -12,7 +12,6 @@ module RuboCop
12
12
  # render plain: 'foo/bar', status: 304
13
13
  # redirect_to root_url, status: 301
14
14
  # head 200
15
- # get '/foobar', to: redirect('/foobar/baz', status: 301)
16
15
  #
17
16
  # # good
18
17
  # render :foo, status: :ok
@@ -20,7 +19,6 @@ module RuboCop
20
19
  # render plain: 'foo/bar', status: :not_modified
21
20
  # redirect_to root_url, status: :moved_permanently
22
21
  # head :ok
23
- # get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
24
22
  #
25
23
  # @example EnforcedStyle: numeric
26
24
  # # bad
@@ -29,7 +27,6 @@ module RuboCop
29
27
  # render plain: 'foo/bar', status: :not_modified
30
28
  # redirect_to root_url, status: :moved_permanently
31
29
  # head :ok
32
- # get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
33
30
  #
34
31
  # # good
35
32
  # render :foo, status: 200
@@ -37,20 +34,18 @@ module RuboCop
37
34
  # render plain: 'foo/bar', status: 304
38
35
  # redirect_to root_url, status: 301
39
36
  # head 200
40
- # get '/foobar', to: redirect('/foobar/baz', status: 301)
41
37
  #
42
38
  class HttpStatus < Base
43
39
  include ConfigurableEnforcedStyle
44
40
  extend AutoCorrector
45
41
 
46
- RESTRICT_ON_SEND = %i[render redirect_to head redirect].freeze
42
+ RESTRICT_ON_SEND = %i[render redirect_to head].freeze
47
43
 
48
44
  def_node_matcher :http_status, <<~PATTERN
49
45
  {
50
46
  (send nil? {:render :redirect_to} _ $hash)
51
47
  (send nil? {:render :redirect_to} $hash)
52
48
  (send nil? :head ${int sym} ...)
53
- (send nil? :redirect _ $hash)
54
49
  }
55
50
  PATTERN
56
51
 
@@ -26,34 +26,53 @@ module RuboCop
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) :[] $_key))
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, key|
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
- replacement = "pluck(#{key.source})"
42
- message = message(replacement, node)
43
-
44
- add_offense(offense_range(node), message: message) do |corrector|
45
- corrector.replace(offense_range(node), replacement)
46
- end
44
+ register_offense(node, key)
47
45
  end
48
46
  end
49
47
  alias on_numblock on_block
50
48
 
51
49
  private
52
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
+
53
63
  def offense_range(node)
54
64
  node.send_node.loc.selector.join(node.loc.end)
55
65
  end
56
66
 
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
+
57
76
  def message(replacement, node)
58
77
  current = offense_range(node).source
59
78
 
@@ -45,7 +45,7 @@ module RuboCop
45
45
 
46
46
  def multiple_arguments_hash?(hash)
47
47
  return true if hash.pairs.size >= 2
48
- return false unless hash.values[0].hash_type?
48
+ return false unless hash.values[0]&.hash_type?
49
49
 
50
50
  multiple_arguments_hash?(hash.values[0])
51
51
  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.17.0'
7
+ STRING = '2.17.2'
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.17.0
4
+ version: 2.17.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-10-22 00:00:00.000000000 Z
13
+ date: 2022-10-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport