language/node: enable typed: strict

Signed-off-by: Michael Cho <michael@michaelcho.dev>
This commit is contained in:
Michael Cho 2024-03-29 18:26:13 -04:00
parent 3054b91e80
commit fb5306fc35
No known key found for this signature in database
GPG Key ID: 55E85E28A7CD1E85
2 changed files with 14 additions and 8 deletions

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module Language module Language
@ -11,6 +11,7 @@ module Language
"cache=#{HOMEBREW_CACHE}/npm_cache" "cache=#{HOMEBREW_CACHE}/npm_cache"
end end
sig { returns(String) }
def self.pack_for_installation def self.pack_for_installation
# Homebrew assumes the buildpath/testpath will always be disposable # Homebrew assumes the buildpath/testpath will always be disposable
# and from npm 5.0.0 the logic changed so that when a directory is # and from npm 5.0.0 the logic changed so that when a directory is
@ -36,11 +37,12 @@ module Language
output.lines.last.chomp output.lines.last.chomp
end end
sig { void }
def self.setup_npm_environment def self.setup_npm_environment
# guard that this is only run once # guard that this is only run once
return if @env_set return if @env_set
@env_set = true @env_set = T.let(true, T.nilable(T::Boolean))
# explicitly use our npm and node-gyp executables instead of the user # explicitly use our npm and node-gyp executables instead of the user
# managed ones in HOMEBREW_PREFIX/lib/node_modules which might be broken # managed ones in HOMEBREW_PREFIX/lib/node_modules which might be broken
begin begin
@ -50,6 +52,7 @@ module Language
end end
end end
sig { params(libexec: Pathname).returns(T::Array[String]) }
def self.std_npm_install_args(libexec) def self.std_npm_install_args(libexec)
setup_npm_environment setup_npm_environment
# tell npm to not install .brew_home by adding it to the .npmignore file # tell npm to not install .brew_home by adding it to the .npmignore file
@ -95,7 +98,7 @@ module Language
NODE_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?node( |$)} NODE_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?node( |$)}
# The length of the longest shebang matching `SHEBANG_REGEX`. # The length of the longest shebang matching `SHEBANG_REGEX`.
NODE_SHEBANG_MAX_LENGTH = "#! /usr/bin/env node ".length NODE_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env node ".length, Integer)
# @private # @private
sig { params(node_path: T.any(String, Pathname)).returns(Utils::Shebang::RewriteInfo) } sig { params(node_path: T.any(String, Pathname)).returns(Utils::Shebang::RewriteInfo) }
@ -107,8 +110,8 @@ module Language
) )
end end
sig { params(formula: T.untyped).returns(Utils::Shebang::RewriteInfo) } sig { params(formula: Formula).returns(Utils::Shebang::RewriteInfo) }
def detected_node_shebang(formula = self) def detected_node_shebang(formula = T.cast(self, Formula))
node_deps = formula.deps.map(&:name).grep(/^node(@.+)?$/) node_deps = formula.deps.map(&:name).grep(/^node(@.+)?$/)
raise ShebangDetectionError.new("Node", "formula does not depend on Node") if node_deps.empty? raise ShebangDetectionError.new("Node", "formula does not depend on Node") if node_deps.empty?
raise ShebangDetectionError.new("Node", "formula has multiple Node dependencies") if node_deps.length > 1 raise ShebangDetectionError.new("Node", "formula has multiple Node dependencies") if node_deps.length > 1

View File

@ -15,17 +15,20 @@ RSpec.describe Language::Node do
expect(ENV).to receive(:prepend_path) expect(ENV).to receive(:prepend_path)
end end
described_class.instance_variable_set(:@env_set, false) described_class.instance_variable_set(:@env_set, false)
expect(described_class.setup_npm_environment).to be_nil described_class.setup_npm_environment
expect(described_class.instance_variable_get(:@env_set)).to be(true) expect(described_class.instance_variable_get(:@env_set)).to be(true)
without_partial_double_verification do without_partial_double_verification do
expect(ENV).not_to receive(:prepend_path) expect(ENV).not_to receive(:prepend_path)
end end
expect(described_class.setup_npm_environment).to be_nil described_class.setup_npm_environment
end end
it "does not call prepend_path when node formula does not exist" do it "does not call prepend_path when node formula does not exist" do
expect(described_class.setup_npm_environment).to be_nil without_partial_double_verification do
expect(ENV).not_to receive(:prepend_path)
end
described_class.setup_npm_environment
end end
end end