Sha256: 3aad1595443fe0c42f2ae6c0a5bb8ebb8d14244fd47127153d9e571fc9db0fef

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Discourse
      # When fabricating a record without custom attributes, we can use the
      # fabricator shorthand as long as the identifier matches the fabricator
      # name.
      #
      # @example
      #
      # # bad
      # fab!(:user) { Fabricate(:user) }
      #
      # # good
      # fab!(:user)
      #
      # When using custom attributes or the identifier doesn't match, the
      # shorthand can't be used.
      #
      # @example
      #
      # # good
      # fab!(:user) { Fabricate(:user, trust_level: TrustLevel[0]) }
      #
      # # good
      # fab!(:another_user) { Fabricate(:user) }
      class FabricatorShorthand < Base
        def_node_matcher :offending_fabricator?, <<-MATCHER
          (block
            (send nil? :fab!
              (sym $_identifier))
            (args)
            (send nil? :Fabricate
              (sym $_identifier)))
        MATCHER

        def on_block(node)
          offending_fabricator?(node) do |identifier|
            add_offense(node, message: message(identifier))
          end
        end

        private

        def message(identifier)
          "Use the fabricator shorthand: `fab!(:#{identifier})`"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-discourse-3.7.0 lib/rubocop/cop/discourse/fabricator_shorthand.rb