rubocop-rails 2.26.1 → 2.27.0
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 +4 -4
- data/README.md +35 -1
- data/config/default.yml +5 -5
- data/lib/rubocop/cop/mixin/target_rails_version.rb +3 -5
- data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +5 -5
- data/lib/rubocop/cop/rails/compact_blank.rb +8 -4
- data/lib/rubocop/cop/rails/enum_syntax.rb +4 -0
- data/lib/rubocop/cop/rails/env_local.rb +26 -3
- data/lib/rubocop/cop/rails/file_path.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_foreign_key.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -1
- data/lib/rubocop/cop/rails/reflection_class_name.rb +1 -1
- data/lib/rubocop/cop/rails/request_referer.rb +1 -1
- data/lib/rubocop/cop/rails/select_map.rb +2 -1
- data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +1 -1
- data/lib/rubocop/cop/rails/time_zone.rb +3 -0
- data/lib/rubocop/cop/rails/transaction_exit_statement.rb +5 -0
- data/lib/rubocop/cop/rails/where_range.rb +1 -1
- data/lib/rubocop/rails/version.rb +1 -1
- metadata +4 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a20ef13a67612f99cb522625350e57836511b07a2666b75a8cbc556620adf01
|
4
|
+
data.tar.gz: 436aaa2f094779e0101e17d2cb56f4a7bdd64fbf14360e9e22102be13da413af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d73f8ec4a0567acff57d0e39b267a24ee615e58d20114fa8fd37d5614ea2d4b3e6435c9ed34db322d7368086f9df150de0646c5e63cfd2987d0847dc3abd057
|
7
|
+
data.tar.gz: 93f42a0c60daec890a8cd363f588e37e5f65210d63dbb7e85e1c19cd32dc6459d6e593fbc3947b1cef832b981ca7b06bb5871c60cf6439a07e32c3df54257bcc
|
data/README.md
CHANGED
@@ -63,9 +63,29 @@ RuboCop::RakeTask.new do |task|
|
|
63
63
|
end
|
64
64
|
```
|
65
65
|
|
66
|
+
## RuboCop Rails configuration
|
67
|
+
|
68
|
+
The following settings specific to RuboCop Rails can be configured in `.rubocop.yml`.
|
69
|
+
|
70
|
+
### `AllCops: TargetRailsVersion`
|
71
|
+
|
72
|
+
What version of Rails is the inspected code using? If a value is specified
|
73
|
+
for `TargetRailsVersion` then it is used. Acceptable values are specified
|
74
|
+
as a float (e.g., 7.2); the patch version of Rails should not be included.
|
75
|
+
|
76
|
+
```yaml
|
77
|
+
AllCops:
|
78
|
+
TargetRailsVersion: 7.2
|
79
|
+
```
|
80
|
+
|
81
|
+
If `TargetRailsVersion` is not set, RuboCop will parse the Gemfile.lock or
|
82
|
+
gems.locked file to find the version of Rails that has been bound to the
|
83
|
+
application. If neither of those files exist, RuboCop will use Rails 5.0
|
84
|
+
as the default.
|
85
|
+
|
66
86
|
## Rails configuration tip
|
67
87
|
|
68
|
-
|
88
|
+
In Rails 6.1+, add the following `config.generators.after_generate` setting to
|
69
89
|
your `config/environments/development.rb` to apply RuboCop autocorrection to code generated by `bin/rails g`.
|
70
90
|
|
71
91
|
```ruby
|
@@ -84,6 +104,20 @@ It uses `rubocop -A` to apply `Style/FrozenStringLiteralComment` and other unsaf
|
|
84
104
|
`rubocop -A` is unsafe autocorrection, but code generated by default is simple and less likely to
|
85
105
|
be incompatible with `rubocop -A`. If you have problems you can replace it with `rubocop -a` instead.
|
86
106
|
|
107
|
+
In Rails 7.2+, it is recommended to use `config.generators.apply_rubocop_autocorrect_after_generate!` instead of the above setting:
|
108
|
+
|
109
|
+
```diff
|
110
|
+
# config/environments/development.rb
|
111
|
+
Rails.application.configure do
|
112
|
+
(snip)
|
113
|
+
# Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
|
114
|
+
- # config.generators.apply_rubocop_autocorrect_after_generate!
|
115
|
+
+ config.generators.apply_rubocop_autocorrect_after_generate!
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
You only need to uncomment.
|
120
|
+
|
87
121
|
## The Cops
|
88
122
|
|
89
123
|
All cops are located under
|
data/config/default.yml
CHANGED
@@ -17,12 +17,12 @@ AllCops:
|
|
17
17
|
# Enable checking Active Support extensions.
|
18
18
|
# See: https://docs.rubocop.org/rubocop/configuration.html#enable-checking-active-support-extensions
|
19
19
|
ActiveSupportExtensionsEnabled: true
|
20
|
-
# What version of Rails is the inspected code using?
|
21
|
-
# for TargetRailsVersion then it is used.
|
22
|
-
# as a float (
|
23
|
-
# If TargetRailsVersion is not set, RuboCop will parse the Gemfile.lock or
|
20
|
+
# What version of Rails is the inspected code using? If a value is specified
|
21
|
+
# for `TargetRailsVersion` then it is used. Acceptable values are specified
|
22
|
+
# as a float (e.g., 7.2); the patch version of Rails should not be included.
|
23
|
+
# If `TargetRailsVersion` is not set, RuboCop will parse the Gemfile.lock or
|
24
24
|
# gems.locked file to find the version of Rails that has been bound to the
|
25
|
-
# application.
|
25
|
+
# application. If neither of those files exist, RuboCop will use Rails 5.0
|
26
26
|
# as the default.
|
27
27
|
TargetRailsVersion: ~
|
28
28
|
|
@@ -7,6 +7,9 @@ module RuboCop
|
|
7
7
|
# Informs the base RuboCop gem that it the Rails version is checked via `requires_gem` API,
|
8
8
|
# without needing to call this `#support_target_rails_version` method.
|
9
9
|
USES_REQUIRES_GEM_API = true
|
10
|
+
# Look for `railties` instead of `rails`, to support apps that only use a subset of `rails`
|
11
|
+
# See https://github.com/rubocop/rubocop/pull/11289
|
12
|
+
TARGET_GEM_NAME = 'railties' # :nodoc:
|
10
13
|
|
11
14
|
def minimum_target_rails_version(version)
|
12
15
|
if respond_to?(:requires_gem)
|
@@ -33,11 +36,6 @@ module RuboCop
|
|
33
36
|
@minimum_target_rails_version <= version
|
34
37
|
end
|
35
38
|
end
|
36
|
-
|
37
|
-
# Look for `railties` instead of `rails`, to support apps that only use a subset of `rails`
|
38
|
-
# See https://github.com/rubocop/rubocop/pull/11289
|
39
|
-
TARGET_GEM_NAME = 'railties'
|
40
|
-
private_constant :TARGET_GEM_NAME
|
41
39
|
end
|
42
40
|
end
|
43
41
|
end
|
@@ -72,13 +72,13 @@ module RuboCop
|
|
72
72
|
if (node = context.each_ancestor(:if, :rescue).first)
|
73
73
|
return false if use_redirect_to?(context)
|
74
74
|
|
75
|
-
context = node
|
75
|
+
context = node
|
76
|
+
elsif context.right_siblings.empty?
|
77
|
+
return true
|
76
78
|
end
|
79
|
+
context = context.right_siblings
|
77
80
|
|
78
|
-
|
79
|
-
return true if siblings.empty?
|
80
|
-
|
81
|
-
siblings.compact.any? do |render_candidate|
|
81
|
+
context.compact.any? do |render_candidate|
|
82
82
|
render?(render_candidate)
|
83
83
|
end
|
84
84
|
end
|
@@ -25,6 +25,8 @@ module RuboCop
|
|
25
25
|
# collection.reject { |_k, v| v.blank? }
|
26
26
|
# collection.select(&:present?)
|
27
27
|
# collection.select { |_k, v| v.present? }
|
28
|
+
# collection.filter(&:present?)
|
29
|
+
# collection.filter { |_k, v| v.present? }
|
28
30
|
#
|
29
31
|
# # good
|
30
32
|
# collection.compact_blank
|
@@ -44,7 +46,8 @@ module RuboCop
|
|
44
46
|
extend TargetRailsVersion
|
45
47
|
|
46
48
|
MSG = 'Use `%<preferred_method>s` instead.'
|
47
|
-
RESTRICT_ON_SEND = %i[reject delete_if select keep_if].freeze
|
49
|
+
RESTRICT_ON_SEND = %i[reject delete_if select filter keep_if].freeze
|
50
|
+
DESTRUCTIVE_METHODS = %i[delete_if keep_if].freeze
|
48
51
|
|
49
52
|
minimum_target_rails_version 6.1
|
50
53
|
|
@@ -64,19 +67,20 @@ module RuboCop
|
|
64
67
|
|
65
68
|
def_node_matcher :select_with_block?, <<~PATTERN
|
66
69
|
(block
|
67
|
-
(send _ {:select :keep_if})
|
70
|
+
(send _ {:select :filter :keep_if})
|
68
71
|
$(args ...)
|
69
72
|
(send
|
70
73
|
$(lvar _) :present?))
|
71
74
|
PATTERN
|
72
75
|
|
73
76
|
def_node_matcher :select_with_block_pass?, <<~PATTERN
|
74
|
-
(send _ {:select :keep_if}
|
77
|
+
(send _ {:select :filter :keep_if}
|
75
78
|
(block-pass
|
76
79
|
(sym :present?)))
|
77
80
|
PATTERN
|
78
81
|
|
79
82
|
def on_send(node)
|
83
|
+
return if target_ruby_version < 2.6 && node.method?(:filter)
|
80
84
|
return unless bad_method?(node)
|
81
85
|
|
82
86
|
range = offense_range(node)
|
@@ -120,7 +124,7 @@ module RuboCop
|
|
120
124
|
end
|
121
125
|
|
122
126
|
def preferred_method(node)
|
123
|
-
|
127
|
+
DESTRUCTIVE_METHODS.include?(node.method_name) ? 'compact_blank!' : 'compact_blank'
|
124
128
|
end
|
125
129
|
end
|
126
130
|
end
|
@@ -17,8 +17,10 @@ module RuboCop
|
|
17
17
|
#
|
18
18
|
class EnumSyntax < Base
|
19
19
|
extend AutoCorrector
|
20
|
+
extend TargetRubyVersion
|
20
21
|
extend TargetRailsVersion
|
21
22
|
|
23
|
+
minimum_target_ruby_version 3.0
|
22
24
|
minimum_target_rails_version 7.0
|
23
25
|
|
24
26
|
MSG = 'Enum defined with keyword arguments in `%<enum>s` enum declaration. Use positional arguments instead.'
|
@@ -104,6 +106,8 @@ module RuboCop
|
|
104
106
|
end
|
105
107
|
|
106
108
|
def option_key?(pair)
|
109
|
+
return false unless pair.respond_to?(:key)
|
110
|
+
|
107
111
|
UNDERSCORED_OPTION_NAMES.include?(pair.key.source)
|
108
112
|
end
|
109
113
|
|
@@ -19,20 +19,33 @@ module RuboCop
|
|
19
19
|
extend TargetRailsVersion
|
20
20
|
|
21
21
|
MSG = 'Use `Rails.env.local?` instead.'
|
22
|
+
MSG_NEGATED = 'Use `!Rails.env.local?` instead.'
|
22
23
|
LOCAL_ENVIRONMENTS = %i[development? test?].to_set.freeze
|
23
24
|
|
24
25
|
minimum_target_rails_version 7.1
|
25
26
|
|
26
|
-
# @!method
|
27
|
-
def_node_matcher :
|
27
|
+
# @!method rails_env_local_or?(node)
|
28
|
+
def_node_matcher :rails_env_local_or?, <<~PATTERN
|
28
29
|
(or
|
29
30
|
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
|
30
31
|
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
|
31
32
|
)
|
32
33
|
PATTERN
|
33
34
|
|
35
|
+
# @!method rails_env_local_and?(node)
|
36
|
+
def_node_matcher :rails_env_local_and?, <<~PATTERN
|
37
|
+
(and
|
38
|
+
(send
|
39
|
+
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
|
40
|
+
:!)
|
41
|
+
(send
|
42
|
+
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
|
43
|
+
:!)
|
44
|
+
)
|
45
|
+
PATTERN
|
46
|
+
|
34
47
|
def on_or(node)
|
35
|
-
|
48
|
+
rails_env_local_or?(node) do |*environments|
|
36
49
|
next unless environments.to_set == LOCAL_ENVIRONMENTS
|
37
50
|
|
38
51
|
add_offense(node) do |corrector|
|
@@ -40,6 +53,16 @@ module RuboCop
|
|
40
53
|
end
|
41
54
|
end
|
42
55
|
end
|
56
|
+
|
57
|
+
def on_and(node)
|
58
|
+
rails_env_local_and?(node) do |*environments|
|
59
|
+
next unless environments.to_set == LOCAL_ENVIRONMENTS
|
60
|
+
|
61
|
+
add_offense(node, message: MSG_NEGATED) do |corrector|
|
62
|
+
corrector.replace(node, '!Rails.env.local?')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
43
66
|
end
|
44
67
|
end
|
45
68
|
end
|
@@ -97,7 +97,7 @@ module RuboCop
|
|
97
97
|
return unless node.arguments.any? { |e| rails_root_nodes?(e) }
|
98
98
|
|
99
99
|
register_offense(node, require_to_s: true) do |corrector|
|
100
|
-
autocorrect_file_join(corrector, node)
|
100
|
+
autocorrect_file_join(corrector, node) unless node.first_argument.array_type?
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
@@ -40,7 +40,7 @@ module RuboCop
|
|
40
40
|
def on_send(node)
|
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
|
-
add_offense(foreign_key_pair
|
43
|
+
add_offense(foreign_key_pair) do |corrector|
|
44
44
|
range = range_with_surrounding_space(foreign_key_pair.source_range, side: :left)
|
45
45
|
range = range_with_surrounding_comma(range, :left)
|
46
46
|
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
return if reflection_class_name.value.send_type? && reflection_class_name.value.receiver.nil?
|
44
44
|
return if reflection_class_name.value.lvar_type? && str_assigned?(reflection_class_name)
|
45
45
|
|
46
|
-
add_offense(reflection_class_name
|
46
|
+
add_offense(reflection_class_name) do |corrector|
|
47
47
|
autocorrect(corrector, reflection_class_name)
|
48
48
|
end
|
49
49
|
end
|
@@ -40,12 +40,13 @@ module RuboCop
|
|
40
40
|
autocorrect(corrector, select_node, node, preferred_method)
|
41
41
|
end
|
42
42
|
end
|
43
|
+
alias on_csend on_send
|
43
44
|
|
44
45
|
private
|
45
46
|
|
46
47
|
def find_select_node(node, column_name)
|
47
48
|
node.descendants.detect do |select_candidate|
|
48
|
-
next if !select_candidate.
|
49
|
+
next if !select_candidate.call_type? || !select_candidate.method?(:select)
|
49
50
|
|
50
51
|
match_column_name?(select_candidate, column_name)
|
51
52
|
end
|
@@ -28,6 +28,8 @@ module RuboCop
|
|
28
28
|
# Time.zone.now
|
29
29
|
# Time.zone.parse('2015-03-02T19:05:37')
|
30
30
|
# Time.zone.parse('2015-03-02T19:05:37Z') # Respect ISO 8601 format with timezone specifier.
|
31
|
+
# Time.parse('2015-03-02T19:05:37Z') # Also respects ISO 8601
|
32
|
+
# '2015-03-02T19:05:37Z'.to_time # Also respects ISO 8601
|
31
33
|
#
|
32
34
|
# @example EnforcedStyle: flexible (default)
|
33
35
|
# # `flexible` allows usage of `in_time_zone` instead of `zone`.
|
@@ -67,6 +69,7 @@ module RuboCop
|
|
67
69
|
|
68
70
|
def on_send(node)
|
69
71
|
return if !node.receiver&.str_type? || !node.method?(:to_time)
|
72
|
+
return if attach_timezone_specifier?(node.receiver)
|
70
73
|
|
71
74
|
add_offense(node.loc.selector, message: MSG_STRING_TO_TIME) do |corrector|
|
72
75
|
corrector.replace(node, "Time.zone.parse(#{node.receiver.source})") unless node.csend_type?
|
@@ -15,6 +15,10 @@ module RuboCop
|
|
15
15
|
#
|
16
16
|
# If you are defining custom transaction methods, you can configure it with `TransactionMethods`.
|
17
17
|
#
|
18
|
+
# NOTE: This cop is disabled on Rails >= 7.2 because transactions were restored
|
19
|
+
# to their historical behavior. In Rails 7.1, the behavior is controlled with
|
20
|
+
# the config `active_record.commit_transaction_on_non_local_return`.
|
21
|
+
#
|
18
22
|
# @example
|
19
23
|
# # bad
|
20
24
|
# ApplicationRecord.transaction do
|
@@ -76,6 +80,7 @@ module RuboCop
|
|
76
80
|
PATTERN
|
77
81
|
|
78
82
|
def on_send(node)
|
83
|
+
return if target_rails_version >= 7.2
|
79
84
|
return unless in_transaction_block?(node)
|
80
85
|
|
81
86
|
exit_statements(node.parent.body).each do |statement_node|
|
metadata
CHANGED
@@ -1,16 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.27.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
8
8
|
- Jonas Arvidsson
|
9
9
|
- Yuji Nakayama
|
10
|
-
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date: 2024-
|
12
|
+
date: 2024-10-26 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: activesupport
|
@@ -247,10 +246,9 @@ metadata:
|
|
247
246
|
homepage_uri: https://docs.rubocop.org/rubocop-rails/
|
248
247
|
changelog_uri: https://github.com/rubocop/rubocop-rails/blob/master/CHANGELOG.md
|
249
248
|
source_code_uri: https://github.com/rubocop/rubocop-rails/
|
250
|
-
documentation_uri: https://docs.rubocop.org/rubocop-rails/2.
|
249
|
+
documentation_uri: https://docs.rubocop.org/rubocop-rails/2.27/
|
251
250
|
bug_tracker_uri: https://github.com/rubocop/rubocop-rails/issues
|
252
251
|
rubygems_mfa_required: 'true'
|
253
|
-
post_install_message:
|
254
252
|
rdoc_options: []
|
255
253
|
require_paths:
|
256
254
|
- lib
|
@@ -265,8 +263,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
265
263
|
- !ruby/object:Gem::Version
|
266
264
|
version: '0'
|
267
265
|
requirements: []
|
268
|
-
rubygems_version: 3.
|
269
|
-
signing_key:
|
266
|
+
rubygems_version: 3.6.0.dev
|
270
267
|
specification_version: 4
|
271
268
|
summary: Automatic Rails code style checking tool.
|
272
269
|
test_files: []
|