rubocop-rails 2.3.2 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/default.yml +33 -2
- data/lib/rubocop/cop/rails/application_controller.rb +36 -0
- data/lib/rubocop/cop/rails/application_mailer.rb +40 -0
- data/lib/rubocop/cop/rails/date.rb +1 -1
- data/lib/rubocop/cop/rails/delegate.rb +1 -1
- data/lib/rubocop/cop/rails/enum_hash.rb +4 -6
- data/lib/rubocop/cop/rails/file_path.rb +2 -2
- data/lib/rubocop/cop/rails/presence.rb +1 -1
- data/lib/rubocop/cop/rails/rake_environment.rb +91 -0
- data/lib/rubocop/cop/rails/redundant_allow_nil.rb +1 -1
- data/lib/rubocop/cop/rails/reversible_migration.rb +24 -21
- data/lib/rubocop/cop/rails/safe_navigation.rb +25 -27
- data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +48 -0
- data/lib/rubocop/cop/rails/save_bang.rb +16 -10
- data/lib/rubocop/cop/rails/time_zone.rb +1 -1
- data/lib/rubocop/cop/rails/unknown_env.rb +30 -9
- data/lib/rubocop/cop/rails_cops.rb +4 -0
- data/lib/rubocop/rails/version.rb +1 -1
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eac129cae1115b4038b4731039495db93945c73a041fb762d7412521d4ec3619
|
4
|
+
data.tar.gz: 5a68473da7c7799e223d0c5de4c10609751a633b6db573cb155d41c942dfb755
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f0efc72cd3569bfe93874a5f77113186e73947bc9d6665fbf926a4e9724e9e4390a312541b628d305c8ea733a94ce94212f38f80db71ec4be3d291d63029f1f
|
7
|
+
data.tar.gz: ae2d76dd0b58c09108b15cf83f363262904c698a8f0839532838f39d969b101885329228674ed237fcafc10a63732be8a6a8c2a1a800339bb7e4a4bd18fb759b
|
data/config/default.yml
CHANGED
@@ -49,11 +49,21 @@ Rails/ActiveSupportAliases:
|
|
49
49
|
Enabled: true
|
50
50
|
VersionAdded: '0.48'
|
51
51
|
|
52
|
+
Rails/ApplicationController:
|
53
|
+
Description: 'Check that controllers subclass ApplicationController.'
|
54
|
+
Enabled: true
|
55
|
+
VersionAdded: '2.4'
|
56
|
+
|
52
57
|
Rails/ApplicationJob:
|
53
58
|
Description: 'Check that jobs subclass ApplicationJob.'
|
54
59
|
Enabled: true
|
55
60
|
VersionAdded: '0.49'
|
56
61
|
|
62
|
+
Rails/ApplicationMailer:
|
63
|
+
Description: 'Check that mailers subclass ApplicationMailer.'
|
64
|
+
Enabled: true
|
65
|
+
VersionAdded: '2.4'
|
66
|
+
|
57
67
|
Rails/ApplicationRecord:
|
58
68
|
Description: 'Check that models subclass ApplicationRecord.'
|
59
69
|
Enabled: true
|
@@ -183,8 +193,8 @@ Rails/FilePath:
|
|
183
193
|
Description: 'Use `Rails.root.join` for file path joining.'
|
184
194
|
Enabled: true
|
185
195
|
VersionAdded: '0.47'
|
186
|
-
VersionChanged: '
|
187
|
-
EnforcedStyle:
|
196
|
+
VersionChanged: '2.4'
|
197
|
+
EnforcedStyle: slashes
|
188
198
|
SupportedStyles:
|
189
199
|
- slashes
|
190
200
|
- arguments
|
@@ -323,6 +333,15 @@ Rails/Present:
|
|
323
333
|
# Convert usages of `unless blank?` to `if present?`
|
324
334
|
UnlessBlank: true
|
325
335
|
|
336
|
+
Rails/RakeEnvironment:
|
337
|
+
Description: 'Set `:environment` task as a dependency to all rake task.'
|
338
|
+
Enabled: true
|
339
|
+
Safe: false
|
340
|
+
VersionAdded: '2.4'
|
341
|
+
Include:
|
342
|
+
- '**/Rakefile'
|
343
|
+
- '**/*.rake'
|
344
|
+
|
326
345
|
Rails/ReadWriteAttribute:
|
327
346
|
Description: >-
|
328
347
|
Checks for read_attribute(:attr) and
|
@@ -395,6 +414,18 @@ Rails/SafeNavigation:
|
|
395
414
|
# implement the intended method. `try` will not raise an exception for this.
|
396
415
|
ConvertTry: false
|
397
416
|
|
417
|
+
Rails/SafeNavigationWithBlank:
|
418
|
+
Description: 'Avoid `foo&.blank?` in conditionals.'
|
419
|
+
Enabled: true
|
420
|
+
VersionAdded: '2.4'
|
421
|
+
# While the safe navigation operator is generally a good idea, when
|
422
|
+
# checking `foo&.blank?` in a conditional, `foo` being `nil` will actually
|
423
|
+
# do the opposite of what the author intends.
|
424
|
+
#
|
425
|
+
# foo&.blank? #=> nil
|
426
|
+
# foo.blank? #=> true
|
427
|
+
SafeAutoCorrect: false
|
428
|
+
|
398
429
|
Rails/SaveBang:
|
399
430
|
Description: 'Identifies possible cases where Active Record save! or related should be used.'
|
400
431
|
StyleGuide: 'https://rails.rubystyle.guide#save-bang'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# This cop checks that controllers subclass ApplicationController.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # good
|
11
|
+
# class MyController < ApplicationController
|
12
|
+
# # ...
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # bad
|
16
|
+
# class MyController < ActionController::Base
|
17
|
+
# # ...
|
18
|
+
# end
|
19
|
+
class ApplicationController < Cop
|
20
|
+
MSG = 'Controllers should subclass `ApplicationController`.'
|
21
|
+
SUPERCLASS = 'ApplicationController'
|
22
|
+
BASE_PATTERN = '(const (const nil? :ActionController) :Base)'
|
23
|
+
|
24
|
+
# rubocop:disable Layout/ClassStructure
|
25
|
+
include RuboCop::Cop::EnforceSuperclass
|
26
|
+
# rubocop:enable Layout/ClassStructure
|
27
|
+
|
28
|
+
def autocorrect(node)
|
29
|
+
lambda do |corrector|
|
30
|
+
corrector.replace(node.source_range, self.class::SUPERCLASS)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# This cop checks that mailers subclass ApplicationMailer with Rails 5.0.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # good
|
11
|
+
# class MyMailer < ApplicationMailer
|
12
|
+
# # ...
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # bad
|
16
|
+
# class MyMailer < ActionMailer::Base
|
17
|
+
# # ...
|
18
|
+
# end
|
19
|
+
class ApplicationMailer < Cop
|
20
|
+
extend TargetRailsVersion
|
21
|
+
|
22
|
+
minimum_target_rails_version 5.0
|
23
|
+
|
24
|
+
MSG = 'Mailers should subclass `ApplicationMailer`.'
|
25
|
+
SUPERCLASS = 'ApplicationMailer'
|
26
|
+
BASE_PATTERN = '(const (const nil? :ActionMailer) :Base)'
|
27
|
+
|
28
|
+
# rubocop:disable Layout/ClassStructure
|
29
|
+
include RuboCop::Cop::EnforceSuperclass
|
30
|
+
# rubocop:enable Layout/ClassStructure
|
31
|
+
|
32
|
+
def autocorrect(node)
|
33
|
+
lambda do |corrector|
|
34
|
+
corrector.replace(node.source_range, self.class::SUPERCLASS)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -85,7 +85,7 @@ module RuboCop
|
|
85
85
|
|
86
86
|
def check_deprecated_methods(node)
|
87
87
|
DEPRECATED_METHODS.each do |relevant:, deprecated:|
|
88
|
-
next unless node.
|
88
|
+
next unless node.method?(deprecated.to_sym)
|
89
89
|
|
90
90
|
add_offense(node, location: :selector,
|
91
91
|
message: format(DEPRECATED_MSG,
|
@@ -71,7 +71,7 @@ module RuboCop
|
|
71
71
|
delegation = ["delegate :#{node.body.method_name}",
|
72
72
|
"to: :#{node.body.receiver.method_name}"]
|
73
73
|
|
74
|
-
if node.
|
74
|
+
if node.method?(prefixed_method_name(node.body))
|
75
75
|
delegation << ['prefix: true']
|
76
76
|
end
|
77
77
|
|
@@ -41,13 +41,11 @@ module RuboCop
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def autocorrect(node)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
.each_with_index
|
48
|
-
.map { |elem, index| [elem.children.first, index] }.to_h
|
44
|
+
hash = node.children.each_with_index.map do |elem, index|
|
45
|
+
"#{elem.source} => #{index}"
|
46
|
+
end.join(', ')
|
49
47
|
|
50
|
-
->(corrector) { corrector.replace(
|
48
|
+
->(corrector) { corrector.replace(node.loc.expression, "{#{hash}}") }
|
51
49
|
end
|
52
50
|
|
53
51
|
private
|
@@ -7,7 +7,7 @@ module RuboCop
|
|
7
7
|
# to use `Rails.root.join` clause. It is used to add uniformity when
|
8
8
|
# joining paths.
|
9
9
|
#
|
10
|
-
# @example EnforcedStyle: arguments
|
10
|
+
# @example EnforcedStyle: arguments
|
11
11
|
# # bad
|
12
12
|
# Rails.root.join('app/models/goober')
|
13
13
|
# File.join(Rails.root, 'app/models/goober')
|
@@ -16,7 +16,7 @@ module RuboCop
|
|
16
16
|
# # good
|
17
17
|
# Rails.root.join('app', 'models', 'goober')
|
18
18
|
#
|
19
|
-
# @example EnforcedStyle: slashes
|
19
|
+
# @example EnforcedStyle: slashes (default)
|
20
20
|
# # bad
|
21
21
|
# Rails.root.join('app', 'models', 'goober')
|
22
22
|
# File.join(Rails.root, 'app/models/goober')
|
@@ -129,7 +129,7 @@ module RuboCop
|
|
129
129
|
end
|
130
130
|
|
131
131
|
def build_source_for_or_method(other)
|
132
|
-
if other.parenthesized? || !other.arguments?
|
132
|
+
if other.parenthesized? || other.method?('[]') || !other.arguments?
|
133
133
|
" || #{other.source}"
|
134
134
|
else
|
135
135
|
method = range_between(
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# This cop checks rake task definition without `environment` dependency.
|
7
|
+
# `environment` dependency is important because it loads application code
|
8
|
+
# for the rake task. The rake task cannot use application code, such as
|
9
|
+
# models, without `environment` dependency.
|
10
|
+
#
|
11
|
+
# You can ignore the offense if the task satisfies at least one of the
|
12
|
+
# following conditions:
|
13
|
+
#
|
14
|
+
# * The task does not need application code.
|
15
|
+
# * The task invokes :environment task.
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# # bad
|
19
|
+
# task :foo do
|
20
|
+
# do_something
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# task foo: :environment do
|
25
|
+
# do_something
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
class RakeEnvironment < Cop
|
29
|
+
MSG = 'Set `:environment` task as a dependency to all rake task.'
|
30
|
+
|
31
|
+
def_node_matcher :task_definition?, <<-PATTERN
|
32
|
+
(send nil? :task ...)
|
33
|
+
PATTERN
|
34
|
+
|
35
|
+
def on_send(node)
|
36
|
+
return unless task_definition?(node)
|
37
|
+
return if task_name(node) == :default
|
38
|
+
return if with_dependencies?(node)
|
39
|
+
|
40
|
+
add_offense(node)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def task_name(node)
|
46
|
+
first_arg = node.arguments[0]
|
47
|
+
case first_arg&.type
|
48
|
+
when :sym, :str
|
49
|
+
first_arg.value.to_sym
|
50
|
+
when :hash
|
51
|
+
return nil if first_arg.children.size != 1
|
52
|
+
|
53
|
+
pair = first_arg.children.first
|
54
|
+
key = pair.children.first
|
55
|
+
case key.type
|
56
|
+
when :sym, :str
|
57
|
+
key.value.to_sym
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def with_dependencies?(node)
|
63
|
+
first_arg = node.arguments[0]
|
64
|
+
return false unless first_arg
|
65
|
+
|
66
|
+
if first_arg.hash_type?
|
67
|
+
with_hash_style_dependencies?(first_arg)
|
68
|
+
else
|
69
|
+
task_args = node.arguments[1]
|
70
|
+
return false unless task_args
|
71
|
+
return false unless task_args.hash_type?
|
72
|
+
|
73
|
+
with_hash_style_dependencies?(task_args)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def with_hash_style_dependencies?(hash_node)
|
78
|
+
deps = hash_node.pairs.first&.value
|
79
|
+
return false unless deps
|
80
|
+
|
81
|
+
case deps.type
|
82
|
+
when :array
|
83
|
+
!deps.values.empty?
|
84
|
+
else
|
85
|
+
true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -36,7 +36,7 @@ module RuboCop
|
|
36
36
|
'`allow_nil: false` is redundant when `allow_blank` is true.'
|
37
37
|
|
38
38
|
def on_send(node)
|
39
|
-
return unless node.
|
39
|
+
return unless node.method?(:validates)
|
40
40
|
|
41
41
|
allow_nil, allow_blank = find_allow_nil_and_allow_blank(node)
|
42
42
|
return unless allow_nil && allow_blank
|
@@ -127,22 +127,15 @@ module RuboCop
|
|
127
127
|
# @see https://api.rubyonrails.org/classes/ActiveRecord/Migration/CommandRecorder.html
|
128
128
|
class ReversibleMigration < Cop
|
129
129
|
MSG = '%<action>s is not reversible.'
|
130
|
-
IRREVERSIBLE_CHANGE_TABLE_CALLS = %i[
|
131
|
-
change change_default remove
|
132
|
-
].freeze
|
133
130
|
|
134
131
|
def_node_matcher :irreversible_schema_statement_call, <<-PATTERN
|
135
|
-
(send nil? ${:
|
132
|
+
(send nil? ${:execute :remove_belongs_to} ...)
|
136
133
|
PATTERN
|
137
134
|
|
138
135
|
def_node_matcher :drop_table_call, <<-PATTERN
|
139
136
|
(send nil? :drop_table ...)
|
140
137
|
PATTERN
|
141
138
|
|
142
|
-
def_node_matcher :change_column_default_call, <<-PATTERN
|
143
|
-
(send nil? :change_column_default {[(sym _) (sym _)] (splat _)} $...)
|
144
|
-
PATTERN
|
145
|
-
|
146
139
|
def_node_matcher :remove_column_call, <<-PATTERN
|
147
140
|
(send nil? :remove_column $...)
|
148
141
|
PATTERN
|
@@ -161,7 +154,7 @@ module RuboCop
|
|
161
154
|
|
162
155
|
check_irreversible_schema_statement_node(node)
|
163
156
|
check_drop_table_node(node)
|
164
|
-
|
157
|
+
check_reversible_hash_node(node)
|
165
158
|
check_remove_column_node(node)
|
166
159
|
check_remove_foreign_key_node(node)
|
167
160
|
end
|
@@ -193,17 +186,15 @@ module RuboCop
|
|
193
186
|
end
|
194
187
|
end
|
195
188
|
|
196
|
-
def
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
end
|
206
|
-
end
|
189
|
+
def check_reversible_hash_node(node)
|
190
|
+
return if reversible_change_table_call?(node)
|
191
|
+
|
192
|
+
add_offense(
|
193
|
+
node,
|
194
|
+
message: format(
|
195
|
+
MSG, action: "#{node.method_name}(without :from and :to)"
|
196
|
+
)
|
197
|
+
)
|
207
198
|
end
|
208
199
|
|
209
200
|
def check_remove_column_node(node)
|
@@ -244,7 +235,7 @@ module RuboCop
|
|
244
235
|
def check_change_table_offense(receiver, node)
|
245
236
|
method_name = node.method_name
|
246
237
|
return if receiver != node.receiver &&
|
247
|
-
|
238
|
+
reversible_change_table_call?(node)
|
248
239
|
|
249
240
|
add_offense(
|
250
241
|
node,
|
@@ -252,6 +243,18 @@ module RuboCop
|
|
252
243
|
)
|
253
244
|
end
|
254
245
|
|
246
|
+
def reversible_change_table_call?(node)
|
247
|
+
case node.method_name
|
248
|
+
when :change, :remove
|
249
|
+
false
|
250
|
+
when :change_default, :change_column_default, :change_table_comment,
|
251
|
+
:change_column_comment
|
252
|
+
all_hash_key?(node.arguments.last, :from, :to)
|
253
|
+
else
|
254
|
+
true
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
255
258
|
def within_change_method?(node)
|
256
259
|
node.each_ancestor(:def).any? do |ancestor|
|
257
260
|
ancestor.method?(:change)
|
@@ -6,38 +6,36 @@ module RuboCop
|
|
6
6
|
# This cop converts usages of `try!` to `&.`. It can also be configured
|
7
7
|
# to convert `try`. It will convert code to use safe navigation.
|
8
8
|
#
|
9
|
-
# @example
|
10
|
-
# #
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# foo.try!(:bar) { |e| e.baz }
|
9
|
+
# @example ConvertTry: false (default)
|
10
|
+
# # bad
|
11
|
+
# foo.try!(:bar)
|
12
|
+
# foo.try!(:bar, baz)
|
13
|
+
# foo.try!(:bar) { |e| e.baz }
|
15
14
|
#
|
16
|
-
#
|
15
|
+
# foo.try!(:[], 0)
|
17
16
|
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
17
|
+
# # good
|
18
|
+
# foo.try(:bar)
|
19
|
+
# foo.try(:bar, baz)
|
20
|
+
# foo.try(:bar) { |e| e.baz }
|
22
21
|
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
22
|
+
# foo&.bar
|
23
|
+
# foo&.bar(baz)
|
24
|
+
# foo&.bar { |e| e.baz }
|
26
25
|
#
|
26
|
+
# @example ConvertTry: true
|
27
|
+
# # bad
|
28
|
+
# foo.try!(:bar)
|
29
|
+
# foo.try!(:bar, baz)
|
30
|
+
# foo.try!(:bar) { |e| e.baz }
|
31
|
+
# foo.try(:bar)
|
32
|
+
# foo.try(:bar, baz)
|
33
|
+
# foo.try(:bar) { |e| e.baz }
|
27
34
|
#
|
28
|
-
# #
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
# foo.try!(:bar) { |e| e.baz }
|
33
|
-
# foo.try(:bar)
|
34
|
-
# foo.try(:bar, baz)
|
35
|
-
# foo.try(:bar) { |e| e.baz }
|
36
|
-
#
|
37
|
-
# # good
|
38
|
-
# foo&.bar
|
39
|
-
# foo&.bar(baz)
|
40
|
-
# foo&.bar { |e| e.baz }
|
35
|
+
# # good
|
36
|
+
# foo&.bar
|
37
|
+
# foo&.bar(baz)
|
38
|
+
# foo&.bar { |e| e.baz }
|
41
39
|
class SafeNavigation < Cop
|
42
40
|
include RangeHelp
|
43
41
|
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# This cop checks to make sure safe navigation isn't used with `blank?` in
|
7
|
+
# a conditional.
|
8
|
+
#
|
9
|
+
# While the safe navigation operator is generally a good idea, when
|
10
|
+
# checking `foo&.blank?` in a conditional, `foo` being `nil` will actually
|
11
|
+
# do the opposite of what the author intends.
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# # bad
|
15
|
+
# do_something if foo&.blank?
|
16
|
+
# do_something unless foo&.blank?
|
17
|
+
#
|
18
|
+
# # good
|
19
|
+
# do_something if foo.blank?
|
20
|
+
# do_something unless foo.blank?
|
21
|
+
#
|
22
|
+
class SafeNavigationWithBlank < Cop
|
23
|
+
MSG =
|
24
|
+
'Avoid calling `blank?` with the safe navigation operator ' \
|
25
|
+
'in conditionals.'
|
26
|
+
|
27
|
+
def_node_matcher :safe_navigation_blank_in_conditional?, <<-PATTERN
|
28
|
+
(if $(csend ... :blank?) ...)
|
29
|
+
PATTERN
|
30
|
+
|
31
|
+
def on_if(node)
|
32
|
+
return unless safe_navigation_blank_in_conditional?(node)
|
33
|
+
|
34
|
+
add_offense(node)
|
35
|
+
end
|
36
|
+
|
37
|
+
def autocorrect(node)
|
38
|
+
lambda do |corrector|
|
39
|
+
corrector.replace(
|
40
|
+
safe_navigation_blank_in_conditional?(node).location.dot,
|
41
|
+
'.'
|
42
|
+
)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -141,7 +141,7 @@ module RuboCop
|
|
141
141
|
return unless persist_method?(node)
|
142
142
|
return if return_value_assigned?(node)
|
143
143
|
return if implicit_return?(node)
|
144
|
-
return if
|
144
|
+
return if check_used_in_condition_or_compound_boolean(node)
|
145
145
|
return if argument?(node)
|
146
146
|
return if explicit_return?(node)
|
147
147
|
|
@@ -211,8 +211,8 @@ module RuboCop
|
|
211
211
|
array
|
212
212
|
end
|
213
213
|
|
214
|
-
def
|
215
|
-
return false unless
|
214
|
+
def check_used_in_condition_or_compound_boolean(node)
|
215
|
+
return false unless in_condition_or_compound_boolean?(node)
|
216
216
|
|
217
217
|
unless MODIFY_PERSIST_METHODS.include?(node.method_name)
|
218
218
|
add_offense_for_node(node, CREATE_CONDITIONAL_MSG)
|
@@ -221,15 +221,21 @@ module RuboCop
|
|
221
221
|
true
|
222
222
|
end
|
223
223
|
|
224
|
-
def
|
224
|
+
def in_condition_or_compound_boolean?(node)
|
225
225
|
node = node.block_node || node
|
226
|
+
parent = node.parent
|
227
|
+
return false unless parent
|
226
228
|
|
227
|
-
|
228
|
-
|
229
|
+
operator_or_single_negative?(parent) ||
|
230
|
+
(conditional?(parent) && node == parent.condition)
|
231
|
+
end
|
232
|
+
|
233
|
+
def operator_or_single_negative?(node)
|
234
|
+
node.or_type? || node.and_type? || single_negative?(node)
|
235
|
+
end
|
229
236
|
|
230
|
-
|
231
|
-
|
232
|
-
single_negative?(condition)
|
237
|
+
def conditional?(parent)
|
238
|
+
parent.if_type? || parent.case_type?
|
233
239
|
end
|
234
240
|
|
235
241
|
def allowed_receiver?(node)
|
@@ -249,7 +255,7 @@ module RuboCop
|
|
249
255
|
if node.variable?
|
250
256
|
node.node_parts.first == receiver_part.to_sym
|
251
257
|
elsif node.send_type?
|
252
|
-
node.
|
258
|
+
node.method?(receiver_part.to_sym)
|
253
259
|
elsif node.const_type?
|
254
260
|
const_matches?(node.const_name, receiver_part)
|
255
261
|
end
|
@@ -9,9 +9,11 @@ module RuboCop
|
|
9
9
|
# @example
|
10
10
|
# # bad
|
11
11
|
# Rails.env.proudction?
|
12
|
+
# Rails.env == 'proudction'
|
12
13
|
#
|
13
14
|
# # good
|
14
15
|
# Rails.env.production?
|
16
|
+
# Rails.env == 'production'
|
15
17
|
class UnknownEnv < Cop
|
16
18
|
include NameSimilarity
|
17
19
|
|
@@ -19,28 +21,43 @@ module RuboCop
|
|
19
21
|
MSG_SIMILAR = 'Unknown environment `%<name>s`. ' \
|
20
22
|
'Did you mean `%<similar>s`?'
|
21
23
|
|
22
|
-
def_node_matcher :
|
24
|
+
def_node_matcher :rails_env?, <<~PATTERN
|
23
25
|
(send
|
24
|
-
(
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
{(const nil? :Rails) (const (cbase) :Rails)}
|
27
|
+
:env)
|
28
|
+
PATTERN
|
29
|
+
|
30
|
+
def_node_matcher :unknown_environment_predicate?, <<~PATTERN
|
31
|
+
(send #rails_env? $#unknown_env_predicate?)
|
32
|
+
PATTERN
|
33
|
+
|
34
|
+
def_node_matcher :unknown_environment_equal?, <<-PATTERN
|
35
|
+
{
|
36
|
+
(send #rails_env? {:== :===} $(str #unknown_env_name?))
|
37
|
+
(send $(str #unknown_env_name?) {:== :===} #rails_env?)
|
38
|
+
}
|
28
39
|
PATTERN
|
29
40
|
|
30
41
|
def on_send(node)
|
31
|
-
|
42
|
+
unknown_environment_predicate?(node) do |name|
|
32
43
|
add_offense(node, location: :selector, message: message(name))
|
33
44
|
end
|
45
|
+
|
46
|
+
unknown_environment_equal?(node) do |str_node|
|
47
|
+
name = str_node.value
|
48
|
+
add_offense(str_node, message: message(name))
|
49
|
+
end
|
34
50
|
end
|
35
51
|
|
36
52
|
private
|
37
53
|
|
38
54
|
def collect_variable_like_names(_scope)
|
39
|
-
environments
|
55
|
+
environments
|
40
56
|
end
|
41
57
|
|
42
58
|
def message(name)
|
43
|
-
|
59
|
+
name = name.to_s.chomp('?')
|
60
|
+
similar = find_similar_name(name, [])
|
44
61
|
if similar
|
45
62
|
format(MSG_SIMILAR, name: name, similar: similar)
|
46
63
|
else
|
@@ -48,12 +65,16 @@ module RuboCop
|
|
48
65
|
end
|
49
66
|
end
|
50
67
|
|
51
|
-
def
|
68
|
+
def unknown_env_predicate?(name)
|
52
69
|
name = name.to_s
|
53
70
|
name.end_with?('?') &&
|
54
71
|
!environments.include?(name[0..-2])
|
55
72
|
end
|
56
73
|
|
74
|
+
def unknown_env_name?(name)
|
75
|
+
!environments.include?(name)
|
76
|
+
end
|
77
|
+
|
57
78
|
def environments
|
58
79
|
cop_config['Environments']
|
59
80
|
end
|
@@ -6,7 +6,9 @@ require_relative 'rails/action_filter'
|
|
6
6
|
require_relative 'rails/active_record_aliases'
|
7
7
|
require_relative 'rails/active_record_override'
|
8
8
|
require_relative 'rails/active_support_aliases'
|
9
|
+
require_relative 'rails/application_controller'
|
9
10
|
require_relative 'rails/application_job'
|
11
|
+
require_relative 'rails/application_mailer'
|
10
12
|
require_relative 'rails/application_record'
|
11
13
|
require_relative 'rails/assert_not'
|
12
14
|
require_relative 'rails/belongs_to'
|
@@ -39,6 +41,7 @@ require_relative 'rails/output_safety'
|
|
39
41
|
require_relative 'rails/pluralization_grammar'
|
40
42
|
require_relative 'rails/presence'
|
41
43
|
require_relative 'rails/present'
|
44
|
+
require_relative 'rails/rake_environment'
|
42
45
|
require_relative 'rails/read_write_attribute'
|
43
46
|
require_relative 'rails/redundant_allow_nil'
|
44
47
|
require_relative 'rails/redundant_receiver_in_with_options'
|
@@ -48,6 +51,7 @@ require_relative 'rails/relative_date_constant'
|
|
48
51
|
require_relative 'rails/request_referer'
|
49
52
|
require_relative 'rails/reversible_migration'
|
50
53
|
require_relative 'rails/safe_navigation'
|
54
|
+
require_relative 'rails/safe_navigation_with_blank'
|
51
55
|
require_relative 'rails/save_bang'
|
52
56
|
require_relative 'rails/scope_args'
|
53
57
|
require_relative 'rails/skips_model_validations'
|
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.
|
4
|
+
version: 2.4.0
|
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: 2019-
|
13
|
+
date: 2019-11-27 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rack
|
@@ -60,7 +60,9 @@ files:
|
|
60
60
|
- lib/rubocop/cop/rails/active_record_aliases.rb
|
61
61
|
- lib/rubocop/cop/rails/active_record_override.rb
|
62
62
|
- lib/rubocop/cop/rails/active_support_aliases.rb
|
63
|
+
- lib/rubocop/cop/rails/application_controller.rb
|
63
64
|
- lib/rubocop/cop/rails/application_job.rb
|
65
|
+
- lib/rubocop/cop/rails/application_mailer.rb
|
64
66
|
- lib/rubocop/cop/rails/application_record.rb
|
65
67
|
- lib/rubocop/cop/rails/assert_not.rb
|
66
68
|
- lib/rubocop/cop/rails/belongs_to.rb
|
@@ -93,6 +95,7 @@ files:
|
|
93
95
|
- lib/rubocop/cop/rails/pluralization_grammar.rb
|
94
96
|
- lib/rubocop/cop/rails/presence.rb
|
95
97
|
- lib/rubocop/cop/rails/present.rb
|
98
|
+
- lib/rubocop/cop/rails/rake_environment.rb
|
96
99
|
- lib/rubocop/cop/rails/read_write_attribute.rb
|
97
100
|
- lib/rubocop/cop/rails/redundant_allow_nil.rb
|
98
101
|
- lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb
|
@@ -102,6 +105,7 @@ files:
|
|
102
105
|
- lib/rubocop/cop/rails/request_referer.rb
|
103
106
|
- lib/rubocop/cop/rails/reversible_migration.rb
|
104
107
|
- lib/rubocop/cop/rails/safe_navigation.rb
|
108
|
+
- lib/rubocop/cop/rails/safe_navigation_with_blank.rb
|
105
109
|
- lib/rubocop/cop/rails/save_bang.rb
|
106
110
|
- lib/rubocop/cop/rails/scope_args.rb
|
107
111
|
- lib/rubocop/cop/rails/skips_model_validations.rb
|
@@ -137,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
141
|
- !ruby/object:Gem::Version
|
138
142
|
version: '0'
|
139
143
|
requirements: []
|
140
|
-
rubygems_version: 3.0.
|
144
|
+
rubygems_version: 3.0.3
|
141
145
|
signing_key:
|
142
146
|
specification_version: 4
|
143
147
|
summary: Automatic Rails code style checking tool.
|