8000 Merge pull request #156 from pocke/Rails/UnknownEnv-more-detection · rubocop/rubocop-rails@72c0fdd · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 72c0fdd

Browse files
authored
Merge pull request #156 from pocke/Rails/UnknownEnv-more-detection
Make `Rails/UnknownEnv` cop aware of `Rails.env == 'unknown_env'`
2 parents d2a1a4c + 23caaa0 commit 72c0fdd

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
* [#136](https://github.com/rubocop-hq/rubocop-rails/pull/136): Fix a false positive for `Rails/ReversibleMigration` when using `change_default` with `:from` and `:to` options. ([@sinsoku][])
1717
* [#144](https://github.com/rubocop-hq/rubocop-rails/issues/144): Fix a false positive for `Rails/ReversibleMigration` when using `change_table_comment` or `change_column_comment` with a `:from` and `:to` hash. ([@DNA][])
1818

19+
### Changes
20+
21+
* [#156](https://github.com/rubocop-hq/rubocop-rails/pull/156): Make `Rails/UnknownEnv` cop aware of `Rails.env == 'unknown_env'`. ([@pocke][])
22+
1923
## 2.3.2 (2019-09-01)
2024

2125
### Bug fixes

lib/rubocop/cop/rails/unknown_env.rb

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,72 @@ module Rails
99
# @example
1010
# # bad
1111
# Rails.env.proudction?
12+
# Rails.env == 'proudction'
1213
#
1314
# # good
1415
# Rails.env.production?
16+
# Rails.env == 'production'
1517
class UnknownEnv < Cop
1618
include NameSimilarity
1719

1820
MSG = 'Unknown environment `%<name>s`.'
1921
MSG_SIMILAR = 'Unknown environment `%<name>s`. ' \
2022
'Did you mean `%<similar>s`?'
2123

22-
def_node_matcher :unknown_environment?, <<-PATTERN
24+
def_node_matcher :rails_env?, <<~PATTERN
2325
(send
24-
(send
25-
{(const nil? :Rails) (const (cbase) :Rails)}
26-
:env)
27-
$#unknown_env_name?)
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+
}
2839
PATTERN
2940

3041
def on_send(node)
31-
unknown_environment?(node) do |name|
42+
unknown_environment_predicate?(node) do |name|
3243
add_offense(node, location: :selector, message: message(name))
3344
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
3450
end
3551

3652
private
3753

3854
def collect_variable_like_names(_scope)
39-
environments.map { |env| env + '?' }
55+
environments
4056
end
4157

4258
def message(name)
43-
similar = find_similar_name(name.to_s, [])
59+
name = name.to_s.chomp('?')
60+
similar = find_similar_name(name, [])
4461
if similar
4562
format(MSG_SIMILAR, name: name, similar: similar)
4663
else
4764
format(MSG, name: name)
4865
end
4966
end
5067

51-
def unknown_env_name?(name)
68+
def unknown_env_predicate?(name)
5269
name = name.to_s
5370
name.end_with?('?') &&
5471
!environments.include?(name[0..-2])
5572
end
5673

74+
def unknown_env_name?(name)
75+
!environments.include?(name)
76+
end
77+
5778
def environments
5879
cop_config['Environments']
5980
end

manual/cops_rails.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,9 +2502,11 @@ exist.
25022502
```ruby
25032503
# bad
25042504
Rails.env.proudction?
2505+
Rails.env == 'proudction'
25052506

25062507
# good
25072508
Rails.env.production?
2509+
Rails.env == 'production'
25082510
```
25092511

25102512
### Configurable attributes

spec/rubocop/cop/rails/unknown_env_spec.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,32 @@
1414
end
1515

1616
it 'registers an offense for typo of environment name' do
17-
expect_offense(<<-RUBY)
17+
expect_offense(<<~RUBY)
1818
Rails.env.proudction?
19-
^^^^^^^^^^^ Unknown environment `proudction?`. Did you mean `production?`?
19+
^^^^^^^^^^^ Unknown environment `proudction`. Did you mean `production`?
2020
Rails.env.developpment?
21-
^^^^^^^^^^^^^ Unknown environment `developpment?`. Did you mean `development?`?
21+
^^^^^^^^^^^^^ Unknown environment `developpment`. Did you mean `development`?
2222
Rails.env.something?
23-
^^^^^^^^^^ Unknown environment `something?`.
23+
^^^^^^^^^^ Unknown environment `something`.
24+
RUBY
25+
end
26+
27+
it 'registers an offense for typo of environment name with `==` operator' do
28+
expect_offense(<<~RUBY)
29+
Rails.env == 'proudction'
30+
^^^^^^^^^^^^ Unknown environment `proudction`. Did you mean `production`?
31+
'developpment' == Rails.env
32+
^^^^^^^^^^^^^^ Unknown environment `developpment`. Did you mean `development`?
33+
34+
'something' === Rails.env
35+
^^^^^^^^^^^ Unknown environment `something`.
2436
RUBY
2537
end
2638

2739
it 'accepts correct environment name' do
28-
expect_no_offenses(<<-RUBY)
40+
expect_no_offenses(<<~RUBY)
2941
Rails.env.production?
42+
Rails.env == 'production'
3043
RUBY
3144
end
3245
end

0 commit comments

Comments
 (0)
0