Class: Homebrew::DevCmd::Typecheck Private

Inherits:
AbstractCommand show all
Includes:
FileUtils
Defined in:
dev-cmd/typecheck.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/typecheck.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::DevCmd::Typecheck::Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
# File 'sorbet/rbi/dsl/homebrew/dev_cmd/typecheck.rbi', line 10

def args; end

#runvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'dev-cmd/typecheck.rb', line 46

def run
  if (args.dir.present? || args.file.present?) && args.named.present?
    raise UsageError, "Cannot use `--dir` or `--file` when specifying a tap."
  elsif args.fix? && args.named.present?
    raise UsageError, "Cannot use `--fix` when specifying a tap."
  end

  update = args.update? || args.update_all?
  groups = update ? Homebrew.valid_gem_groups : ["typecheck"]
  Homebrew.install_bundler_gems!(groups:)

  # Sorbet doesn't use bash privileged mode so we align EUID and UID here.
  Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid

  HOMEBREW_LIBRARY_PATH.cd do
    if update
      workers = args.debug? ? ["--workers=1"] : []
      safe_system "bundle", "exec", "tapioca", "annotations"
      safe_system "bundle", "exec", "tapioca", "dsl", *workers
      # Prefer adding args here: Library/Homebrew/sorbet/tapioca/config.yml
      tapioca_args = args.update_all? ? ["--all"] : []

      ohai "Updating Tapioca RBI files..."
      safe_system "bundle", "exec", "tapioca", "gem", *tapioca_args

      ohai "Trimming RuboCop RBI because by default it's massive..."
      trim_rubocop_rbi

      if args.suggest_typed?
        ohai "Checking if we can bump Sorbet `typed` sigils..."
        # --sorbet needed because of https://github.com/Shopify/spoom/issues/488
        system "bundle", "exec", "spoom", "srb", "bump", "--from", "false", "--to", "true",
               "--sorbet", "#{Gem.bin_path("sorbet", "srb")} tc"
        system "bundle", "exec", "spoom", "srb", "bump", "--from", "true", "--to", "strict",
               "--sorbet", "#{Gem.bin_path("sorbet", "srb")} tc"
      end

      return
    end

    srb_exec = %w[bundle exec srb tc]

    srb_exec << "--quiet" if args.quiet?

    if args.fix?
      # Auto-correcting method names is almost always wrong.
      srb_exec << "--suppress-error-code" << "7003"

      srb_exec << "--autocorrect"
    end

    if args.lsp?
      srb_exec << "--lsp"
      if (watchman = which("watchman", ORIGINAL_PATHS))
        srb_exec << "--watchman-path" << watchman
      else
        srb_exec << "--disable-watchman"
      end
    end

    srb_exec += ["--ignore", args.ignore] if args.ignore.present?
    if args.file.present? || args.dir.present? || (tap_dir = args.named.to_paths(only: :tap).first).present?
      cd("sorbet") do
        srb_exec += ["--file", "../#{args.file}"] if args.file
        srb_exec += ["--dir", "../#{args.dir}"] if args.dir
        srb_exec += ["--dir", tap_dir.to_s] if tap_dir
      end
    end
    success = system(*srb_exec)
    return if success

    $stderr.puts "Check #{Formatter.url("https://docs.brew.sh/Typechecking")} for " \
                 "more information on how to resolve these errors."
    Homebrew.failed = true
  end
end

#trim_rubocop_rbi(path: HOMEBREW_LIBRARY_PATH/"sorbet/rbi/gems/rubocop@*.rbi") ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • path (String, Pathname) (defaults to: HOMEBREW_LIBRARY_PATH/"sorbet/rbi/gems/rubocop@*.rbi")


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'dev-cmd/typecheck.rb', line 124

def trim_rubocop_rbi(path: HOMEBREW_LIBRARY_PATH/"sorbet/rbi/gems/rubocop@*.rbi")
  rbi_file = Dir.glob(path).first
  return unless rbi_file.present?
  return unless (rbi_path = Pathname.new(rbi_file)).exist?

  require "prism"
  original_content = rbi_path.read
  parsed = Prism.parse(original_content)
  return unless parsed.success?

  allowlist = %w[
    Parser::Source
    RuboCop::AST::Node
    RuboCop::AST::NodePattern
    RuboCop::AST::ProcessedSource
    RuboCop::CLI
    RuboCop::Config
    RuboCop::Cop::AllowedPattern
    RuboCop::Cop::AllowedMethods
    RuboCop::Cop::AutoCorrector
    RuboCop::Cop::AutocorrectLogic
    RuboCop::Cop::Base
    RuboCop::Cop::CommentsHelp
    RuboCop::Cop::ConfigurableFormatting
    RuboCop::Cop::ConfigurableNaming
    RuboCop::Cop::Corrector
    RuboCop::Cop::IgnoredMethods
    RuboCop::Cop::IgnoredNode
    RuboCop::Cop::IgnoredPattern
    RuboCop::Cop::MethodPreference
    RuboCop::Cop::Offense
    RuboCop::Cop::RangeHelp
    RuboCop::Cop::Registry
    RuboCop::Cop::Util
    RuboCop::DirectiveComment
    RuboCop::Error
    RuboCop::ExcludeLimit
    RuboCop::Ext::Comment
    RuboCop::Ext::ProcessedSource
    RuboCop::Ext::Range
    RuboCop::FileFinder
    RuboCop::Formatter::TextUtil
    RuboCop::Formatter::PathUtil
    RuboCop::Options
    RuboCop::ResultCache
    RuboCop::Runner
    RuboCop::TargetFinder
    RuboCop::Version
  ].freeze

  nodes_to_keep = Set.new

  parsed.value.statements.body.each do |node|
    case node
    when Prism::ModuleNode, Prism::ClassNode
      # Keep if it's in our allowlist or is a top-level essential node.
      full_name = extract_full_name(node)
      nodes_to_keep << node if full_name.blank? || allowlist.any? { |name| full_name.start_with?(name) }
    when Prism::ConstantWriteNode # Keep essential constants.
      nodes_to_keep << node if node.name.to_s.match?(/^[[:digit:][:upper:]_]+$/)
    else # Keep other top-level nodes (comments, etc.)
      nodes_to_keep << node
    end
  end

  new_content = generate_trimmed_rbi(original_content, nodes_to_keep, parsed)
  rbi_path.write(new_content)
end