2023-02-14 19:19:37 -08:00
|
|
|
# typed: true
|
2020-07-30 16:29:12 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rubocops/shared/helper_functions"
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
2020-11-05 17:17:03 -05:00
|
|
|
# This module performs common checks the `desc` field in both formulae and casks.
|
2020-08-26 02:52:48 +02:00
|
|
|
#
|
|
|
|
# @api private
|
2020-07-30 16:29:12 +01:00
|
|
|
module DescHelper
|
|
|
|
include HelperFunctions
|
|
|
|
|
2020-08-15 00:16:37 +02:00
|
|
|
MAX_DESC_LENGTH = 80
|
|
|
|
|
2020-07-30 16:29:12 +01:00
|
|
|
VALID_LOWERCASE_WORDS = %w[
|
2020-08-13 18:18:03 +02:00
|
|
|
iOS
|
|
|
|
iPhone
|
2020-07-30 16:29:12 +01:00
|
|
|
macOS
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
def audit_desc(type, name, desc_call)
|
|
|
|
# Check if a desc is present.
|
|
|
|
if desc_call.nil?
|
|
|
|
problem "#{type.to_s.capitalize} should have a desc (Description)."
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-08-12 01:02:01 +02:00
|
|
|
@offensive_node = desc_call
|
|
|
|
|
2020-07-30 16:29:12 +01:00
|
|
|
desc = desc_call.first_argument
|
|
|
|
|
|
|
|
# Check if the desc is empty.
|
2020-08-15 00:16:37 +02:00
|
|
|
desc_length = string_content(desc).length
|
|
|
|
if desc_length.zero?
|
2020-07-30 16:29:12 +01:00
|
|
|
problem "The desc (description) should not be an empty string."
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check the desc for leading whitespace.
|
2021-01-12 18:12:56 +11:00
|
|
|
desc_problem "Description shouldn't have leading spaces." if regex_match_group(desc, /^\s+/)
|
2020-07-30 16:29:12 +01:00
|
|
|
|
|
|
|
# Check the desc for trailing whitespace.
|
2021-01-12 18:12:56 +11:00
|
|
|
desc_problem "Description shouldn't have trailing spaces." if regex_match_group(desc, /\s+$/)
|
2020-07-30 16:29:12 +01:00
|
|
|
|
|
|
|
# Check if "command-line" is spelled incorrectly in the desc.
|
2021-02-12 18:33:37 +05:30
|
|
|
if (match = regex_match_group(desc, /(command ?line)/i))
|
2020-07-30 16:29:12 +01:00
|
|
|
c = match.to_s[0]
|
2021-01-12 18:12:56 +11:00
|
|
|
desc_problem "Description should use \"#{c}ommand-line\" instead of \"#{match}\"."
|
2020-07-30 16:29:12 +01:00
|
|
|
end
|
|
|
|
|
2020-09-17 20:44:09 +02:00
|
|
|
# Check if the desc starts with an article.
|
2021-01-12 18:12:56 +11:00
|
|
|
desc_problem "Description shouldn't start with an article." if regex_match_group(desc, /^(the|an?)(?=\s)/i)
|
2020-07-30 16:29:12 +01:00
|
|
|
|
|
|
|
# Check if invalid lowercase words are at the start of a desc.
|
2023-04-07 21:35:38 +01:00
|
|
|
if !VALID_LOWERCASE_WORDS.include?(string_content(desc).split.first) && regex_match_group(desc, /^[a-z]/)
|
2021-01-12 18:12:56 +11:00
|
|
|
desc_problem "Description should start with a capital letter."
|
2020-07-30 16:29:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check if the desc starts with the formula's or cask's name.
|
2021-03-26 14:11:03 +00:00
|
|
|
name_regex = name.delete("-").chars.join('[\s\-]?')
|
2021-01-12 18:12:56 +11:00
|
|
|
if regex_match_group(desc, /^#{name_regex}\b/i)
|
|
|
|
desc_problem "Description shouldn't start with the #{type} name."
|
|
|
|
end
|
2020-07-30 16:29:12 +01:00
|
|
|
|
2020-09-01 14:05:52 +01:00
|
|
|
if type == :cask &&
|
2020-09-19 04:30:41 +02:00
|
|
|
(match = regex_match_group(desc, /\b(macOS|Mac( ?OS( ?X)?)?|OS ?X)(?! virtual machines?)\b/i)) &&
|
2020-09-01 14:05:52 +01:00
|
|
|
match[1] != "MAC"
|
2021-01-12 18:12:56 +11:00
|
|
|
desc_problem "Description shouldn't contain the platform."
|
2020-09-01 13:18:59 +02:00
|
|
|
end
|
|
|
|
|
2020-07-30 16:29:12 +01:00
|
|
|
# Check if a full stop is used at the end of a desc (apart from in the case of "etc.").
|
|
|
|
if regex_match_group(desc, /\.$/) && !string_content(desc).end_with?("etc.")
|
2021-01-12 18:12:56 +11:00
|
|
|
desc_problem "Description shouldn't end with a full stop."
|
2020-07-30 16:29:12 +01:00
|
|
|
end
|
|
|
|
|
2020-08-15 00:16:37 +02:00
|
|
|
# Check if the desc length exceeds maximum length.
|
|
|
|
return if desc_length <= MAX_DESC_LENGTH
|
2020-07-30 16:29:12 +01:00
|
|
|
|
2020-08-15 00:16:37 +02:00
|
|
|
problem "Description is too long. It should be less than #{MAX_DESC_LENGTH} characters. " \
|
|
|
|
"The current length is #{desc_length}."
|
2020-07-30 16:29:12 +01:00
|
|
|
end
|
|
|
|
|
2021-01-12 18:12:56 +11:00
|
|
|
# Auto correct desc problems. `regex_match_group` must be called before this to populate @offense_source_range.
|
|
|
|
def desc_problem(message)
|
|
|
|
add_offense(@offensive_source_range, message: message) do |corrector|
|
2023-02-14 19:19:37 -08:00
|
|
|
match_data = @offensive_node.source.match(/\A(?<quote>["'])(?<correction>.*)(?:\k<quote>)\Z/)
|
|
|
|
correction = match_data[:correction]
|
|
|
|
quote = match_data[:quote]
|
2020-07-30 16:29:12 +01:00
|
|
|
|
|
|
|
next if correction.nil?
|
|
|
|
|
|
|
|
correction.gsub!(/^\s+/, "")
|
|
|
|
correction.gsub!(/\s+$/, "")
|
|
|
|
|
2020-09-17 20:44:09 +02:00
|
|
|
correction.sub!(/^(the|an?)\s+/i, "")
|
2020-07-30 16:29:12 +01:00
|
|
|
|
|
|
|
first_word = correction.split.first
|
|
|
|
unless VALID_LOWERCASE_WORDS.include?(first_word)
|
|
|
|
first_char = first_word.to_s[0]
|
|
|
|
correction[0] = first_char.upcase if first_char
|
|
|
|
end
|
|
|
|
|
|
|
|
correction.gsub!(/(ommand ?line)/i, "ommand-line")
|
2021-01-12 18:12:56 +11:00
|
|
|
correction.gsub!(/(^|[^a-z])#{@name}([^a-z]|$)/i, "\\1\\2")
|
2020-07-30 16:29:12 +01:00
|
|
|
correction.gsub!(/^\s+/, "")
|
|
|
|
correction.gsub!(/\s+$/, "")
|
|
|
|
correction.gsub!(/\.$/, "")
|
|
|
|
|
2021-01-12 18:12:56 +11:00
|
|
|
corrector.replace(@offensive_node.source_range, "#{quote}#{correction}#{quote}")
|
2020-07-30 16:29:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|