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 instance variables, class variables and class instance variables #149

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tk0miya
Copy link
Contributor

@tk0miya tk0miya commented Nov 22, 2024

This adds support for instance variables, class variables and class instance variables.

After this change, RBS::Inline can extract them from assignment for these variables.

Example:

class Example
  def method
    @ivar = 123  #: Integer
    @@cvar = "blah blah"  #: String
  end
end
class Example
  def method: () -> untyped
  @ivar: Integer
  @@cvar: String
end

Comment on lines +488 to +489
# @rbs! type t = Prism::ClassVariableWriteNode | Prism::ClassVariableOrWriteNode
# | Prism::InstanceVariableWriteNode | Prism::InstanceVariableOrWriteNode
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be nice to give a better name for such type alias. Please let me know if you have opinions.


# @rbs node: t
# @rbs annotation: Annotations::TypeAssertion?
# @rbs scope: :class | :method
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure this name is the best.

@@ -258,6 +258,9 @@ def visit_def_node(node)
assertion = assertion_annotation(node.rparen_loc || node&.parameters&.location || node.name_loc)

current_decl.members << AST::Members::RubyDef.new(node, associated_comment, current_visibility, current_module_function, assertion)

instance_variables = MethodParser.parse(self, node)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MethodParser? RubyDefParser? DefNodeParser?

if var.type != untyped
case decl
when AST::Declarations::ClassDecl
warn "error: duplicate variable #{var.name} in #{decl.class_name}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use #warn to warn duplications. Is there a better way to do that?

@tk0miya tk0miya force-pushed the support_ivar branch 2 times, most recently from 3bc7900 to f243322 Compare November 22, 2024 21:27
This adds support for instance variables, class variables and class
instance variables.

After this change, RBS::Inline can extract them from assignment for
these variables.

Example:

```ruby
class Example
  def method
    @ivar = 123  #: Integer
    @@cvar = "blah blah"  #: String
  end
end
```

```rbs
class Example
  def method: () -> untyped
  @ivar: Integer
  @@cvar: String
end
```
Comment on lines +47 to +49
@tree: untyped

@source: untyped
Copy link
Contributor Author

Choose a reason for hiding this comment

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

attr_readers for these variables are defined in the base class. Therefore these definitions are bothersome...

@tk0miya tk0miya force-pushed the support_ivar branch 3 times, most recently from c650b86 to 9f6cecd Compare March 6, 2025 11:46
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.

1 participant