Skip to content

Conversation

@tommycrumrine
Copy link

@tommycrumrine tommycrumrine commented Aug 7, 2025

Motivation

Shows references for methods defined with attr_reader, attr_writer, attr_accessor

Re-opening stale PR #2848. Sorry this fell off my radar!

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.

Additionally, I have ensured it works with empty and string arguments as well as added tests for these cases.

Automated Tests

Yes!

Manual Tests

Screenshot 2024-11-15 at 1 43 07 PM

@graphite-app
Copy link

graphite-app bot commented Aug 7, 2025

How to use the Graphite Merge Queue

Add the label graphite-merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@tommycrumrine tommycrumrine marked this pull request as ready for review August 7, 2025 01:48
@tommycrumrine tommycrumrine requested a review from a team as a code owner August 7, 2025 01:48
@tommycrumrine tommycrumrine changed the title Add attr method references Adds support for attr_* method references Aug 7, 2025
Copy link
Contributor

@egiurleo egiurleo left a comment

Choose a reason for hiding this comment

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

Thanks for re-opening your PR and apologies for the slow response on our end! I just left two quick questions. I'll also re-run CI for you to make sure the tests look good.

if @target.is_a?(MethodTarget) && (name = node.name.to_s) == @target.method_name
@references << Reference.new(
name,
node.message_loc, #: as !nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Does type checking pass without this #: as !nil?

Copy link
Author

Choose a reason for hiding this comment

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

Opps, yep something is definitely off here, will look into it. I'm a little new to the type checking 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

All good, let me know if you need any support on it.

Copy link
Member

Choose a reason for hiding this comment

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

I think we do need to cast it because message_loc can be nil. I checked out the branch and type checking is failing without the narrows.

def unescaped_argument_names(node)
return [] if node.arguments.nil?

node.arguments.arguments.select { |arg| arg.respond_to?(:unescaped) }.map(&:unescaped)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can node.arguments.arguments be nil?

@egiurleo egiurleo added enhancement New feature or request server This pull request should be included in the server gem's release notes labels Sep 30, 2025
@egiurleo egiurleo self-assigned this Oct 31, 2025
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.

This is working well for when we ask for references on a call to an attribute, but when trying to find references on the attribute definition itself, we are accidentally searching for the attr_reader call instead of the symbol/string names.

class Foo
  attr_reader :bar # here we search for `attr_reader` instead of bar

  def something
    bar # here it works well
  end
end

We need to adjust the target in references.

Basically, we need to:

  1. Start accepting SymbolNode as a possible target
  2. Check if the node_context.call_node is an attribute related call when we get a SymbolNode back
  3. Construct the target object using the symbol value instead of the call node's name

def unescaped_argument_names(node)
return [] if node.arguments&.arguments.nil?

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.

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

if @target.is_a?(MethodTarget) && (name = node.name.to_s) == @target.method_name
@references << Reference.new(
name,
node.message_loc, #: as !nil
Copy link
Member

Choose a reason for hiding this comment

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

I think we do need to cast it because message_loc can be nil. I checked out the branch and type checking is failing without the narrows.

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

3 participants