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

[Writer] Escape method names that are invalid with def syntax #537

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/rbs/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,11 @@ def write_member(member)
def method_name(name)
s = name.to_s

if /\A#{Parser::KEYWORDS_RE}\z/.match?(s)
"`#{s}`"
else
if [:tOPERATOR, :kAMP, :kHAT, :kSTAR, :kLT, :kEXCLAMATION, :kSTAR2, :kBAR].include?(Parser::PUNCTS[s]) ||
(/\A[a-zA-Z_]\w*[?!=]?\z/.match?(s) && !/\Aself\??\z/.match?(s))
s
else
"`#{s}`"
end
end

Expand Down
8 changes: 7 additions & 1 deletion test/rbs/writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,16 @@ def !: () -> void

def __id__: () -> Integer

def `def`: () -> Symbol
def def: () -> Symbol

def `self`: () -> void

def `self?`: () -> void

def timeout: () -> Integer

def `foo!=`: () -> Integer

def `: (String) -> untyped
end
SIG
Expand Down