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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-12-20 17:22:11 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2022-12-20 17:22:11 +0300
commit0c872e02b2c822e3397515ec324051ff540f0cd5 (patch)
treece2fb6ce7030e4dad0f4118d21ab6453e5938cdd /rubocop/cop/graphql/descriptions.rb
parentf7e05a6853b12f02911494c4b3fe53d9540d74fc (diff)
Add latest changes from gitlab-org/gitlab@15-7-stable-eev15.7.0-rc42
Diffstat (limited to 'rubocop/cop/graphql/descriptions.rb')
-rw-r--r--rubocop/cop/graphql/descriptions.rb25
1 files changed, 23 insertions, 2 deletions
diff --git a/rubocop/cop/graphql/descriptions.rb b/rubocop/cop/graphql/descriptions.rb
index 3c945507699..239f5b966a4 100644
--- a/rubocop/cop/graphql/descriptions.rb
+++ b/rubocop/cop/graphql/descriptions.rb
@@ -3,6 +3,11 @@
# This cop checks for missing GraphQL descriptions and enforces the description style guide:
# https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#description-style-guide
#
+# @safety
+# This cop is unsafe because not all cases of "this" can be substituted with
+# "the". This will require a technical writer to assist with the alternative,
+# proper grammar that can be used for that particular GraphQL descriptions.
+#
# @examples
#
# # bad
@@ -49,6 +54,8 @@ module RuboCop
MSG_NO_DESCRIPTION = "Please add a `description` property. #{MSG_STYLE_GUIDE_LINK}"
MSG_NO_PERIOD = "`description` strings must end with a `.`. #{MSG_STYLE_GUIDE_LINK}"
MSG_BAD_START = "`description` strings should not start with \"A...\" or \"The...\". #{MSG_STYLE_GUIDE_LINK}"
+ MSG_CONTAINS_THIS = "`description` strings should not contain the demonstrative \"this\"."\
+ " #{MSG_STYLE_GUIDE_LINK}"
def_node_matcher :graphql_describable?, <<~PATTERN
(send nil? {:field :argument :value} ...)
@@ -82,15 +89,17 @@ module RuboCop
MSG_NO_PERIOD
elsif bad_start?(description)
MSG_BAD_START
+ elsif contains_demonstrative_this?(description)
+ MSG_CONTAINS_THIS
end
return unless message
add_offense(node, message: message) do |corrector|
- description = locate_description(node)
next unless description
- corrector.insert_after(before_end_quote(description), '.')
+ corrector.insert_after(before_end_quote(description), '.') if no_period?(description)
+ corrector.replace(locate_this(description), 'the') if contains_demonstrative_this?(description)
end
end
@@ -114,6 +123,10 @@ module RuboCop
string?(description) && description.value.strip.downcase.start_with?('a ', 'the ')
end
+ def contains_demonstrative_this?(description)
+ string?(description) && /\bthis\b/.match?(description.value.strip)
+ end
+
# Returns true if `description` node is a `:str` (as opposed to a `#copy_field_description` call)
def string?(description)
description.type == :str
@@ -127,6 +140,14 @@ module RuboCop
adjust = heredoc_source.index(/\s+\Z/) - heredoc_source.length
string.location.heredoc_body.adjust(end_pos: adjust)
end
+
+ # Returns a `Parser::Source::Range` of the first `this` encountered
+ def locate_this(string)
+ target = 'this'
+ range = string.heredoc? ? string.location.heredoc_body : string.location.expression
+ index = range.source.index(target)
+ range.adjust(begin_pos: index, end_pos: (index + target.length) - range.length)
+ end
end
end
end