Skip to content

Commit

Permalink
Make Rails/UnknownEnv cop aware of Rails.env == 'unknown_env'
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Nov 15, 2019
1 parent 69ae4cb commit 23caaa0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* [#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][])
* [#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][])

### Changes

* [#156](https://github.com/rubocop-hq/rubocop-rails/pull/156): Make `Rails/UnknownEnv` cop aware of `Rails.env == 'unknown_env'`. ([@pocke][])

## 2.3.2 (2019-09-01)

### Bug fixes
Expand Down
39 changes: 30 additions & 9 deletions lib/rubocop/cop/rails/unknown_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,72 @@ module Rails
# @example
# # bad
# Rails.env.proudction?
# Rails.env == 'proudction'
#
# # good
# Rails.env.production?
# Rails.env == 'production'
class UnknownEnv < Cop
include NameSimilarity

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

def_node_matcher :unknown_environment?, <<-PATTERN
def_node_matcher :rails_env?, <<~PATTERN
(send
(send
{(const nil? :Rails) (const (cbase) :Rails)}
:env)
$#unknown_env_name?)
{(const nil? :Rails) (const (cbase) :Rails)}
:env)
PATTERN

def_node_matcher :unknown_environment_predicate?, <<~PATTERN
(send #rails_env? $#unknown_env_predicate?)
PATTERN

def_node_matcher :unknown_environment_equal?, <<-PATTERN
{
(send #rails_env? {:== :===} $(str #unknown_env_name?))
(send $(str #unknown_env_name?) {:== :===} #rails_env?)
}
PATTERN

def on_send(node)
unknown_environment?(node) do |name|
unknown_environment_predicate?(node) do |name|
add_offense(node, location: :selector, message: message(name))
end

unknown_environment_equal?(node) do |str_node|
name = str_node.value
add_offense(str_node, message: message(name))
end
end

private

def collect_variable_like_names(_scope)
environments.map { |env| env + '?' }
environments
end

def message(name)
similar = find_similar_name(name.to_s, [])
name = name.to_s.chomp('?')
similar = find_similar_name(name, [])
if similar
format(MSG_SIMILAR, name: name, similar: similar)
else
format(MSG, name: name)
end
end

def unknown_env_name?(name)
def unknown_env_predicate?(name)
name = name.to_s
name.end_with?('?') &&
!environments.include?(name[0..-2])
end

def unknown_env_name?(name)
!environments.include?(name)
end

def environments
cop_config['Environments']
end
Expand Down
2 changes: 2 additions & 0 deletions manual/cops_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -2502,9 +2502,11 @@ exist.
```ruby
# bad
Rails.env.proudction?
Rails.env == 'proudction'

# good
Rails.env.production?
Rails.env == 'production'
```

### Configurable attributes
Expand Down
23 changes: 18 additions & 5 deletions spec/rubocop/cop/rails/unknown_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,32 @@
end

it 'registers an offense for typo of environment name' do
expect_offense(<<-RUBY)
expect_offense(<<~RUBY)
Rails.env.proudction?
^^^^^^^^^^^ Unknown environment `proudction?`. Did you mean `production?`?
^^^^^^^^^^^ Unknown environment `proudction`. Did you mean `production`?
Rails.env.developpment?
^^^^^^^^^^^^^ Unknown environment `developpment?`. Did you mean `development?`?
^^^^^^^^^^^^^ Unknown environment `developpment`. Did you mean `development`?
Rails.env.something?
^^^^^^^^^^ Unknown environment `something?`.
^^^^^^^^^^ Unknown environment `something`.
RUBY
end

it 'registers an offense for typo of environment name with `==` operator' do
expect_offense(<<~RUBY)
Rails.env == 'proudction'
^^^^^^^^^^^^ Unknown environment `proudction`. Did you mean `production`?
'developpment' == Rails.env
^^^^^^^^^^^^^^ Unknown environment `developpment`. Did you mean `development`?
'something' === Rails.env
^^^^^^^^^^^ Unknown environment `something`.
RUBY
end

it 'accepts correct environment name' do
expect_no_offenses(<<-RUBY)
expect_no_offenses(<<~RUBY)
Rails.env.production?
Rails.env == 'production'
RUBY
end
end

0 comments on commit 23caaa0

Please sign in to comment.