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

Adds support for attr_* method references #2848

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

Conversation

tommycrumrine
Copy link

@tommycrumrine tommycrumrine commented Nov 14, 2024

Motivation

Shows references for methods defined with attr_reader, attr_writer, attr_accessor
Closes #2668

Implementation

attr_reader, attr_writer, attr_accessor are just methods, so on call node if we detect one of these methods, and one of the arguments match the target method name, add the reference.

Automated Tests

ya

Manual Tests

Screenshot 2024-11-15 at 1 43 07 PM

@tommycrumrine
Copy link
Author

I have signed the CLA!

@tommycrumrine tommycrumrine marked this pull request as ready for review November 15, 2024 19:43
@tommycrumrine tommycrumrine requested a review from a team as a code owner November 15, 2024 19:43
Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

Just a small change, but this looks great.

def test_matches_attr_writer_with_call_node_argument
refs = find_method_references("foo=", <<~RUBY)
class Bar
attr_reader :foo, bar
Copy link
Member

Choose a reason for hiding this comment

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

Should this be attr_writer or attr_accessor?


sig { params(node: Prism::CallNode).returns(T::Array[String]) }
def unescaped_argument_names(node)
node.arguments.arguments.select { |arg| arg.respond_to?(:unescaped) }.map(&:unescaped)
Copy link
Member

Choose a reason for hiding this comment

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

In the middle of typing, we may find empty arguments, so we have to handle that. Also, we need to account for symbol and string nodes as arguments.

Suggested change
node.arguments.arguments.select { |arg| arg.respond_to?(:unescaped) }.map(&:unescaped)
arguments = node.arguments.arguments
return [] unless arguments
arguments.filter_map do |arg|
case arg
when Prism::StringNode
arg.unescaped
when Prism::SymbolNode
arg.value
end
end

@vinistock vinistock added enhancement New feature or request server This pull request should be included in the server gem's release notes labels Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support attr_* for References
2 participants