SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Emulate `protected` visiblity modifier on fields/functions

Open ursusursus opened this issue 2 years ago • 1 comments

Swift has no protected modifier. It still is handly. Currently we just make it public and just agree to not use certain functions/fields.

This is obviously error prone. Best solution would be some sort of annotation, like this

class Foo {
   @Protected
   protected bar() {
      //
   }
}

and then linter would enforce not calling this outside of the class

Would that be possible?

ursusursus avatar Jul 27 '22 09:07 ursusursus

In principle, it is possible to implement something like you propose as an analyzer rule which requires the full set of compile commands and takes considerably more time to run than the other basic lint rules. A rule working solely on the AST is not sufficient since it would not know which properties a called method has. This can only be figured out in the context of the whole project.

Having said that, I don't think this should be a generally available rule. Your way of protecting methods from external access seems to be very specific and others might have other ways to ensure that. However, SwiftLint just recently gained support for native custom rules. They might be a reasonable possibility for you to add your rule to a custom build.

SimplyDanny avatar Aug 27 '22 13:08 SimplyDanny