diff --git a/CHANGELOG.md b/CHANGELOG.md index ed9beefbd4..4a5de17263 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/rails/unknown_env.rb b/lib/rubocop/cop/rails/unknown_env.rb index b41f5148ec..3157603815 100644 --- a/lib/rubocop/cop/rails/unknown_env.rb +++ b/lib/rubocop/cop/rails/unknown_env.rb @@ -9,9 +9,11 @@ module Rails # @example # # bad # Rails.env.proudction? + # Rails.env == 'proudction' # # # good # Rails.env.production? + # Rails.env == 'production' class UnknownEnv < Cop include NameSimilarity @@ -19,28 +21,43 @@ class UnknownEnv < Cop MSG_SIMILAR = 'Unknown environment `%s`. ' \ 'Did you mean `%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 @@ -48,12 +65,16 @@ def message(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 diff --git a/manual/cops_rails.md b/manual/cops_rails.md index 97b8e39e7a..9535247b70 100644 --- a/manual/cops_rails.md +++ b/manual/cops_rails.md @@ -2502,9 +2502,11 @@ exist. ```ruby # bad Rails.env.proudction? +Rails.env == 'proudction' # good Rails.env.production? +Rails.env == 'production' ``` ### Configurable attributes diff --git a/spec/rubocop/cop/rails/unknown_env_spec.rb b/spec/rubocop/cop/rails/unknown_env_spec.rb index 696e473311..cbf344797c 100644 --- a/spec/rubocop/cop/rails/unknown_env_spec.rb +++ b/spec/rubocop/cop/rails/unknown_env_spec.rb @@ -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