@@ -2,8 +2,7 @@ import SwiftSyntax
22
33@SwiftSyntaxRule ( optIn: true )
44struct OneDeclarationPerFileRule : Rule {
5- var configuration = SeverityConfiguration < Self > ( . warning)
6-
5+ var configuration = OneDeclarationPerFileConfiguration ( )
76 static let description = RuleDescription (
87 identifier: " one_declaration_per_file " ,
98 name: " One Declaration per File " ,
@@ -22,6 +21,18 @@ struct OneDeclarationPerFileRule: Rule {
2221 struct N {}
2322 }
2423 """ ) ,
24+ Example ( """
25+ enum Foo {
26+ }
27+ struct Bar {
28+ }
29+ """ ,
30+ configuration: [ " allowed_types " : [ " enum " , " struct " ] ] ) ,
31+ Example ( """
32+ struct Foo {}
33+ struct Bar {}
34+ """ ,
35+ configuration: [ " allowed_types " : [ " struct " ] ] ) ,
2536 ] ,
2637 triggeringExamples: [
2738 Example ( """
@@ -36,14 +47,24 @@ struct OneDeclarationPerFileRule: Rule {
3647 struct Foo {}
3748 ↓struct Bar {}
3849 """ ) ,
50+ Example ( """
51+ struct Foo {}
52+ ↓enum Bar {}
53+ """ ,
54+ configuration: [ " allowed_types " : [ " protocol " ] ] ) ,
3955 ]
4056 )
4157}
4258
4359private extension OneDeclarationPerFileRule {
4460 final class Visitor : ViolationsSyntaxVisitor < ConfigurationType > {
61+ private let allowedTypes : Set < String >
4562 private var declarationVisited = false
4663 override var skippableDeclarations : [ any DeclSyntaxProtocol . Type ] { . all }
64+ override init ( configuration: OneDeclarationPerFileConfiguration , file: SwiftLintFile ) {
65+ allowedTypes = Set ( configuration. enabledTypes. map ( \. rawValue) )
66+ super. init ( configuration: configuration, file: file)
67+ }
4768
4869 override func visitPost( _ node: ActorDeclSyntax ) {
4970 appendViolationIfNeeded ( node: node. actorKeyword)
@@ -66,10 +87,10 @@ private extension OneDeclarationPerFileRule {
6687 }
6788
6889 func appendViolationIfNeeded( node: TokenSyntax ) {
69- if declarationVisited {
90+ defer { declarationVisited = true }
91+ if declarationVisited && !allowedTypes. contains ( node. text) {
7092 violations. append ( node. positionAfterSkippingLeadingTrivia)
7193 }
72- declarationVisited = true
7394 }
7495 }
7596}
0 commit comments