-
Notifications
You must be signed in to change notification settings - Fork 638
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?
Conversation
This is a great idea, but I fear the implementation may be over-simplistic, because the user might have defined another variable with the same name that shadows the first, and this would end up replacing both. I'll add some tests and see if that's a problem in practice. |
@nicklockwood I added the option to pass in a comma delimited list of identifiers. e.g. my *.swiftformat file now includes the following line:
|
3ce318c
to
68131f1
Compare
ss
, sSelf
, and strongSelf
with self
for strongifiedSelf rule 5f6b8b2
to
ad8294a
Compare
* Add period after help message * Shorten it a bit more
eb771ee
to
5c604df
Compare
$0 == .operator("=", .infix) | ||
}), formatter.next(.nonSpaceOrCommentOrLinebreak, after: equalIndex) == .identifier("self") else { | ||
return | ||
} |
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 to strongSelf
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:
- Rename the disallowed variable name with self
- Update any references to the disallowed variable name to self
- Fix optionality of existing references to self which are no longer optional
Example for number 3:
{ [weak self] in
guard let strongSelf = self else { return }
self?.doWork()
}
In the above example, we would also need to remove the trailing ?
after self
:
{ [weak self] in
guard let self = self else { return }
self.doWork()
}
(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:
reuse_self_for_strongified_self:
name: Reuse self for strongified self
regex: "(?i) (s|ss|sSelf|strongSelf) = self[\n ,]"
message: "Reuse self as the variable name when strongifying self as per Airbnb Swift style guide"
severity: error
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!
fa385b8
to
a3e985b
Compare
73b9f9a
to
dbe1144
Compare
b7bfa27
to
9a2fb9a
Compare
043a620
to
de82a49
Compare
28bcc60
to
dd989a4
Compare
8eabbce
to
ddb7462
Compare
Related: #521
For the
strongifiedSelf
rule, it would be great if the user can customize an additional list of variable names to be auto-corrected to self (e.g.ss
orstrongSelf
).This PR introduces a new option for the
strongifiedSelf
rule which is a comma-delimited list of identifiers we want to replace with self.