Skip to content

Add a rule to disallow public extensions while allowing private and fileprivate ones #715

@Matejkob

Description

@Matejkob

Currently, the NoAccessLevelOnExtensionDeclaration rule removes access level attributes from all extensions, regardless of the specific access level. However, there is a valid use case for allowing private and fileprivate extensions while disallowing public extensions.

When an extension is marked as private or fileprivate, it provides a clear indication that the extension and its members are not intended to be accessed from outside the current file or declaration scope. This can help prevent unintended leakage of implementation details and maintain encapsulation.

On the other hand, public extensions can be a source of unintended behavior and may not always align with the intended visibility of the extension members.

func testDisallowPublicExtension() {
  assertFormatting(
    NewRule???.self,
    input: """
        public extension Foo {
            var bar: String { "" }
        }
        
        private extension Foo {
            var baz: String { "" }
        }
        """,
    expected: """
        extension Foo {
            public var bar: String { "" }
        }
        
        private extension Foo {
            var baz: String { "" }
        }
        """,
    findings: [
      // TODO: Add findings
    ]
  )
}

Originated from: swiftlang/swift-syntax#2602 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions