Skip to content

Commit 8226a15

Browse files
committed
Report json
1 parent e885084 commit 8226a15

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

Sources/SwiftSource/SwiftAccessLevel.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-
public enum SwiftAccessLevel: Int, Decodable {
27+
public enum SwiftAccessLevel: Int, Codable {
2828
case `open`
2929
case `public`
3030
case `internal`

Sources/SwiftSource/SwiftComment.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-
public struct SwiftComment: Decodable {
27+
public struct SwiftComment: Codable {
2828
public let text: String
2929
public var isDoc: Bool
3030

Sources/SwiftSource/SwiftDeclaration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fileprivate struct StringBuilder {
4747
}
4848
}
4949

50-
public struct SwiftDeclaration: Decodable {
50+
public struct SwiftDeclaration: Codable {
5151
public let comments: [SwiftComment]
5252
public let accessLevel: SwiftAccessLevel
5353
public var keyword: SwiftKeyword

Sources/SwiftSource/SwiftKeyword.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-
public enum SwiftKeyword: String, Decodable {
27+
public enum SwiftKeyword: String, Codable {
2828
case actor
2929
case `associatedtype`
3030
case `case`

Sources/SwiftSource/SwiftSource.swift

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

2727

28-
public struct SwiftSource: Decodable {
28+
public struct SwiftSource: Codable {
2929
public let url: URL?
3030
public let declarations: [SwiftDeclaration]
3131

Sources/swift-doc-coverage/main.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct SwiftDocCoverage: ParsableCommand {
116116
}
117117

118118
let totalTime = Date()
119-
var i = 0
119+
var index = 0
120120

121121
// Sources
122122
sources = try urls.map { url in
@@ -127,10 +127,10 @@ struct SwiftDocCoverage: ParsableCommand {
127127
if report != .json {
128128
let declarations = source.declarations(level: minimumAccessLevel.accessLevel)
129129
if declarations.count > 0 {
130-
i += 1
130+
index += 1
131131
let filePath = url.absoluteString
132132
if report == .coverage {
133-
Self.coverage(i: i, time: sourceTime, filePath: filePath, declarations: declarations, out: out)
133+
Self.coverage(index: index, time: sourceTime, filePath: filePath, declarations: declarations, out: out)
134134
}
135135
else if report == .warnings {
136136
Self.warnings(filePath: filePath, declarations: declarations, out: out)
@@ -156,14 +156,13 @@ struct SwiftDocCoverage: ParsableCommand {
156156
out?.write("\nTotal: \(coverage)% [\(documentedCount)/\(totalCount)] (\(Self.string(from: -totalTime.timeIntervalSinceNow)))")
157157
}
158158
else if report == .json {
159-
print(sources)
160-
// let encoder = JSONEncoder()
161-
// encoder.outputFormatting = .prettyPrinted
162-
//
163-
// let data = try encoder.encode(sources)
164-
// let json = String(data: data, encoding: .utf8)!
165-
//
166-
// output.write(json)
159+
let encoder = JSONEncoder()
160+
encoder.outputFormatting = .prettyPrinted
161+
162+
let data = try encoder.encode(sources)
163+
let json = String(data: data, encoding: .utf8)!
164+
165+
out?.write(json)
167166
}
168167
}
169168

@@ -247,7 +246,7 @@ struct SwiftDocCoverage: ParsableCommand {
247246
return time
248247
}
249248

250-
static func coverage(i: Int, time: Date, filePath: String, declarations: [SwiftDeclaration], out: Output?) {
249+
static func coverage(index: Int, time: Date, filePath: String, declarations: [SwiftDeclaration], out: Output?) {
251250
assert(declarations.count > 0)
252251

253252
let undocumented = declarations.filter { $0.isDocumented == false }
@@ -256,7 +255,7 @@ struct SwiftDocCoverage: ParsableCommand {
256255
let documentedCount = totalCount - undocumented.count
257256
let coverage = documentedCount * 100 / totalCount
258257

259-
out?.write("\(i)) \(filePath): \(coverage)% [\(documentedCount)/\(totalCount)] (\(string(from: -time.timeIntervalSinceNow)))")
258+
out?.write("\(index)) \(filePath): \(coverage)% [\(documentedCount)/\(totalCount)] (\(string(from: -time.timeIntervalSinceNow)))")
260259

261260
if undocumented.count > 0 {
262261
let fileName = NSString(string: filePath).lastPathComponent

Tests/SwiftDocCoverageTests/SwiftDocCoverageTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,21 @@ final class SwiftDocCoverageTests: XCTestCase {
491491
XCTAssert(cmd.sources[0].declarations(level: .private).count == 4)
492492
}
493493

494+
func test_warnings() throws {
495+
let output = Output()
496+
_ = try SwiftDocCoverage.run(output: output, rectFileURL.path, "--report", "warnings")
497+
XCTAssert(output.buffer.contains("Rect.swift:9:3: warning: No documentation for 'var Rect.size'.") == true)
498+
XCTAssert(output.buffer.contains("Rect.swift:12:3: warning: No documentation for 'var Rect.center'.") == true)
499+
}
500+
501+
func test_json() throws {
502+
let output = Output()
503+
_ = try SwiftDocCoverage.run(output: output, rectFileURL.path, "--report", "json")
504+
let sources = try JSONDecoder().decode([SwiftSource].self, from: output.buffer.data(using: .utf8)!)
505+
XCTAssert(sources.count == 1)
506+
XCTAssert(sources[0].declarations.count == 4)
507+
}
508+
494509
func test_file() throws {
495510
let cmd = try SwiftDocCoverage.run(rectFileURL.path)
496511
XCTAssert(cmd.sources.count == 1)

0 commit comments

Comments
 (0)