forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add package support to
T::NonForcingConstants
(sorbet#3470)
* Add helper method to mangle a package name in the same way as packager pass * Update RBI to know about third optional arg to non_forcing_is_a * Implement support for packaged non_forcing_is_a in resolver * Add test case * Update exps * Add stubbed-out runtime implementation * Handle `package` being a keyword arg * Update exp files * Also return if the package literal is not a string * format_cxx * Fix runtime behavior... for now * Remove expectation test for now * Change to lookupMangledPackageName * Add test to exercise non-existent package and fix error message printing * Just else * Add a comment too * Share the comment * Add todo comment
- Loading branch information
Showing
11 changed files
with
239 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
test/testdata/packager/non_forcing_constants/bar/__package.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# typed: strict | ||
# enable-packager: true | ||
|
||
class Project::Bar < PackageSpec | ||
import Project::Foo | ||
|
||
export Bar | ||
export_methods BarMethods | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# typed: strict | ||
|
||
class Bar | ||
extend T::Sig | ||
sig {params(value: Integer).void} | ||
def initialize(value) | ||
@value = T.let(value, Integer) | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
test/testdata/packager/non_forcing_constants/bar/bar_methods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# typed: strict | ||
|
||
module BarMethods | ||
extend T::Sig | ||
|
||
sig {returns(Project::Foo::Foo)} | ||
def build_foo | ||
# Construct an imported class. | ||
Project::Foo::Foo.new(10) | ||
end | ||
|
||
sig {returns(T::Boolean)} | ||
def check_bar | ||
# Call an imported method. | ||
Project::Foo.good_check_is_bar(Bar.new(5)) | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
test/testdata/packager/non_forcing_constants/foo/__package.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# typed: strict | ||
|
||
class Project::Foo < PackageSpec | ||
export Foo | ||
export_methods FooMethods | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# typed: strict | ||
|
||
class Foo | ||
extend T::Sig | ||
sig {params(value: Integer).void} | ||
def initialize(value) | ||
@value = T.let(value, Integer) | ||
end | ||
end |
77 changes: 77 additions & 0 deletions
77
test/testdata/packager/non_forcing_constants/foo/foo_methods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# typed: strict | ||
|
||
module FooMethods | ||
extend T::Sig | ||
|
||
sig {params(arg: T.untyped).returns(T::Boolean)} | ||
def global_check_is_bar(arg) | ||
if T::NonForcingConstants.non_forcing_is_a?(arg, "::Bar") # error: Unable to resolve constant `::Bar` | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
sig {params(arg: T.untyped).returns(T::Boolean)} | ||
def bad_not_keyword_arg(arg) | ||
if T::NonForcingConstants.non_forcing_is_a?(arg, "::Bar", "Project::Bar") # error: Too many positional arguments | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
sig {params(arg: T.untyped).returns(T::Boolean)} | ||
def bad_wrong_keyword_arg(arg) | ||
if T::NonForcingConstants.non_forcing_is_a?(arg, "::Bar", pakige: "Project::Bar") # error: Unrecognized keyword argument | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
sig {params(arg: T.untyped).returns(T::Boolean)} | ||
def bad_package_not_a_string(arg) | ||
if T::NonForcingConstants.non_forcing_is_a?(arg, "::Bar", package: 5) # error: Expected `T.nilable(String)` but found `Integer(5)` | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
sig {params(arg: T.untyped).returns(T::Boolean)} | ||
def bad_check_for_global_bar(arg) | ||
if T::NonForcingConstants.non_forcing_is_a?(arg, "::Bar", package: "Project::Bar") # error: should not be an absolute constant reference | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
sig {params(arg: T.untyped).returns(T::Boolean)} | ||
def bad_non_existent_package(arg) | ||
if T::NonForcingConstants.non_forcing_is_a?(arg, "Bar", package: "Project::Quux") # error: Unable to find package | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
sig {params(arg: T.untyped).returns(T::Boolean)} | ||
def check_for_non_existing_bar(arg) | ||
if T::NonForcingConstants.non_forcing_is_a?(arg, "Quux", package: "Project::Bar") # error: Unable to resolve constant `::<PackageRegistry>::Project_Bar_Package::Quux` | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
sig {params(arg: T.untyped).returns(T::Boolean)} | ||
def good_check_is_bar(arg) | ||
if T::NonForcingConstants.non_forcing_is_a?(arg, "Bar", package: "Project::Bar") | ||
true | ||
else | ||
false | ||
end | ||
end | ||
end |