Skip to content

Commit 835852d

Browse files
committed
refactor: improve type signatures
- Add typed: strict sigil and signatures to Utils - Use T::Hash[Symbol, T.untyped] for create_code_team attributes - Fix nilable string handling in Plugin.default_data_accessor_name
1 parent 2ca5095 commit 835852d

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

lib/code_teams/plugin.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def self.data_accessor_name(key = default_data_accessor_name)
2525
sig { returns(String) }
2626
def self.default_data_accessor_name
2727
# e.g., MyNamespace::MyPlugin -> my_plugin
28-
Utils.underscore(Utils.demodulize(name))
28+
Utils.underscore(Utils.demodulize(T.must(name)))
2929
end
3030

3131
sig { params(base: T.untyped).void }

lib/code_teams/testing.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ def self.enable!
4040
end
4141
end
4242

43-
sig { params(attributes: T::Hash[T.untyped, T.untyped]).returns(CodeTeams::Team) }
43+
sig { params(attributes: T::Hash[Symbol, T.untyped]).returns(CodeTeams::Team) }
4444
def self.create_code_team(attributes)
4545
attributes = attributes.dup
4646
attributes[:name] ||= "Fake Team #{SecureRandom.hex(4)}"
4747

4848
code_team = CodeTeams::Team.new(
4949
config_yml: 'tmp/fake_config.yml',
50-
raw_hash: T.cast(Utils.deep_stringify_keys(attributes), T::Hash[T.untyped, T.untyped])
50+
raw_hash: Utils.deep_stringify_keys(attributes)
5151
)
5252

5353
code_teams << code_team

lib/code_teams/utils.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
# frozen_string_literal: true
2+
#
3+
# typed: strict
4+
15
module CodeTeams
26
module Utils
7+
extend T::Sig
8+
39
module_function
410

11+
sig { params(string: String).returns(String) }
512
def underscore(string)
613
string.gsub('::', '/')
714
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
@@ -10,10 +17,13 @@ def underscore(string)
1017
.downcase
1118
end
1219

20+
sig { params(string: String).returns(String) }
1321
def demodulize(string)
14-
string.split('::').last
22+
T.must(string.split('::').last)
1523
end
1624

25+
# Recursively converts symbol keys to strings. Top-level input should be a Hash.
26+
sig { params(value: T.untyped).returns(T.untyped) }
1727
def deep_stringify_keys(value)
1828
case value
1929
when Hash

0 commit comments

Comments
 (0)