brew/Library/Homebrew/rubocops/shared/homepage_helper.rb
Issy Long 45978435e7
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 15:24:27 +01:00

96 lines
4.5 KiB
Ruby

# 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 `homepage` field in both formulae and casks.
module HomepageHelper
include HelperFunctions
def audit_homepage(type, content, homepage_node, homepage_parameter_node)
@offensive_node = homepage_node
problem "#{type.to_s.capitalize} should have a homepage." if content.empty?
@offensive_node = homepage_parameter_node
problem "The homepage should start with http or https." unless content.match?(%r{^https?://})
case content
# Freedesktop is complicated to handle - It has SSL/TLS, but only on certain subdomains.
# To enable https Freedesktop change the URL from http://project.freedesktop.org/wiki to
# https://wiki.freedesktop.org/project_name.
# "Software" is redirected to https://wiki.freedesktop.org/www/Software/project_name
when %r{^http://((?:www|nice|libopenraw|liboil|telepathy|xorg)\.)?freedesktop\.org/(?:wiki/)?}
if content.include?("Software")
problem "Freedesktop homepages should be styled " \
"`https://wiki.freedesktop.org/www/Software/project_name`"
else
problem "Freedesktop homepages should be styled `https://wiki.freedesktop.org/project_name`"
end
# Google Code homepages should end in a slash
when %r{^https?://code\.google\.com/p/[^/]+[^/]$}
problem "Google Code homepages should end with a slash" do |corrector|
corrector.replace(homepage_parameter_node.source_range, "\"#{content}/\"")
end
when %r{^http://([^/]*)\.(sf|sourceforge)\.net(/|$)}
fixed = "https://#{Regexp.last_match(1)}.sourceforge.io/"
problem "Sourceforge homepages should be `#{fixed}`" do |corrector|
corrector.replace(homepage_parameter_node.source_range, "\"#{fixed}\"")
end
when /readthedocs\.org/
fixed = content.sub("readthedocs.org", "readthedocs.io")
problem "Readthedocs homepages should be `#{fixed}`" do |corrector|
corrector.replace(homepage_parameter_node.source_range, "\"#{fixed}\"")
end
when %r{^https://github.com.*\.git$}
problem "GitHub homepages should not end with .git" do |corrector|
corrector.replace(homepage_parameter_node.source_range, "\"#{content.delete_suffix(".git")}\"")
end
# People will run into mixed content sometimes, but we should enforce and then add
# exemptions as they are discovered. Treat mixed content on homepages as a bug.
# Justify each exemptions with a code comment so we can keep track here.
#
# Compact the above into this list as we're able to remove detailed notations, etc over time.
when
# Check for `http://` GitHub homepage URLs, `https://` is preferred.
# NOTE: Only check homepages that are repository pages, not `*.github.com` hosts.
%r{^http://github\.com/},
%r{^http://[^/]*\.github\.io/},
# Savannah has full SSL/TLS support but no auto-redirect.
# Doesn't apply to the download URLs, only the homepage.
%r{^http://savannah\.nongnu\.org/},
%r{^http://[^/]*\.sourceforge\.io/},
# There's an auto-redirect here, but this mistake is incredibly common too.
# Only applies to the homepage and subdomains for now, not the FTP URLs.
%r{^http://((?:build|cloud|developer|download|extensions|git|
glade|help|library|live|nagios|news|people|
projects|rt|static|wiki|www)\.)?gnome\.org}x,
%r{^http://[^/]*\.apache\.org},
%r{^http://packages\.debian\.org},
%r{^http://wiki\.freedesktop\.org/},
%r{^http://((?:www)\.)?gnupg\.org/},
%r{^http://ietf\.org},
%r{^http://[^/.]+\.ietf\.org},
%r{^http://[^/.]+\.tools\.ietf\.org},
%r{^http://www\.gnu\.org/},
%r{^http://code\.google\.com/},
%r{^http://bitbucket\.org/},
%r{^http://(?:[^/]*\.)?archive\.org}
problem "Please use https:// for #{content}" do |corrector|
corrector.replace(homepage_parameter_node.source_range, "\"#{content.sub("http", "https")}\"")
end
end
end
end
end
end