Sha256: 568cce9245cac52c3467727cdb716988f7c40566200f873f2be071f6d1ee7037

Contents?: true

Size: 1.07 KB

Versions: 210

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require "rubocop"

# :nocov:
module RuboCop
  module Cop
    module Primer
      # This cop ensures that tags are not set with ||=
      #
      # bad
      # @system_arguments[:tag] ||= :h1
      #
      # good
      # @system_arguments[:tag] = fetch_or_fallback(TAG_OPTIONS, tag, DEFAULT_TAG)
      #
      # good
      # @system_arguments[:tag] = :h2
      class NoTagMemoize < RuboCop::Cop::Cop
        INVALID_MESSAGE = <<~STR
          Avoid `[:tag] ||=`. Instead, try one of the following:
            - Don't allow consumers to update the tag by having a fixed tag (e.g. `system_arguments[:tag] = :div`)
            - Use the `fetch_or_fallback` helper to only allow a tag from a restricted list.
        STR

        def_node_search :tag_memoized?, <<~PATTERN
          (or-asgn
            (send
              _
              _
              (sym :tag)
            )
            _
          )
        PATTERN

        def on_or_asgn(node)
          add_offense(node, message: INVALID_MESSAGE) if tag_memoized?(node)
        end
      end
    end
  end
end

Version data entries

210 entries across 210 versions & 2 rubygems

Version Path
primer_view_components-0.0.101 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.100 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.99 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.98 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.97 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.96 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.95 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.94 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.93 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.92 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.91 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.90 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.89 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.88 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.87 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.86 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.85 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.84 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.83 lib/rubocop/cop/primer/no_tag_memoize.rb
primer_view_components-0.0.82 lib/rubocop/cop/primer/no_tag_memoize.rb