Skip to content

Accept keyword as var name opt#155

Merged
soutaro merged 2 commits intoruby:masterfrom
pocke:accept-keyword-as-var_name_opt
Jan 2, 2020
Merged

Accept keyword as var name opt#155
soutaro merged 2 commits intoruby:masterfrom
pocke:accept-keyword-as-var_name_opt

Conversation

@pocke
Copy link
Member

@pocke pocke commented Dec 31, 2019

Fix #154

This pull request allows you to write a keyword as an argument name with escaping by backticks.

class C
  # ok
  def foo: (untyped `type`, void: untyped `void`) -> untyped

  # syntax error
  def bar: (untyped type, void: untyped void) -> untyped

  # syntax error
  def baz: (untyped `**`) -> untyped
end

This pull request also fixes the Writer to generate code with the escaping.


By the way, it also includes a bug fix of Writer for method name escaping.
https://github.com/ruby/ruby-signature/pull/155/files#diff-5e2ea3c3d2ea351a23bbb3cddd984c7dR206

class C
  # Currently Writer escape `timeout` with backticks, because it ends with "out" and "out" is a keyword.
  def `timeout`: () -> Integer

  # But the escaping is unnecessary!
  def timeout: () -> Integer
end

```rbs
class C
  # ok
  def foo: (untyped `type`, void: untyped `void`) -> untyped

  # syntax error
  def bar: (untyped type, void: untyped void) -> untyped

  # syntax error
  def baz: (untyped `**`) -> untyped
end
```
s = name.to_s

if s =~ Parser::KEYWORDS_RE
if /\A#{Parser::KEYWORDS_RE}\z/.match?(s)
Copy link
Member

Choose a reason for hiding this comment

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

💡 🙏

@soutaro soutaro merged commit 7cc396b into ruby:master Jan 2, 2020
@pocke pocke deleted the accept-keyword-as-var_name_opt branch January 2, 2020 05:34
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.

Keyword as argument name is syntax error

2 participants