Class: RuboCop::Cop::Momocop::FactoryBotSingleDefinePerFile

Inherits:
Base
  • Object
show all
Includes:
Momocop::Helpers::FactoryBotHelper
Defined in:
lib/rubocop/cop/momocop/factory_bot_single_define_per_file.rb

Overview

This cop checks for multiple ‘FactoryBot.define` blocks in a single file. It’s recommended to have only one ‘FactoryBot.define` block per file to keep factory definitions clear and maintainable.

Examples:

# bad
FactoryBot.define do
  factory :user do
  end
end

FactoryBot.define do
  factory :admin do
  end
end

# good
FactoryBot.define do
  factory :user do
  end
  factory :admin do
  end
end

Constant Summary collapse

MSG =
'Only one `FactoryBot.define` block is allowed per file.'

Constants included from Momocop::Helpers::FactoryBotHelper

Momocop::Helpers::FactoryBotHelper::RUBOCOP_HELPER_METHODS

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/momocop/factory_bot_single_define_per_file.rb', line 34

def on_new_investigation
  factory_bot_define_blocks =
    processed_source
    .ast
    .descendants
    .select { |node|
      factory_bot_define?(node)
    }

  return if factory_bot_define_blocks.size <= 1

  factory_bot_define_blocks[1..].each do |factory_bot_define_block|
    add_offense(factory_bot_define_block, message: MSG)
  end
end