121 lines
4.6 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "rubocops/shared/helper_functions"
module RuboCop
module Cop
# This module performs common checks the `desc` field in both formulae and casks.
module DescHelper
include HelperFunctions
MAX_DESC_LENGTH = 80
VALID_LOWERCASE_WORDS = %w[
iOS
iPhone
macOS
].freeze
sig { params(type: Symbol, name: T.nilable(String), desc_call: T.nilable(RuboCop::AST::Node)).void }
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
desc = T.cast(desc_call, RuboCop::AST::SendNode).first_argument
# Check if the desc is empty.
desc_length = string_content(desc).length
if desc_length.zero?
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+/)
# 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+$/)
# Check if "command-line" is spelled incorrectly in the desc.
if (match = regex_match_group(desc, /(command ?line)/i))
c = match.to_s[0]
2021-01-12 18:12:56 +11:00
desc_problem "Description should use \"#{c}ommand-line\" instead of \"#{match}\"."
end
# 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)
# Check if invalid lowercase words are at the start of a desc.
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."
end
# Check if the desc starts with the formula's or cask's name.
name_regex = T.must(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-09-01 14:05:52 +01:00
if type == :cask &&
(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"
add_offense(@offensive_source_range, message: "Description shouldn't contain the platform.")
end
# 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."
end
2023-06-20 11:36:47 -04:00
# Check if the desc contains Unicode emojis or symbols (Unicode Other Symbols category).
desc_problem "Description shouldn't contain Unicode emojis or symbols." if regex_match_group(desc, /\p{So}/)
2023-06-20 08:10:30 -04:00
# Check if the desc length exceeds maximum length.
return if desc_length <= MAX_DESC_LENGTH
problem "Description is too long. It should be less than #{MAX_DESC_LENGTH} characters. " \
"The current length is #{desc_length}."
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)
2024-03-07 16:20:20 +00:00
add_offense(@offensive_source_range, 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]
next if correction.nil?
correction.gsub!(/^\s+/, "")
correction.gsub!(/\s+$/, "")
correction.sub!(/^(the|an?)\s+/i, "")
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")
2023-06-20 11:36:47 -04:00
correction.gsub!(/\s?\p{So}/, "")
correction.gsub!(/^\s+/, "")
correction.gsub!(/\s+$/, "")
correction.gsub!(/\.$/, "")
next if correction == match_data[:correction]
2021-01-12 18:12:56 +11:00
corrector.replace(@offensive_node.source_range, "#{quote}#{correction}#{quote}")
end
end
end
end
end