Sha256: 11a8ddec63155ad9a2a2f4f85f7aa16e87dfeadcdc77c165d1c5afb2654de50b

Contents?: true

Size: 1.53 KB

Versions: 12

Compression:

Stored size: 1.53 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
        extend AutoCorrector
        include IgnoredNode

        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 |expression, _identifier|
            add_offense(
              node,
              message: message(expression.source)
            ) do |corrector|
              next if part_of_ignored_node?(node)
              corrector.replace(node, expression.source)
            end
            ignore_node(node)
          end
        end

        private

        def message(expression)
          "Use the fabricator shorthand: `#{expression}`"
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-discourse-3.10.0 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.9.3 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.9.2 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.9.1 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.9.0 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.8.6 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.8.5 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.8.4 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.8.3 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.8.2 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.8.1 lib/rubocop/cop/discourse/fabricator_shorthand.rb
rubocop-discourse-3.7.1 lib/rubocop/cop/discourse/fabricator_shorthand.rb