Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an easier syntax to create methods and signatures #284

Merged
merged 1 commit into from
Apr 4, 2024

Conversation

Morriar
Copy link
Collaborator

@Morriar Morriar commented Apr 4, 2024

Creating a new method with a signature can be a bit verbose when manipulating the RBI classes directly:

tree << RBI::Method.new("foo") do |node|
  node << RBI::ReqParam.new("p1")
  node << RBI::OptParam.new("p2", "42")

  node.sigs << RBI::Sig.new do |sig|
    sig << RBI::SigParam.new("p1", "T.untyped")
    sig << RBI::SigParam.new("p2", "Integer")
  end
end

This PR introduces a more elegant syntax:

tree << RBI::Method.new("foo") do |node|
  node.add_param("p1")
  node.add_opt_param("p2", "42")

  node.add_sig do |sig|
    sig.add_param("p1", "T.untyped")
    sig.add_param("p2", "Integer")
  end
end

@Morriar Morriar added the enhancement New feature or request label Apr 4, 2024
@Morriar Morriar self-assigned this Apr 4, 2024
@Morriar Morriar requested a review from a team as a code owner April 4, 2024 15:15
@Morriar Morriar requested review from andyw8 and KaanOzkan April 4, 2024 15:15
@@ -534,6 +534,79 @@ def <<(param)
@params << param
end

sig { params(name: String).void }
def add_param(name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are all in the form add_..., perhaps it could be shortened to param, opt_param etc? No big deal though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is with add_sig, defining a method named sig in a Sorbet project is creating all kind of trouble. So I went with add_ prefix everywhere

Copy link
Member

@paracycle paracycle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants