summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-01-21 00:08:59 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2023-01-21 00:08:59 +0000
commit367847e266036617e540e41b7fd3c7d03033800c (patch)
treea14547ad7556d48dcdeb977021f8cd89305ea026 /rubocop
parent5d7e5a8902382caaffa616e1b496b684ba72d148 (diff)
downloadgitlab-ce-367847e266036617e540e41b7fd3c7d03033800c.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/gitlab/doc_url.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/rubocop/cop/gitlab/doc_url.rb b/rubocop/cop/gitlab/doc_url.rb
new file mode 100644
index 00000000000..cbfbdf7eb57
--- /dev/null
+++ b/rubocop/cop/gitlab/doc_url.rb
@@ -0,0 +1,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