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

Handle @option tags #68

Closed
wants to merge 4 commits into from

Conversation

connorshea
Copy link
Contributor

@connorshea connorshea commented Jul 1, 2019

Implements #59.

Input:

module A
  # @param [Hash] opts
  # @option opts [String] bar
  # @option opts [Integer] baz
  # @return [void]
  def foo(opts); end
end

Output (Before):

# typed: strong
module A
  sig { params(opts: Hash).void }
  def foo(opts); end
end

Output (After):

# typed: strong
module A
  sig { params(opts: { bar: String, baz: Integer }).void }
  def foo(opts); end
end

Adds five test cases.

I didn't update sord.rbi as part of this so as not to conflict with #67, but you should probably regenerate it after merging this (if you merge it). Then the keys available in the options hash for the RbiGenerator initializer will be explicitly specified :)

At least, what I expect them to look like.
I found this issue in Bundler, now it should generate the correct
signatures.
@connorshea connorshea changed the title Handle option tags Handle @option tags Jul 1, 2019
@connorshea
Copy link
Contributor Author

connorshea commented Jul 1, 2019

You can find these in the files generated with the seed task by searching for : {, rouge has quite a few.

@connorshea
Copy link
Contributor Author

One other problem I just noticed (will probably fix it tomorrow, feel free to fix it yourself if you want), the options parser will accept invalid keys, this happens in the GitLab gem a decent amount:

Input:

# Get all license templates.
# @param  [Hash] options A customizable set of options.
# @option options [Boolean] popular(optional) If passed, returns only popular licenses.
# @return [Array<Gitlab::ObjectifiedHash>]
def license_templates(options = {})
  get('/templates/licenses', query: options)
end

Output:

sig { params(key: String, options: { project(optional): String, fullname(optional): String }).returns(Gitlab::ObjectifiedHash) }
def license_template(key, options = {}); end

These should probably output SORD_ERRORs?

@connorshea
Copy link
Contributor Author

Another problem: Sorbet doesn't currently have a way to make the key/value pairs in a Shape optional :/

This is probably not a good idea to merge, then. Maybe put it behind a flag or something?

@AaronC81
Copy link
Owner

AaronC81 commented Jul 1, 2019

The lack of optional keys in shapes is a real nuisance!

This is probably not a good idea to merge, then. Maybe put it behind a flag or something?

I suppose it could go behind a flag called --experimental-option-tags or something, sure!


I have devised an incredibly hacky and horrible-feeling solution that is quite honestly a disgrace to Sorbet, but it seems to work! which might work with some tweaking? (correction: it doesn't actually seem to work at the moment; it allows incorrectly-typed values or extraneous keys) You essentially work out the powerset of the optional parts of the shape, and shove all the results in a T.any. Here's an example on sorbet.run.

Edit: Yeah, this really doesn't work because of shapes allowing extra keys. It'll keep going down the list of types in T.any until it finds one without a particular key if that key was mistyped, so this just allows anything.

@AaronC81
Copy link
Owner

AaronC81 commented Jul 1, 2019

I had a look through Sorbet's own RBIs to see how they chose to implement this, but it looks as if they're mostly just typed as Hash, T::Hash[Symbol, T.untyped], or just outright T.untyped: https://github.com/search?q=options+path%3A%2Frbi+repo%3Asorbet%2Fsorbet&type=Code

@connorshea
Copy link
Contributor Author

I opened an issue to add optional key/value pairs in Shapes: sorbet/sorbet#1160

@connorshea
Copy link
Contributor Author

I would say without the ability for optional key/value pairs, this isn't worth bothering with. Maybe just have Sord detect @option tags and set the parameter to a Hash?

@AaronC81
Copy link
Owner

AaronC81 commented Jul 3, 2019

Yeah, I'd say that would be the best course of action for now.

@connorshea
Copy link
Contributor Author

I'd say the benefits aren't really worth the trouble right now, so I'm closing this.

@connorshea connorshea closed this Jul 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants