SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Rule request: explicit access control level

Open jdmarshall90 opened this issue 9 years ago • 10 comments

See title

I know there is a rule for top level ACL declarations, but one for all declarations would be nice. This would probably be opt-in, as it would mostly be useful by frameworks and not applications.

jdmarshall90 avatar Jul 01 '17 02:07 jdmarshall90

Do you have any examples on how explicit non top level declarations would benefit from this requested rule? The use case from the top level ACL rule is frameworks.

masters3d avatar Jul 10 '17 22:07 masters3d

I don't have any specific examples; this is just more of a personal preference that I like to follow when developing frameworks. It gives more context to anything that you're looking at within the library.

jdmarshall90 avatar Jul 13 '17 02:07 jdmarshall90

+1 because I'm a fan of this opinion:

Because default access levels vary across languages, and many people program in more than one language. It's easy to become confused, either as the author or as someone reading the code later, thus explicit is nicer to deal with than implicit.

mshibanami avatar Jul 26 '17 09:07 mshibanami

@mshibanami but adding them to the top level declarations should be enough to cover that case

marcelofabri avatar Jul 26 '17 09:07 marcelofabri

@marcelofabri

internal class Hello {
    let v = "world"
}

In this case, I think it would be nice if SwiftLint warns me about v doesn't have a explicit access modifier. But it seems that explicit_top_level_acl doesn't work so.

I'm sorry if I misunderstood.

mshibanami avatar Jul 26 '17 10:07 mshibanami

That rule won't trigger a violation because it only validates top level declarations (as Hello in your example).

I actually got your point now. Sorry for adding noise to the conversation.

marcelofabri avatar Jul 26 '17 10:07 marcelofabri

+1 to this. I've seen many Swift codebases that neglect using ACLs in the internals of a class/enum/struct. I'm unsure why this is so pervasive specifically on iOS, but I'd prefer to ensure that my team does not neglect something as crucial as class encapsulation.

My concern on class encapsulation is due to Swift defaulting to internal, and as opposed to Java where that's package protected, internal means your whole target can see that field.

In summary, adding this rule would improve the health of any codebase that opts in, so I'm a bit surprised it's not already baked in.

joseprl89 avatar Nov 25 '17 11:11 joseprl89

@joseprl89 since you appear to feel strongly about this, perhaps you're motivated enough to contribute this rule to SwiftLint yourself? I'm happy to provide guidance if you get stuck.

jpsim avatar Nov 29 '17 22:11 jpsim

@jpsim I had some time that I could chip into it, please review #1966 and let me know if I missed anything 👍

joseprl89 avatar Dec 02 '17 20:12 joseprl89

Done in #1966.

jpsim avatar Dec 22 '17 17:12 jpsim