Refactor brew typecheck.

This commit is contained in:
Markus Reiter 2020-11-25 12:34:35 +01:00
parent c5a9de8ff5
commit 0032cfca5b
4 changed files with 42 additions and 3 deletions

View File

@ -22,6 +22,9 @@ module Homebrew
description: "Silence all non-critical errors." description: "Silence all non-critical errors."
switch "--update", switch "--update",
description: "Update RBI files." description: "Update RBI files."
switch "--suggest-typed",
description: "Try upgrading `typed` sigils.",
depends_on: "--update"
switch "--fail-if-not-changed", switch "--fail-if-not-changed",
description: "Return a failing status code if all gems are up to date " \ description: "Return a failing status code if all gems are up to date " \
"and gem definitions do not need a tapioca update." "and gem definitions do not need a tapioca update."
@ -38,6 +41,7 @@ module Homebrew
end end
end end
sig { void }
def typecheck def typecheck
args = typecheck_args.parse args = typecheck_args.parse
@ -50,12 +54,44 @@ module Homebrew
system "bundle", "exec", "srb", "rbi", "hidden-definitions" system "bundle", "exec", "srb", "rbi", "hidden-definitions"
system "bundle", "exec", "srb", "rbi", "todo" system "bundle", "exec", "srb", "rbi", "todo"
if args.suggest_typed?
result = system_command(
"bundle",
args: ["exec", "--", "srb", "tc", "--suggest-typed", "--typed=strict", "--error-white-list=7022"],
print_stderr: false,
)
allowed_changes = {
"false" => ["true", "strict"],
"true" => ["strict"],
}
# Workaround for `srb tc rbi suggest-typed`, which currently fails get to a converging state.
result.stderr.scan(/^(.*\.rb):\d+:\s+You could add `#\s*typed:\s*(.*?)`/).each do |path, new_level|
path = Pathname(path)
next unless path.file?
contents = path.read
next unless (match = contents.match(/\A\s*#\s*typed:\s*([^\s]+)/))
existing_level = match[1]
next unless allowed_changes.fetch(existing_level, []).include?(new_level)
puts "#{path}: #{existing_level} -> #{new_level}"
path.atomic_write contents.sub(/\A(\s*#\s*typed:\s*)(?:[^\s]+)/, "\\1#{new_level}")
end
end
Homebrew.failed = system("git", "diff", "--stat", "--exit-code") if args.fail_if_not_changed? Homebrew.failed = system("git", "diff", "--stat", "--exit-code") if args.fail_if_not_changed?
return return
end end
srb_exec = %w[bundle exec srb tc] srb_exec = %w[bundle exec srb tc]
srb_exec << "--error-black-list" << "5061"
srb_exec << "--quiet" if args.quiet? srb_exec << "--quiet" if args.quiet?
srb_exec << "--autocorrect" if args.fix? srb_exec << "--autocorrect" if args.fix?
srb_exec += ["--ignore", args.ignore] if args.ignore.present? srb_exec += ["--ignore", args.ignore] if args.ignore.present?

View File

@ -9,6 +9,3 @@
--dsl-plugins --dsl-plugins
sorbet/triggers.yml sorbet/triggers.yml
--error-black-list
5061

View File

@ -1269,6 +1269,8 @@ Check for typechecking errors using Sorbet.
Silence all non-critical errors. Silence all non-critical errors.
* `--update`: * `--update`:
Update RBI files. Update RBI files.
* `--suggest-typed`:
Try upgrading `typed` sigils.
* `--fail-if-not-changed`: * `--fail-if-not-changed`:
Return a failing status code if all gems are up to date and gem definitions do not need a tapioca update. Return a failing status code if all gems are up to date and gem definitions do not need a tapioca update.
* `--dir`: * `--dir`:

View File

@ -1753,6 +1753,10 @@ Silence all non\-critical errors\.
Update RBI files\. Update RBI files\.
. .
.TP .TP
\fB\-\-suggest\-typed\fR
Try upgrading \fBtyped\fR sigils\.
.
.TP
\fB\-\-fail\-if\-not\-changed\fR \fB\-\-fail\-if\-not\-changed\fR
Return a failing status code if all gems are up to date and gem definitions do not need a tapioca update\. Return a failing status code if all gems are up to date and gem definitions do not need a tapioca update\.
. .