diff --git a/CHANGELOG.md b/CHANGELOG.md index 13df636ab8..07d3d68c52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1152,3 +1152,4 @@ [@fwolfst]: https://github.com/fwolfst [@maxprokopiev]: https://github.com/maxprokopiev [@ytjmt]: https://github.com/ytjmt +[@cdudas17]: https://github.com/cdudas17 diff --git a/changelog/new_env_cop.md b/changelog/new_env_cop.md new file mode 100644 index 0000000000..3a97499a04 --- /dev/null +++ b/changelog/new_env_cop.md @@ -0,0 +1 @@ +* [#1375](https://github.com/rubocop/rubocop-rails/pull/1375): Add new `Rails/Env` cop. ([@cdudas17][]) diff --git a/config/default.yml b/config/default.yml index 0e7d183460..4099cfefa2 100644 --- a/config/default.yml +++ b/config/default.yml @@ -441,6 +441,11 @@ Rails/EnumUniqueness: Include: - app/models/**/*.rb +Rails/Env: + Description: 'Use Feature Flags instead of `Rails.env`.' + Enabled: pending + VersionAdded: '<>' + Rails/EnvLocal: Description: 'Use `Rails.env.local?` instead of `Rails.env.development? || Rails.env.test?`.' Enabled: pending diff --git a/lib/rubocop/cop/rails/env.rb b/lib/rubocop/cop/rails/env.rb new file mode 100644 index 0000000000..e4bb0efe4b --- /dev/null +++ b/lib/rubocop/cop/rails/env.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Rails + # Checks for usage of `Rails.env` which can be replaced with Feature Flags + # + # @example + # + # # bad + # Rails.env.development? || Rails.env.production? || Rails.env.local? + # + # # good + # if FeatureFlag.enabled?(:new_feature) + # # new feature code + # end + # + class Env < Base + MSG = 'Use Feature Flags instead of `Rails.env`.' + RESTRICT_ON_SEND = %i[env].freeze + + def on_send(node) + return unless node.receiver&.const_name == 'Rails' + return unless node.parent&.method_name + + add_offense(node) + end + end + end + end +end diff --git a/lib/rubocop/cop/rails_cops.rb b/lib/rubocop/cop/rails_cops.rb index b7eddc4fe4..c966c86f13 100644 --- a/lib/rubocop/cop/rails_cops.rb +++ b/lib/rubocop/cop/rails_cops.rb @@ -48,6 +48,7 @@ require_relative 'rails/enum_hash' require_relative 'rails/enum_syntax' require_relative 'rails/enum_uniqueness' +require_relative 'rails/env' require_relative 'rails/env_local' require_relative 'rails/environment_comparison' require_relative 'rails/environment_variable_access' diff --git a/spec/rubocop/cop/rails/env_spec.rb b/spec/rubocop/cop/rails/env_spec.rb new file mode 100644 index 0000000000..ca06645f46 --- /dev/null +++ b/spec/rubocop/cop/rails/env_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::Cop::Rails::Env, :config do + it 'registers an offense for `Rails.env.development? || Rails.env.test?`' do + expect_offense(<<~RUBY) + Rails.env.development? || Rails.env.test? + ^^^^^^^^^ Use Feature Flags instead of `Rails.env`. + ^^^^^^^^^ Use Feature Flags instead of `Rails.env`. + RUBY + end + + it 'registers an offense for `Rails.env.production?`' do + expect_offense(<<~RUBY) + Rails.env.production? + ^^^^^^^^^ Use Feature Flags instead of `Rails.env`. + RUBY + end + + it 'does not register an offense for `Rails.env`' do + expect_no_offenses(<<~RUBY) + Rails.env + RUBY + end + + it 'does not register an offense for unrelated config' do + expect_no_offenses(<<~RUBY) + Rails.environment + RUBY + end +end