-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 'implicit_self' rule #3140
Add 'implicit_self' rule #3140
Conversation
I love how this is implemented without using Having that said, I wonder if we can unify these two rules into a single one and just make it configurable with a
The implementation would be based on your PR of course, to get the performance boost from dropping What do you think @msbm ? |
Yeah I think that makes total sense, given |
@msbm Did you get a chance to work on this? If not, I might find some time on the weekend to give it a shot. |
Yeah I've been (slowly, sorry) working on it. I think I'm pretty close so I'll set aside some time this weekend to finish it up and update the PR |
@svenmuennich After working on this more this weekend I'm realizing i don't think it'll actually be possible to simplify ExplicitSelf using this logic. For ImplicitSelf I'm looking at every identifier and asserting that it isn't preceded with |
Hi, any news on this? Thanks! |
Sorry @msbm, I somehow missed the notification when you commented again 😞
This is indeed correct. I missed that when initially looking over this PR. So we probably have to stick to having two methods Thank you for putting in the time trying to work this out though 👏 |
* Added 'implicit_self' opt-in rule. | ||
[msbm](https://github.com/msbm) | ||
[#59](https://github.com/realm/SwiftLint/issues/59) |
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.
* Added 'implicit_self' opt-in rule. | |
[msbm](https://github.com/msbm) | |
[#59](https://github.com/realm/SwiftLint/issues/59) | |
* Added 'implicit_self' opt-in rule. | |
[msbm](https://github.com/msbm) | |
[#59](https://github.com/realm/SwiftLint/issues/59) |
return nil | ||
} | ||
|
||
let selfOffset = identifier.offset - 5 |
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.
Does this work when self
is placed on a different line than the identifier
? E.g.:
self
.someMethodCall()
We should probably add a test case for this either way.
if configuration.initSelfUsage == .beforeInitCall && | ||
structure.isInit && |
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.
These two conditions are constant for all elements of structure.identifiers
(i.e. identifier
) and both are reused below for determining subViolations
. It might be worth extracting this into a variable.
let kind: String | ||
let offset: ByteCount | ||
|
||
init?(token: SwiftLintSyntaxToken, file: SwiftLintFile) { |
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.
Why is this an optional initializer? Should we maybe return nil
if name
and/or kind
would otherwise be initialized with a fallback value of ""
?
} | ||
|
||
private var enclosedParameters: [Identifier] { | ||
return substructure.flatMap { structure -> [Identifier] in |
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.
A compactMap
seems to fit better here.
I don't think it's possible to write this reliably without access to the post-typechecked AST (aka The SwiftLint codebase itself uses an implicit self style, so I tried turning this on for this project and got ~80 violations, most of which are false positives. |
This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions! |
Are there any plans to implement this lint rule? |
#59