-
Notifications
You must be signed in to change notification settings - Fork 119
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
Improve constant lookup in SourceFinder #871
Conversation
6f71459
to
6c10788
Compare
elsif parts == [''] # ::ConstName | ||
Object | ||
else # ConstPath::ConstName | ||
eval_receiver_or_owner(parts.join('::')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, the result of this should be the same as eval_receiver_or_owner(signature)
? If that's the case, maybe we can store that and use it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If signature == 'A::B::C'
, parts.join('::')
will be 'A::B'
We need to evaluate A::B
in current binding and get base = FooBar::A::B
(FooBar part depends on binding's Module.nesting
)
Now we can find source by calling base.const_source_location('C')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks for explaining 👍
lib/irb/source_finder.rb
Outdated
base = @irb_context.workspace.binding.receiver.yield_self { |r| r.is_a?(Module) ? r : Object } | ||
file, line = base.const_source_location(signature) | ||
*parts, name = signature.split('::', -1) | ||
base = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think having these parentheses introduces a new style that didn't exist in this repo before 🤔 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to
a =
if cond
else
end
style
6c10788
to
7a4714b
Compare
Improve show_source of constant to find const owner more precise.
Constant lookup does not depend on
self
, but onModule.nesting
In this test code I added,
X
,Y
,Z
,Array
are all visible with value == 1Module.nesting
is[A::B, A]
and lookup order seemsA::B → A → A::B.ancestors → A.ancestors → TOPLEVEL(Object)