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

Support singleton attr by prototype rb #505

Merged
merged 1 commit into from Dec 6, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/rbs/prototype/rb.rb
Expand Up @@ -15,6 +15,14 @@ def method_kind
:instance
end
end

def attribute_kind
Copy link
Member Author

Choose a reason for hiding this comment

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

rbs/lib/rbs/parser.y

Lines 205 to 207 in 8871f6d

attribute_kind:
{ result = :instance }
| kSELF kDOT { result = :singleton }

Attribute doesn't support singleton_instance syntax (self?), unlike method definition. Is it intentional?

Copy link
Member

Choose a reason for hiding this comment

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

It's intentional but I'm open to adding the syntax if it's better.

I was assuming that we rarely define attributes for both singleton and instance while the presence of module_function implies it happens frequently fordefs.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was assuming that we rarely define attributes for both singleton and instance while the presence of module_function implies it happens frequently for defs.

I completely agree. It is rare and I do not have any use cases for singleton_instance attributes.

if singleton
:singleton
else
:instance
end
end
end

attr_reader :source_decls
Expand Down Expand Up @@ -202,7 +210,7 @@ def process(node, decls:, comments:, context:)
name: name,
ivar_name: nil,
type: Types::Bases::Any.new(location: nil),
kind: :instance,
kind: context.attribute_kind,
location: nil,
comment: comments[node.first_lineno - 1],
annotations: []
Expand All @@ -216,7 +224,7 @@ def process(node, decls:, comments:, context:)
name: name,
ivar_name: nil,
type: Types::Bases::Any.new(location: nil),
kind: :instance,
kind: context.attribute_kind,
location: nil,
comment: comments[node.first_lineno - 1],
annotations: []
Expand All @@ -230,7 +238,7 @@ def process(node, decls:, comments:, context:)
name: name,
ivar_name: nil,
type: Types::Bases::Any.new(location: nil),
kind: :instance,
kind: context.attribute_kind,
location: nil,
comment: comments[node.first_lineno - 1],
annotations: []
Expand Down
16 changes: 16 additions & 0 deletions test/rbs/rb_prototype_test.rb
Expand Up @@ -250,6 +250,12 @@ class Hello
attr_reader :x
attr_accessor :y, :z
attr_writer foo, :a, 'b'

class << self
attr_reader :x2
attr_accessor :y2, :z2
attr_writer foo2, :a2, 'b2'
end
end

module Mod
Expand Down Expand Up @@ -278,6 +284,16 @@ class Hello
attr_writer a: untyped

attr_writer b: untyped

attr_reader self.x2: untyped

attr_accessor self.y2: untyped

attr_accessor self.z2: untyped

attr_writer self.a2: untyped

attr_writer self.b2: untyped
end

module Mod
Expand Down