Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Allow user to customize additional variable names to be replaced by self with the strongifiedSelf rule #523
base: main
Are you sure you want to change the base?
Allow user to customize additional variable names to be replaced by self with the strongifiedSelf rule #523
Changes from 9 commits
4826cfe
a0c1360
7e3cb26
e12614b
28470e2
576a461
a99cba1
e3f5178
5364008
d42fcca
3fe0098
225dc96
68131f1
ad8294a
54da3b2
099412d
d6a6258
a53b488
5c604df
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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 don't think this will work. It only replaces the identifier if it appears in an expression like
strongSelf = self
, but it won't replace any references tostrongSelf
in the subsequent code.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.
@nicklockwood, yeah, I also discussed this with my coworkers to see if autocorrect was the right thing to do here. there's actually 3 things that need to be autocorrected:
Example for number 3:
In the above example, we would also need to remove the trailing
?
afterself
:(2) and (3) both feel really complicated for autocorrection w/o full understanding of the code graph and would be probably better off with human intervention. However, we still found it to be useful for autocorrect to complete step (1) despite the fact it might leave the code uncompilable.
thoughts?
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.
is similar in complexity to what I already do for removing implicit self. It's certainly complicated because it requires an understanding of scope and tracking variable declarations and shadowing, but it's a local problem (i.e. it doesn't require knowledge of externally-defined symbols and their types) so it can be solved.
is probably too difficult since modifications from optional to non-optional may be nontrivial, but I also think it's a bad idea generally because the developer may have a good reason for referencing
self?
rather thanstrongSelf
and a formatter shouldn't second-guess that. However it wouldn't be hard to detect the user ofself
and not replacestrongSelf
in those cases.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.
Just doing 1) and leaving code non-compilable is not acceptable IMO. Now that SwiftFormat has fairly decent linting capability, it might make sense to introduce the concept of "lint-only" rules for cases like this, where it would warn but not actually replace the symbol.
But TBH, it would probably make more sense to make it a SwiftLint rule instead in that case.
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, we have a simple SwiftLint custom rule for detecting this for now:
If you have an idea of how to autocorrect for (2), that would be great! I can take a look at
removing implicit self
and see if I can mimic that behavior, but if you have pointers or have the bandwidth to add that, even better!