Skip to content

Commit dbe4349

Browse files
authored
Merge pull request #455 from Shopify/at/rbs-comment-signatures
Migrate Sorbet signatures to RBS comments
2 parents de4e234 + c0f9886 commit dbe4349

87 files changed

Lines changed: 719 additions & 1102 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@
1010
/tmp/
1111
.rubocop-*
1212
.byebug_history
13-
sorbet/rbi/hidden-definitions/errors.txt

.rubocop.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ AllCops:
1313
Exclude:
1414
- 'test/fixtures/**/*'
1515

16+
Layout/LeadingCommentSpace:
17+
AllowRBSInlineAnnotation: true
18+
19+
Layout/LineLength:
20+
AllowRBSInlineAnnotation: true
21+
1622
Sorbet/ConstantsFromStrings:
1723
Enabled: true
1824

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ gem("minitest-mock")
2323

2424
gem("m")
2525
gem("rake")
26-
gem("sorbet-static-and-runtime")
26+
gem("sorbet-static")
2727
gem("zeitwerk")

Gemfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ PATH
1111
parallel (< 2)
1212
parser
1313
prism (>= 1.4.0)
14-
sorbet-runtime (>= 0.5.9914)
1514
zeitwerk (>= 2.6.1)
1615

1716
GEM
@@ -206,15 +205,15 @@ GEM
206205
thor (>= 0.19.2)
207206
spring (4.0.0)
208207
stringio (3.1.7)
209-
tapioca (0.19.0)
208+
tapioca (0.19.1)
210209
benchmark
211210
bundler (>= 2.2.25)
212211
netrc (>= 0.11.0)
213212
parallel (>= 1.21.0)
214213
rbi (>= 0.3.7)
215214
require-hooks (>= 0.2.2)
216215
rubydex (>= 0.1.0.beta10)
217-
sorbet-static-and-runtime (>= 0.5.11087)
216+
sorbet-static-and-runtime (>= 0.6.12698)
218217
spoom (>= 1.7.9)
219218
thor (>= 1.2.0)
220219
tsort
@@ -233,6 +232,7 @@ PLATFORMS
233232
arm64-darwin-21
234233
arm64-darwin-22
235234
arm64-darwin-23
235+
arm64-darwin-25
236236
x86_64-darwin
237237
x86_64-darwin-20
238238
x86_64-linux
@@ -251,7 +251,7 @@ DEPENDENCIES
251251
rubocop-performance
252252
rubocop-shopify
253253
rubocop-sorbet
254-
sorbet-static-and-runtime
254+
sorbet-static
255255
spring
256256
tapioca
257257
zeitwerk

USAGE.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,24 +302,28 @@ class MyChecker
302302
include Packwerk::Checker
303303
# implement the `Checker` interface
304304

305-
sig { override.returns(String) }
305+
# @override
306+
#: -> String
306307
def violation_type
307308
'my_custom_violation_type'
308309
end
309310

310-
sig { override.params(listed_offense: ReferenceOffense).returns(T::Boolean) }
311+
# @override
312+
#: (ReferenceOffense listed_offense) -> bool
311313
def strict_mode_violation?(listed_offense)
312314
# This will allow "strict mode" to be supported in your checker
313315
referencing_package = listed_offense.reference.package
314316
referencing_package.config["enforce_custom"] == "strict"
315317
end
316318

317-
sig { override.params(reference: Reference).returns(T::Boolean) }
319+
# @override
320+
#: (Reference reference) -> bool
318321
def invalid_reference?(reference)
319322
# your logic here
320323
end
321324

322-
sig { override.params(reference: Reference).returns(String) }
325+
# @override
326+
#: (Reference reference) -> String
323327
def message(reference)
324328
# your message here
325329
end
@@ -345,12 +349,14 @@ class MyValidator
345349
include Packwerk::Validator
346350
# implement the `Validator` interface
347351

348-
sig { override.returns(T::Array[String]) }
352+
# @override
353+
#: -> Array[String]
349354
def permitted_keys
350355
['enforce_my_custom_checker']
351356
end
352357

353-
sig { override.params(package_set: PackageSet, configuration: Configuration).returns(ApplicationValidator::Result) }
358+
# @override
359+
#: (PackageSet package_set, Configuration configuration) -> ApplicationValidator::Result
354360
def call(package_set, configuration)
355361
# your logic here
356362
end

exe/packwerk

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#!/usr/bin/env ruby
22
# frozen_string_literal: true
33

4-
unless defined?(Spring)
5-
require "packwerk/disable_sorbet"
6-
end
7-
84
require "packwerk"
95

106
# Needs to be run in test environment in order to have test helper paths available in the autoload paths

lib/packwerk.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# typed: strict
22
# frozen_string_literal: true
33

4-
require "sorbet-runtime"
54
require "active_support"
65
require "fileutils"
76
require "stringio"

lib/packwerk/application_validator.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ module Packwerk
1010
# correct results.
1111
class ApplicationValidator
1212
include Validator
13-
extend T::Sig
1413
extend ActiveSupport::Autoload
1514

1615
autoload :Helpers
1716

18-
sig { params(package_set: PackageSet, configuration: Configuration).returns(Validator::Result) }
17+
#: (PackageSet package_set, Configuration configuration) -> Validator::Result
1918
def check_all(package_set, configuration)
2019
results = Validator.all.flat_map { |validator| validator.call(package_set, configuration) }
2120
merge_results(results)
2221
end
2322

24-
sig { override.params(package_set: PackageSet, configuration: Configuration).returns(Validator::Result) }
23+
# @override
24+
#: (PackageSet package_set, Configuration configuration) -> Validator::Result
2525
def call(package_set, configuration)
2626
results = [
2727
check_package_manifest_syntax(configuration),
@@ -33,14 +33,15 @@ def call(package_set, configuration)
3333
merge_results(results, separator: "\n❓ ")
3434
end
3535

36-
sig { override.returns(T::Array[String]) }
36+
# @override
37+
#: -> Array[String]
3738
def permitted_keys
3839
[
3940
"metadata",
4041
]
4142
end
4243

43-
sig { params(configuration: Configuration).returns(Validator::Result) }
44+
#: (Configuration configuration) -> Validator::Result
4445
def check_package_manifest_syntax(configuration)
4546
errors = []
4647

@@ -68,7 +69,7 @@ def check_package_manifest_syntax(configuration)
6869
end
6970
end
7071

71-
sig { params(configuration: Configuration).returns(Validator::Result) }
72+
#: (Configuration configuration) -> Validator::Result
7273
def check_application_structure(configuration)
7374
resolver = ConstantResolver.new(
7475
root_path: configuration.root_path.to_s,
@@ -84,7 +85,7 @@ def check_application_structure(configuration)
8485
end
8586
end
8687

87-
sig { params(configuration: Configuration).returns(Validator::Result) }
88+
#: (Configuration configuration) -> Validator::Result
8889
def check_package_manifest_paths(configuration)
8990
all_package_manifests = package_manifests(configuration, "**/")
9091
package_paths_package_manifests = package_manifests(configuration, package_glob(configuration))
@@ -105,7 +106,7 @@ def check_package_manifest_paths(configuration)
105106
end
106107
end
107108

108-
sig { params(configuration: Configuration).returns(Validator::Result) }
109+
#: (Configuration configuration) -> Validator::Result
109110
def check_root_package_exists(configuration)
110111
root_package_path = File.join(configuration.root_path, "package.yml")
111112
all_packages_manifests = package_manifests(configuration, package_glob(configuration))
@@ -124,12 +125,12 @@ def check_root_package_exists(configuration)
124125

125126
private
126127

127-
sig { params(list: T.untyped).returns(T.untyped) }
128+
#: (untyped list) -> untyped
128129
def format_yaml_strings(list)
129130
list.sort.map { |p| "- \"#{p}\"" }.join("\n")
130131
end
131132

132-
sig { params(configuration: Configuration, paths: T::Array[String]).returns(T::Array[Pathname]) }
133+
#: (Configuration configuration, Array[String] paths) -> Array[Pathname]
133134
def relative_paths(configuration, paths)
134135
paths.map { |path| relative_path(configuration, path) }
135136
end

lib/packwerk/association_inspector.rb

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,28 @@
44
module Packwerk
55
# Extracts the implicit constant reference from an active record association
66
class AssociationInspector
7-
extend T::Sig
87
include ConstantNameInspector
98

10-
CustomAssociations = T.type_alias { T.any(T::Array[Symbol], T::Set[Symbol]) }
11-
12-
RAILS_ASSOCIATIONS = T.let(
13-
[
14-
:belongs_to,
15-
:has_many,
16-
:has_one,
17-
:has_and_belongs_to_many,
18-
].to_set,
19-
CustomAssociations
20-
)
21-
22-
sig do
23-
params(
24-
inflector: T.class_of(ActiveSupport::Inflector),
25-
custom_associations: CustomAssociations,
26-
excluded_files: T::Set[String]
27-
).void
28-
end
9+
RAILS_ASSOCIATIONS = [
10+
:belongs_to,
11+
:has_many,
12+
:has_one,
13+
:has_and_belongs_to_many,
14+
].to_set #: Set[Symbol]
15+
16+
#: (
17+
#| inflector: singleton(ActiveSupport::Inflector),
18+
#| ?custom_associations: Set[Symbol],
19+
#| ?excluded_files: Set[String]
20+
#| ) -> void
2921
def initialize(inflector:, custom_associations: Set.new, excluded_files: Set.new)
3022
@inflector = inflector
31-
@associations = T.let(RAILS_ASSOCIATIONS + custom_associations, CustomAssociations)
32-
@excluded_files = T.let(excluded_files, T::Set[String])
23+
@associations = RAILS_ASSOCIATIONS + custom_associations #: Set[Symbol]
24+
@excluded_files = excluded_files #: Set[String]
3325
end
3426

35-
sig do
36-
override
37-
.params(node: AST::Node, ancestors: T::Array[AST::Node], relative_file: String)
38-
.returns(T.nilable(String))
39-
end
27+
# @override
28+
#: (AST::Node node, ancestors: Array[AST::Node], relative_file: String) -> String?
4029
def constant_name_from_node(node, ancestors:, relative_file:)
4130
return unless NodeHelpers.method_call?(node)
4231
return if excluded?(relative_file)
@@ -56,28 +45,28 @@ def constant_name_from_node(node, ancestors:, relative_file:)
5645

5746
private
5847

59-
sig { params(relative_file: String).returns(T::Boolean) }
48+
#: (String relative_file) -> bool
6049
def excluded?(relative_file)
6150
@excluded_files.include?(relative_file)
6251
end
6352

64-
sig { params(node: AST::Node).returns(T::Boolean) }
53+
#: (AST::Node node) -> bool
6554
def association?(node)
6655
method_name = NodeHelpers.method_name(node)
6756
@associations.include?(method_name)
6857
end
6958

70-
sig { params(arguments: T::Array[AST::Node]).returns(T.nilable(AST::Node)) }
59+
#: (Array[AST::Node] arguments) -> AST::Node?
7160
def custom_class_name(arguments)
7261
association_options = arguments.detect { |n| NodeHelpers.hash?(n) }
7362
return unless association_options
7463

7564
NodeHelpers.value_from_hash(association_options, :class_name)
7665
end
7766

78-
sig { params(arguments: T::Array[AST::Node]).returns(T.any(T.nilable(Symbol), T.nilable(String))) }
67+
#: (Array[AST::Node] arguments) -> (Symbol | String)?
7968
def association_name(arguments)
80-
association_name_node = T.must(arguments[0])
69+
association_name_node = arguments[0] #: as !nil
8170
return unless NodeHelpers.symbol?(association_name_node)
8271

8372
NodeHelpers.literal_value(association_name_node)

0 commit comments

Comments
 (0)