Skip to content

Support instance variables, class variables and class instance variables#149

Open
tk0miya wants to merge 3 commits intosoutaro:mainfrom
tk0miya:support_ivar
Open

Support instance variables, class variables and class instance variables#149
tk0miya wants to merge 3 commits intosoutaro:mainfrom
tk0miya:support_ivar

Conversation

@tk0miya
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.


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

instance_variables = MethodParser.parse(self, node)
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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