Sha256: a397cab18af1058050f3210714ec044433ce4816307b3d8890045555ab40c750

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Rubocop
  module Cop
    module Rails
      #  Avoid including `ActionView::Helpers::UrlHelper`.
      #  It adds/overrides ~40 methods while usually only one is needed.
      #  Instead, use the `Gitlab::Routing.url_helpers`/`Application.routes.url_helpers`(outside of gitlab)
      #  and `ActionController::Base.helpers.link_to`.
      #
      # @example
      #   # bad
      #   class Foo
      #     include ActionView::Helpers::UrlHelper  # <-- includes 40 new methods !
      #
      #     def link_to_something
      #       link_to(...)
      #     end
      #   end
      #
      #   # good
      #   class Foo
      #     def link_to_something
      #       url = Gitlab::Routing.url_helpers.project_blob_path(...)
      #       ActionController::Base.helpers.link_to(url, "Link text")
      #     end
      #   end
      #
      # See also
      # - https://gitlab.com/gitlab-org/gitlab/-/issues/340567.
      class IncludeUrlHelper < RuboCop::Cop::Base
        MSG = <<~MSG
          Avoid including `ActionView::Helpers::UrlHelper`.
          It adds/overrides ~40 methods while usually only one is needed.
          Instead, use the `Gitlab::Routing.url_helpers`/`Application.routes.url_helpers`(outside of gitlab)
          and `ActionController::Base.helpers.link_to`.
          See https://gitlab.com/gitlab-org/gitlab/-/issues/340567.
        MSG

        # @!method include_url_helpers_node?(node)
        def_node_matcher :include_url_helpers_node?, <<~PATTERN
          (send nil? :include (const (const (const {nil? cbase} :ActionView) :Helpers) :UrlHelper))
        PATTERN

        def on_send(node)
          add_offense(node) if include_url_helpers_node?(node)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gitlab-styles-13.0.1 lib/rubocop/cop/rails/include_url_helper.rb
gitlab-styles-13.0.0 lib/rubocop/cop/rails/include_url_helper.rb