-
Notifications
You must be signed in to change notification settings - Fork 216
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
[Writer] Escape method names that are invalid with def
syntax
#537
[Writer] Escape method names that are invalid with def
syntax
#537
Conversation
lib/rbs/writer.rb
Outdated
"`#{s}`" | ||
else | ||
if [:tOPERATOR, :kAMP, :kHAT, :kSTAR, :kLT, :kEXCLAMATION, :kSTAR2, :kBAR].include?(Parser::PUNCTS[s]) || | ||
(/\A[a-zA-Z_]\w*[?!=]?\z/.match?(s) && !/\A#{Parser::KEYWORDS_RE}\z/.match?(s)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RBS::Writer
escapes keywords, for example: ``def def
: () -> untyped`. But this escaping is not necessary because it is allowed.
I keep this escaping because of compatibility, and I'm not sure why this escaping needs to keep.
Line 114 in a1e6261
def `def`: () -> Symbol |
If this escaping is not necessary, I can remove the escaping easily by removing && !/\A#{Parser::KEYWORDS_RE}\z/.match?(s)
condition.
What do you think? @soutaro
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense. I don't think we need to keep the quoting behavior.
Let's make it:
- Unquoted even if it is a keyword (I'm proposing this change)
- Unquoted if it is safe
/\A[a-zA-Z_]\w*[?!=]?\z/
- Quoted otherwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your comment. I applied this charge with 2b03212.
But I noticed we still need to quote self
and self?
as a method name because it causes syntax error without quote.
So I replace Parser::KEYWORDS_RE
with /\Aself\??\z/
.
Except `self` and `self?` because they need quote.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
* Don't escape keywords ruby/rbs#537 * Fix accessibilities ruby/rbs#545
This pull request fixes the escaping strategy of RBS::Writer to escape invalid method names for
def
syntax correctly.Problem
Currently
RBS::Writer
doesn't escape invalid method names fordef
syntax. For exampleI expect
def `foo!=`: () -> untyped
as the output, but it displays without backtick escaping. So it generates invalid RBS.By the way, we can't define
foo!=
method withdef
syntax even in Ruby. But we can usedefine_method
for this method name.Solution
Escape method names correctly.