Sha256: 0c76359599cb86d7a0c8db3cd3e55c10e6592b970339c0091aac63d3529ac229

Contents?: true

Size: 1.05 KB

Versions: 364

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require "rubocop"

# :nocov:
module RuboCop
  module Cop
    module Ariadne
      # This cop ensures that tags are not set with ||=
      #
      # bad
      # @attributes[:tag] ||= :h1
      #
      # good
      # @attributes[:tag] = fetch_or_raise(TAG_OPTIONS, tag)
      #
      # good
      # @attributes[:tag] = :h2
      # TODO: TEST THIS
      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. `attributes[:tag] = :div`)
            - Use the `fetch_or_raise` 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

364 entries across 364 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.92 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.91 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.90 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.89.1 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.89 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.88 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.87 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86.9 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86.8 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86.7 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86.6 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86.5 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86.4 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86.3 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86.2 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86.1 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.86 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.85 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.84 lib/rubocop/cop/ariadne/no_tag_memoize.rb
ariadne_view_components-0.0.83.3 lib/rubocop/cop/ariadne/no_tag_memoize.rb