File tree 6 files changed +66
-0
lines changed
6 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 1152
1152
[ @fwolfst ] : https://github.com/fwolfst
1153
1153
[ @maxprokopiev ] : https://github.com/maxprokopiev
1154
1154
[ @ytjmt ] : https://github.com/ytjmt
1155
+ [ @cdudas17 ] : https://github.com/cdudas17
Original file line number Diff line number Diff line change
1
+ * [ #1375 ] ( https://github.com/rubocop/rubocop-rails/pull/1375 ) : Add new ` Rails/Env ` cop. ([ @cdudas17 ] [ ] )
Original file line number Diff line number Diff line change @@ -441,6 +441,11 @@ Rails/EnumUniqueness:
441
441
Include :
442
442
- app/models/**/*.rb
443
443
444
+ Rails/Env :
445
+ Description : ' Use Feature Flags instead of `Rails.env`.'
446
+ Enabled : pending
447
+ VersionAdded : ' <<next>>'
448
+
444
449
Rails/EnvLocal :
445
450
Description : ' Use `Rails.env.local?` instead of `Rails.env.development? || Rails.env.test?`.'
446
451
Enabled : pending
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Checks for usage of `Rails.env` which can be replaced with Feature Flags
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # Rails.env.development? || Rails.env.production? || Rails.env.local?
12
+ #
13
+ # # good
14
+ # if Feature.enabled?(:new_feature)
15
+ # # new feature code
16
+ # end
17
+ #
18
+ class Env < Base
19
+ MSG = 'Use Feature Flags instead of `Rails.env`.'
20
+ # RESTRICT_ON_SEND = %i[env].freeze
21
+
22
+ def_node_matcher :rails_env_any? , <<~PATTERN
23
+ (send (send (const {nil? cbase} :Rails) :env) ...)
24
+ PATTERN
25
+
26
+ def on_send ( node )
27
+ rails_env_any? ( node ) do
28
+ add_offense ( node )
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
Original file line number Diff line number Diff line change 48
48
require_relative 'rails/enum_hash'
49
49
require_relative 'rails/enum_syntax'
50
50
require_relative 'rails/enum_uniqueness'
51
+ require_relative 'rails/env'
51
52
require_relative 'rails/env_local'
52
53
require_relative 'rails/environment_comparison'
53
54
require_relative 'rails/environment_variable_access'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ RSpec . describe RuboCop ::Cop ::Rails ::Env , :config do
4
+ it 'registers an offense for `Rails.env.development? || Rails.env.test?`' do
5
+ expect_offense ( <<~RUBY )
6
+ Rails.env.development? || Rails.env.test?
7
+ ^^^^^^^^^^^^^^^ Use Feature Flags instead of `Rails.env`.
8
+ ^^^^^^^^^^^^^^^^^^^^^^ Use Feature Flags instead of `Rails.env`.
9
+ RUBY
10
+ end
11
+
12
+ it 'registers an offense for `Rails.env.production?`' do
13
+ expect_offense ( <<~RUBY )
14
+ Rails.env.production?
15
+ ^^^^^^^^^^^^^^^^^^^^^ Use Feature Flags instead of `Rails.env`.
16
+ RUBY
17
+ end
18
+
19
+ it 'does not register an offense for unrelated config' do
20
+ expect_no_offenses ( <<~RUBY )
21
+ Rails.environment
22
+ RUBY
23
+ end
24
+ end
You can’t perform that action at this time.
0 commit comments