Skip to content

stdlib: Add types for URI::MailTo - #1858

Merged
soutaro merged 1 commit into
ruby:masterfrom
tk0miya:uri_mailto
Jun 6, 2024
Merged

soutaro merged 1 commit into
ruby:masterfrom
tk0miya:uri_mailto

Conversation

@tk0miya

@tk0miya tk0miya commented Jun 2, 2024

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread stdlib/uri/0/mailto.rbs
# RFC6068, the mailto URL scheme.
#
class MailTo < Generic
EMAIL_REGEXP: Regexp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This constant is not documented in the official document.
https://docs.ruby-lang.org/en/3.3/URI/MailTo.html

But it's well used for verifying the format of email addresses.
For example, the Rails Guide mentions this constant in implementing validators on Rails.
https://guides.rubyonrails.org/active_record_validations.html#custom-validators

Comment thread stdlib/uri/0/mailto.rbs
# m3 = URI::MailTo.build({:to => 'listman@example.com', :headers => [['subject', 'subscribe']]})
# m3.to_s # => "mailto:listman@example.com?subject=subscribe"
#
def self.build: (Array[String]) -> instance

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The official document says URI::MailTo.build takes a tuple of strings. So it would be correct and accurate to use ([String, String]) -> instance instead of this line. But it's not good for use because Steep recognizes the type of tuple literal of strings as an Array[String].

Comment thread stdlib/uri/0/mailto.rbs
#
def self.build: (Array[String]) -> instance
| ([String, Array[Array[String]]]) -> instance
| (Hash[Symbol, String | Array[Array[String]]]) -> instance

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This would also be correct and accurate to use a record type ({ to: String, headers: String | Array[Array[String]] }) -> instance instead. But it's not good for use too.

Comment thread stdlib/uri/0/mailto.rbs
# <!-- rdoc-file=lib/uri/mailto.rb -->
# E-mail headers set by the URL, as an Array of Arrays.
#
def headers: () -> Array[[String, String]]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I found invalid pattern by raap.

$ bundle exec raap -r uri --library uri URI::MailTo#headers
[RaaP] INFO: # URI::MailTo
[RaaP] INFO: ## def headers: () -> ::Array[[ ::String, ::String ]]
EEF

() -> Array[[String, String]]

[RaaP] INFO: success: 0, skip: 0, exception: 2, time: 15ms
# 1) Failure:

def headers: () -> ::Array[[ ::String, ::String ]]
  in /Users/ksss/src/github.com/ksss/rbs/stdlib/uri/0/mailto.rbs:46:17...46:46

## Reason

Failed in case of `#<URI::MailTo>.headers() -> [["ez"]][Array[Array[String]]]`

### Repro

```rb
uri_mailto = URI::MailTo.new(nil, nil, 'lm', -4, nil, nil, nil, 'ez', nil)
uri_mailto.headers()
```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like the query (?ez) cannot be constructed with URI::MailTo.build.

irb(main):020> URI::MailTo.build(["hoge@huga", "foo"]).headers
/Users/soutaro/.rbenv/versions/3.3.2/lib/ruby/3.3.0/uri/mailto.rb:212:in `check_headers': bad component(expected opaque component): foo (URI::InvalidComponentError)

I think we can have Array[[String, String]] here, even raap detects an error.

@soutaro soutaro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@soutaro
soutaro added this pull request to the merge queue Jun 6, 2024
@soutaro soutaro added this to the RBS 3.5 milestone Jun 6, 2024
Merged via the queue into ruby:master with commit ca651ca Jun 6, 2024
@soutaro soutaro added the Released PRs already included in the released version label Jun 6, 2024
@tk0miya
tk0miya deleted the uri_mailto branch June 6, 2024 10:37
@tk0miya

tk0miya commented Jun 6, 2024

Copy link
Copy Markdown
Contributor Author

Thank you for your review and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Released PRs already included in the released version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants