Skip to content

Commit 528699c

Browse files
committed
Fix false negative for != comparison in Rails/UnknownEnv
1 parent 1f80c6a commit 528699c

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1592](https://github.com/rubocop/rubocop-rails/pull/1592): Fix false negative for `!=` comparison in `Rails/UnknownEnv`. ([@lovro-bikic][])

lib/rubocop/cop/rails/unknown_env.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ module Rails
1313
# # bad
1414
# Rails.env.proudction?
1515
# Rails.env == 'proudction'
16+
# Rails.env != 'proudction'
1617
#
1718
# # good
1819
# Rails.env.production?
1920
# Rails.env == 'production'
21+
# Rails.env != 'production'
2022
#
2123
# @example
2224
# # bad
@@ -47,8 +49,8 @@ class UnknownEnv < Base
4749

4850
def_node_matcher :unknown_environment_equal?, <<~PATTERN
4951
{
50-
(send #rails_env? {:== :===} $(str #unknown_env_name?))
51-
(send $(str #unknown_env_name?) {:== :===} #rails_env?)
52+
(send #rails_env? {:== :=== :!=} $(str #unknown_env_name?))
53+
(send $(str #unknown_env_name?) {:== :=== :!=} #rails_env?)
5254
}
5355
PATTERN
5456

spec/rubocop/cop/rails/unknown_env_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@
4444
RUBY
4545
end
4646

47+
it 'registers an offense for typo of environment name with `!=` operator' do
48+
expect_offense(<<~RUBY)
49+
Rails.env != 'proudction'
50+
^^^^^^^^^^^^ Unknown environment `proudction`. Did you mean `production`?
51+
'developpment' != Rails.env
52+
^^^^^^^^^^^^^^ Unknown environment `developpment`. Did you mean `development`?
53+
RUBY
54+
end
55+
4756
it 'registers an offense when case condition is an unknown environment name' do
4857
expect_offense(<<~RUBY)
4958
case Rails.env

0 commit comments

Comments
 (0)