diff --git a/CHANGELOG.md b/CHANGELOG.md index 633609c5..3a9dcde1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Fix a false positive for `FactoryBot/FactoryNameStyle` when namespaced models. ([@ydah]) + ## 2.24.0 (2023-09-18) - Fix `FactoryBot/AssociationStyle` cop to ignore explicit associations with `strategy: :build`. ([@pirj]) diff --git a/docs/modules/ROOT/pages/cops_factorybot.adoc b/docs/modules/ROOT/pages/cops_factorybot.adoc index 6f42fb6d..3932abf4 100644 --- a/docs/modules/ROOT/pages/cops_factorybot.adoc +++ b/docs/modules/ROOT/pages/cops_factorybot.adoc @@ -464,6 +464,9 @@ build "user", username: "NAME" # good create(:user) build :user, username: "NAME" + +# good - namespaced models +create('users/internal') ---- ==== EnforcedStyle: string diff --git a/lib/rubocop/cop/factory_bot/factory_name_style.rb b/lib/rubocop/cop/factory_bot/factory_name_style.rb index 001f4958..2184c80d 100644 --- a/lib/rubocop/cop/factory_bot/factory_name_style.rb +++ b/lib/rubocop/cop/factory_bot/factory_name_style.rb @@ -14,6 +14,9 @@ module FactoryBot # create(:user) # build :user, username: "NAME" # + # # good - namespaced models + # create('users/internal') + # # @example EnforcedStyle: string # # bad # create(:user) @@ -76,13 +79,17 @@ def on_send(node) private def offense_for_symbol_style?(name) - name.str_type? && style == :symbol + name.str_type? && style == :symbol && !namespaced?(name) end def offense_for_string_style?(name) name.sym_type? && style == :string end + def namespaced?(name) + name.value.include?('/') + end + def register_offense(name, prefer) add_offense(name, message: format(MSG, prefer: style.to_s)) do |corrector| diff --git a/spec/rubocop/cop/factory_bot/factory_name_style_spec.rb b/spec/rubocop/cop/factory_bot/factory_name_style_spec.rb index 3007c09a..600002f6 100644 --- a/spec/rubocop/cop/factory_bot/factory_name_style_spec.rb +++ b/spec/rubocop/cop/factory_bot/factory_name_style_spec.rb @@ -97,6 +97,13 @@ build user: :foo RUBY end + + it 'does not register an offense when using `create` ' \ + 'with a method call when string include /' do + expect_no_offenses(<<~RUBY) + create("users/internal") + RUBY + end end context 'when EnforcedStyle is :string' do