Welcome to mirror list, hosted at ThFree Co, Russian Federation.

doc_url.rb « gitlab « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbfbdf7eb57bac1904f60a78ecd9e1635a1dc1d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

module RuboCop
  module Cop
    module Gitlab
      # This cop encourages using helper to link to documentation
      # in string literals.
      #
      # @example
      #   # bad
      #   'See [the docs](https://docs.gitlab.com/ee/user/permissions#roles).'
      #   _('See [the docs](https://docs.gitlab.com/ee/user/permissions#roles).')
      #
      #   # good
      #   docs_link = link_to _('the docs'), help_page_url('user/permissions', anchor: 'roles')
      #   "See #{docs_link}."
      #   _('See %{docs_link}.').html_safe % { docs_link: docs_link.html_safe }
      class DocUrl < RuboCop::Cop::Base
        include RangeHelp

        MSG = 'Use `#help_page_url` instead of directly including link. ' \
              'See https://docs.gitlab.com/ee/development/documentation/#linking-to-help-in-ruby.'

        DOCS_URL_REGEXP = %r{https://docs.gitlab.com/ee/[\w#%./-]+}.freeze

        def on_str(node)
          match = DOCS_URL_REGEXP.match(node.source)
          return unless match

          add_offense(bad_range(node, match))
        end

        private

        def bad_range(node, match)
          url_begin_pos, url_end_pos = match.offset(0)
          begin_pos = node.loc.expression.begin_pos + url_begin_pos

          range_between(begin_pos, begin_pos + (url_end_pos - url_begin_pos))
        end
      end
    end
  end
end