-
Notifications
You must be signed in to change notification settings - Fork 26
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
Refactor & Optimize EmptyLineAfterSig
#185
Conversation
e3a2905
to
677f62b
Compare
0c12e66
to
4752fc2
Compare
677f62b
to
b47599d
Compare
dab8703
to
535137f
Compare
MSG = "Extra empty line or comment detected" | ||
|
||
# @!method signable_method_definition?(node) | ||
def_node_matcher :signable_method_definition?, <<~PATTERN |
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 think we should also enable this cop in the RBI config: https://github.com/Shopify/rubocop-sorbet/blob/main/config/rbi.yml#L195.
Which would mean allowing multiple signatures on the same def, so the next sibling could also be a sig.
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.
Yeah, agreed. Instead of next_sibling
, we should still be looking for the next method/attr_ definition in a more performant way.
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.
The next_sibling
approach already handled the case where there was an empty line between the last signature and method definition in a multiple signature case, but it didn't handle the case where there was an empty line between multiple signatures.
However, that was an easy change: the SignatureHelp
module (replacing the SignatureCop
class in #183) includes a signature?
node pattern, which we can use to update the node pattern we use to check if the next_sibling
is relevant to also consider signatures relevant.
In the interest of keeping this PR focused on the refactor and optimization, I've made those two changes in #188 instead:
- Teaches
EmptyLineAfterSig
to detect empty lines between multiple sigs - Enables
EmptyLineAfterSig
inrbi.yml
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.
Ok, sounds good to me!
a54aa3f
to
535137f
Compare
2643055
to
f1502f0
Compare
These are unlikely, but we should handle them without errors.
The previous implementation would traverse the tokens in the file each time it would analyze a `sig` until it found the corresponding method definition. This would take additional time the further we got into a file, as we would have to re-traverse all tokens so far. This new implementation simply looks for the next node after the `sig`, confirms it is the method definition, and calculates offsets for the lines in between.
535137f
to
4f74f27
Compare
The previous implementation would traverse the tokens in the file each time it would analyze a
sig
until it found the corresponding method definition. This would take additional time the further we got into a file, as we would have to re-traverse all tokens so far.This new implementation simply looks for the next node after the
sig
, confirms it is the method definition, and calculates offsets for the lines in between.This also migrates from this class from the deprecated
Cop
parent class toBase
.When run across some of the largest files in the Shopify monolith, we see a > 2,000x improvement.1
Before Merging
Footnotes
No fancy benchmarking as the difference is large enough. Just ran
rubocop --profile
a couple times toggling between branches and grabbing results from Speedscope. ↩