Skip to content

Commit e721b34

Browse files
committed
Converted Declaration to struct
1 parent 89823e4 commit e721b34

File tree

6 files changed

+78
-120
lines changed

6 files changed

+78
-120
lines changed

Sources/SwiftDocCoverage/Coverage.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension String : LocalizedError {
2929
}
3030

3131
public struct Coverage {
32-
let sources: [Source]
32+
let urls: [URL]
3333
let minAccessLevel: AccessLevel
3434
let output: Output
3535

@@ -87,12 +87,11 @@ public struct Coverage {
8787
}()
8888

8989
public init(paths: [String], skipsHiddenFiles: Bool = true, ignoreFilenameRegex: String = "", minAccessLevel: AccessLevel = .public, output: Output = TerminalOutput()) throws {
90-
self.sources = try paths.flatMap {
90+
self.urls = try paths.flatMap {
9191
try Self.files(path: $0, ext: ".swift", skipsHiddenFiles: skipsHiddenFiles, ignoreFilenameRegex: ignoreFilenameRegex)
9292
}
93-
.map { try Source(url: $0) }
9493

95-
guard sources.count > 0 else {
94+
guard urls.count > 0 else {
9695
throw "Swift files not found."
9796
}
9897

@@ -120,26 +119,30 @@ public struct Coverage {
120119

121120
@discardableResult
122121
public func report(_ body: ((SourceReport, TimeInterval) -> Void)? = nil) throws -> CoverageReport {
123-
precondition(sources.count > 0)
122+
precondition(urls.count > 0)
124123

125124
var sourceReports = [SourceReport]()
126125

127-
sources.forEach { source in
126+
try urls.forEach { url in
128127
let time = Date()
129128

129+
let source = try Source(url: url)
130+
130131
guard source.declarations.count > 0 else {
131132
return
132133
}
133134

134-
let declarations = source.declarations.filter { $0.accessLevel.rawValue <= self.minAccessLevel.rawValue }
135+
let declarations = source.declarations.filter {
136+
$0.accessLevel.rawValue <= self.minAccessLevel.rawValue
137+
}
135138

136139
let undocumented: [DeclarationReport] = declarations
137140
.filter {
138141
$0.comments.contains { $0.hasDoc == false }
139142
}
140143
.map { DeclarationReport(line: $0.line, column: $0.column, name: $0.name) }
141144

142-
let sourceReport = SourceReport(path: source.url!.absoluteString, totalCount: source.declarations.count, undocumented: undocumented)
145+
let sourceReport = SourceReport(path: url.absoluteString, totalCount: source.declarations.count, undocumented: undocumented)
143146
sourceReports.append(sourceReport)
144147

145148
body?(sourceReport, -time.timeIntervalSinceNow)

Sources/SwiftDocCoverage/DeclProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import SwiftSyntax
2525

2626

27-
protocol DeclProtocol: SyntaxProtocol {
27+
protocol DeclProtocol: DeclSyntaxProtocol {
2828

2929
// var attributes: AttributeListSyntax
3030
var modifiers: DeclModifierListSyntax { get }

Sources/SwiftDocCoverage/Declaration.swift

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,23 @@ fileprivate struct StringBuilder {
3535
}
3636
}
3737

38-
public class Declaration {
39-
private let decl: DeclProtocol
40-
private let context: [DeclProtocol]
41-
38+
public struct Declaration {
39+
public let comments: [Comment]
40+
public let accessLevel: AccessLevel
41+
public var keyword: Keyword
42+
public let name: String
4243
public let line: Int
4344
public let column: Int
44-
45-
public var keyword: Keyword { decl.keyword }
46-
47-
public private(set) lazy var accessLevel: AccessLevel = { decl.accessLevel }()
48-
public private(set) lazy var comments: [Comment] = { decl.comments }()
49-
public private(set) lazy var name: String = { buildName() }()
5045

5146
@StringBuilder
52-
private func buildName() -> String {
47+
private static func buildName(decl: DeclProtocol, path: String?) -> String {
5348
if decl.keyword != .`init`, decl.keyword != .subscript {
5449
decl.keyword.rawValue
5550
" "
5651
}
5752

58-
if context.count > 0 {
59-
context.map { $0.name.trimmedDescription }.joined(separator: ".")
53+
if let path = path {
54+
path
6055
"."
6156
}
6257

@@ -71,9 +66,11 @@ public class Declaration {
7166
}
7267
}
7368

74-
init(decl: DeclProtocol, context: [DeclProtocol], location: SourceLocation) {
75-
self.decl = decl
76-
self.context = context
69+
init(decl: DeclProtocol, path: String?, location: SourceLocation) {
70+
self.comments = decl.comments
71+
self.accessLevel = decl.accessLevel
72+
self.keyword = decl.keyword
73+
self.name = Self.buildName(decl: decl, path: path)
7774
self.line = location.line
7875
self.column = location.column
7976
}

Sources/SwiftDocCoverage/Lazy.swift

Lines changed: 0 additions & 42 deletions
This file was deleted.

Sources/SwiftDocCoverage/Source.swift

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,24 @@
2323

2424
import Foundation
2525
import SwiftSyntax
26-
import SwiftParser
2726

2827

2928
public struct Source {
3029
public let url: URL?
31-
public let source: String
30+
public let declarations: [Declaration]
3231

33-
// https://oleb.net/blog/2015/12/lazy-properties-in-structs-swift/
34-
public var declarations: [Declaration] {
35-
Lazy.var {
36-
let sourceFile = Parser.parse(source: source)
37-
let converter = SourceLocationConverter(fileName: "", tree: sourceFile)
38-
let visitor = Visitor(sourceFile: sourceFile, converter: converter)
39-
return visitor.declarations
40-
}
32+
private init(url: URL?, source: String) {
33+
self.url = url
34+
let visitor = Visitor(source: source)
35+
self.declarations = visitor.declarations
4136
}
42-
37+
4338
public init(source: String) {
44-
self.url = nil
45-
self.source = source
39+
self.init(url: nil, source: source)
4640
}
4741

4842
public init(url: URL) throws {
49-
self.url = url
50-
self.source = try String(contentsOf: url)
43+
let source = try String(contentsOf: url)
44+
self.init(url: url, source: source)
5145
}
5246
}

Sources/SwiftDocCoverage/Visitor.swift

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,30 @@
2222
// THE SOFTWARE.
2323

2424
import SwiftSyntax
25+
import SwiftParser
2526

2627

2728
class Visitor: SyntaxVisitor {
28-
var declarations = [Declaration]()
29-
var context = [DeclProtocol]()
30-
let converter: SourceLocationConverter
29+
private var context = [DeclProtocol]()
30+
private let converter: SourceLocationConverter
3131

32-
init(sourceFile: SourceFileSyntax, converter: SourceLocationConverter) {
33-
self.converter = converter
32+
private(set) var declarations = [Declaration]()
33+
34+
init(source: String) {
35+
let sourceFile = Parser.parse(source: source)
36+
self.converter = SourceLocationConverter(fileName: "", tree: sourceFile)
3437
super.init(viewMode: .sourceAccurate)
3538
walk(sourceFile)
3639
}
3740

3841
func append(decl: DeclProtocol) {
3942
let startLocation = decl.startLocation(converter: converter, afterLeadingTrivia: true)
40-
let declaration = Declaration(decl: decl, context: context, location: startLocation)
43+
44+
let path: String? = context.count > 0
45+
? context.map { $0.name.trimmedDescription }.joined(separator: ".")
46+
: nil
47+
48+
let declaration = Declaration(decl: decl, path: path, location: startLocation)
4149
declarations.append(declaration)
4250
}
4351

@@ -59,30 +67,55 @@ class Visitor: SyntaxVisitor {
5967
return .visitChildren
6068
}
6169

70+
override func visitPost(_ node: ClassDeclSyntax) {
71+
let decl = context.popLast()
72+
assert(decl is ClassDeclSyntax)
73+
}
74+
6275
override func visit(_ node: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
6376
append(decl: node)
6477
context.append(node)
6578
return .visitChildren
6679
}
6780

81+
override func visitPost(_ node: ActorDeclSyntax) {
82+
let decl = context.popLast()
83+
assert(decl is ActorDeclSyntax)
84+
}
85+
6886
override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
6987
append(decl: node)
7088
context.append(node)
7189
return .visitChildren
7290
}
7391

92+
override func visitPost(_ node: StructDeclSyntax) {
93+
let decl = context.popLast()
94+
assert(decl is StructDeclSyntax)
95+
}
96+
7497
override func visit(_ node: ProtocolDeclSyntax) -> SyntaxVisitorContinueKind {
7598
append(decl: node)
7699
context.append(node)
77100
return .visitChildren
78101
}
79102

103+
override func visitPost(_ node: ProtocolDeclSyntax) {
104+
let decl = context.popLast()
105+
assert(decl is ProtocolDeclSyntax)
106+
}
107+
80108
override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind {
81109
append(decl: node)
82110
context.append(node)
83111
return .visitChildren
84112
}
85113

114+
override func visitPost(_ node: ExtensionDeclSyntax) {
115+
let decl = context.popLast()
116+
assert(decl is ExtensionDeclSyntax)
117+
}
118+
86119
override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
87120
append(decl: node)
88121
return .skipChildren
@@ -109,6 +142,11 @@ class Visitor: SyntaxVisitor {
109142
return .visitChildren
110143
}
111144

145+
override func visitPost(_ node: EnumDeclSyntax) {
146+
let decl = context.popLast()
147+
assert(decl is EnumDeclSyntax)
148+
}
149+
112150
override func visit(_ node: EnumCaseDeclSyntax) -> SyntaxVisitorContinueKind {
113151
append(decl: node)
114152
return .skipChildren
@@ -123,36 +161,4 @@ class Visitor: SyntaxVisitor {
123161
append(decl: node)
124162
return .skipChildren
125163
}
126-
127-
// MARK: -
128-
129-
override func visitPost(_ node: ClassDeclSyntax) {
130-
let decl = context.popLast()
131-
assert(decl is ClassDeclSyntax)
132-
}
133-
134-
override func visitPost(_ node: ActorDeclSyntax) {
135-
let decl = context.popLast()
136-
assert(decl is ActorDeclSyntax)
137-
}
138-
139-
override func visitPost(_ node: EnumDeclSyntax) {
140-
let decl = context.popLast()
141-
assert(decl is EnumDeclSyntax)
142-
}
143-
144-
override func visitPost(_ node: ExtensionDeclSyntax) {
145-
let decl = context.popLast()
146-
assert(decl is ExtensionDeclSyntax)
147-
}
148-
149-
override func visitPost(_ node: ProtocolDeclSyntax) {
150-
let decl = context.popLast()
151-
assert(decl is ProtocolDeclSyntax)
152-
}
153-
154-
override func visitPost(_ node: StructDeclSyntax) {
155-
let decl = context.popLast()
156-
assert(decl is StructDeclSyntax)
157-
}
158164
}

0 commit comments

Comments
 (0)