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

Add scope to list of symbols #333

Merged
merged 5 commits into from
Apr 18, 2024
Merged

Conversation

natematykiewicz
Copy link
Contributor

Fixes #311

@natematykiewicz
Copy link
Contributor Author

I have signed the CLA!

Comment on lines 158 to 184
test "correctly handles scope" do
response = generate_document_symbols_for_source(<<~RUBY)
class FooModel < ApplicationRecord
scope :foo, ->{ where(a: 1).order(:b) }
end
RUBY

assert_equal(1, response.size)
assert_equal("FooModel", response[0].name)
assert_equal(2, response[0].children.size)
assert_equal("scope :foo", response[0].children[0].name)
assert_equal("scope <anonymous>", response[0].children[1].name)
end

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 wasn't sure if you wanted this test. I see that a lot of supported methods are all untested and just fall under the "correctly handles validate method with all argument types" test basically. So maybe this test isn't necessary.

@natematykiewicz
Copy link
Contributor Author

natematykiewicz commented Apr 15, 2024

I just rebased this to resolve the merge conflict

@andyw8
Copy link
Contributor

andyw8 commented Apr 15, 2024

I should have suggested this in #332 too, but can we add an example of this to the dummy app to allow for easy manual testing?

@egiurleo egiurleo removed their request for review April 15, 2024 20:07
@natematykiewicz
Copy link
Contributor Author

Done! 0630ff5

@andyw8
Copy link
Contributor

andyw8 commented Apr 16, 2024

We need to change the behaviour a little:

Screenshot 2024-04-16 at 11 40 58 AM

For before_create, it's expected there will be two entries, since you can give it a list of callbacks. It would be the equivalent of writing:

before_create :foo
before_create -> () {}

But scope is different, the first argument is the name and second argument is the relation to be applied, so we should only see one entry in the explorer, i.e. scope :adult.

@natematykiewicz
Copy link
Contributor Author

@andyw8 Ahh, right. I haven't figured out how to run this locally yet, but the test output seems to do what you're looking for now.
510342c

@andyw8
Copy link
Contributor

andyw8 commented Apr 16, 2024

This might not be a blocker, but when testing I realised that we will be unintentionally listing scope entries in routes.rb.

@andyw8
Copy link
Contributor

andyw8 commented Apr 16, 2024

@vinistock @st0012 we could try make this only detect scope for ActiveRecord instances, but then we'd also have deal with cases such as scopes being defined in a concern. 🤔

@natematykiewicz
Copy link
Contributor Author

Is there a way to scope all of these to the app/models folder? Right now you’re kind of assuming these are ActiveRecord methods right?

Gems like ActiveModelSerizers define belongs_to and has_many methods for you to use. I assume this code would add symbols for them. Is that a bug or is that a feature? Idk

@vinistock
Copy link
Member

I don't think it's a major issue to include the scope in routes. But one easy way to fix it is to remember if we're inside a class or not, which wouldn't happen in the route definitions.

@andyw8
Copy link
Contributor

andyw8 commented Apr 16, 2024

@natematykiewicz do you want to try that? It would a similar what we do here, but allowing for any class.

@natematykiewicz
Copy link
Contributor Author

Yup! I'll give it a go.

@natematykiewicz
Copy link
Contributor Author

natematykiewicz commented Apr 16, 2024

How's this look? ffd73e7

This is my first time using Sorbet. I think I set up that union type right.

I also struggled to figure out how Prism knows to call on_class_node_enter. I couldn't find it in this documentation.

I basically guessed that on_module_node_enter and on_module_node_leave exist based on the fact that Prism::ModuleNode is listed in the documentation.

Without the module ones, the concern's symbols started failing to get indexed.

I wasn't sure if you wanted this class/module check only for scope, or for all of these ActiveRecord methods. I've currently applied it to everything. If you want me to only have that check for scope, I can move that check further down.

@@ -37,6 +45,8 @@ def on_call_node_enter(node)
)
end

return if @namespace_stack.empty?
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) we could move this to the first line of the method, since tests would always be defined within a module or class.

Copy link
Contributor Author

@natematykiewicz natematykiewicz Apr 17, 2024

Choose a reason for hiding this comment

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

Good call! ba61090

I forgot ActiveSupport::TestCase uses classes. I'm so used to RSpec.describe do end.

@andyw8 andyw8 requested a review from st0012 April 18, 2024 12:53
@st0012 st0012 added the enhancement New feature or request label Apr 18, 2024
@andyw8 andyw8 merged commit 5998de8 into Shopify:main Apr 18, 2024
28 checks passed
@andyw8
Copy link
Contributor

andyw8 commented Apr 18, 2024

@natematykiewicz thanks!

@natematykiewicz natematykiewicz deleted the nate/scope_symbol branch April 18, 2024 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add document symbol support for scope
4 participants