Skip to content

Commit e134137

Browse files
committed
Fix: ignore regex, access level for enum cases
1 parent bb7c0b0 commit e134137

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

Sources/SwiftSource/SwiftDeclaration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public struct SwiftDeclaration: Codable {
8282
}
8383
}
8484

85-
init(decl: DeclProtocol, path: String?, location: SourceLocation) {
85+
init(decl: DeclProtocol, accessLevel: SwiftAccessLevel?, path: String?, location: SourceLocation) {
8686
self.comments = decl.comments
87-
self.accessLevel = decl.accessLevel
87+
self.accessLevel = accessLevel ?? decl.accessLevel
8888
self.keyword = decl.keyword
8989
self.name = Self.buildName(decl: decl, path: path).removingNewLines()
9090
self.line = location.line

Sources/SwiftSource/Visitor.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ class Visitor: SyntaxVisitor {
3737
walk(sourceFile)
3838
}
3939

40-
func append(decl: DeclProtocol) {
40+
func append(decl: DeclProtocol, accessLevel: SwiftAccessLevel? = nil) {
4141
let startLocation = decl.startLocation(converter: converter, afterLeadingTrivia: true)
4242

4343
let path: String? = context.count > 0
4444
? context.map { $0.name.trimmedDescription }.joined(separator: ".")
4545
: nil
4646

47-
let declaration = SwiftDeclaration(decl: decl, path: path, location: startLocation)
47+
let declaration = SwiftDeclaration(decl: decl, accessLevel: accessLevel, path: path, location: startLocation)
4848
declarations.append(declaration)
4949
}
5050

@@ -147,7 +147,9 @@ class Visitor: SyntaxVisitor {
147147
}
148148

149149
override func visit(_ node: EnumCaseDeclSyntax) -> SyntaxVisitorContinueKind {
150-
append(decl: node)
150+
let enumDecl = context.last as? EnumDeclSyntax
151+
assert(enumDecl != nil)
152+
append(decl: node, accessLevel: enumDecl?.accessLevel)
151153
return .skipChildren
152154
}
153155

Sources/swift-doc-coverage/main.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct SwiftDocCoverage: ParsableCommand {
7373
var skipsHiddenFiles: Bool = true
7474

7575
@Option(name: .shortAndLong, help: "Skip source code files with file paths that match the given regular expression.")
76-
var ignoreFilenameRegex: String = ""
76+
var ignoreRegex: String = ""
7777

7878
@Option(name: .shortAndLong, help: "The minimum access level of the symbols considered for coverage statistics: \(AccessLevel.open), \(AccessLevel.public), \(AccessLevel.internal), \(AccessLevel.fileprivate), \(AccessLevel.private).")
7979
var minimumAccessLevel: AccessLevel = .public
@@ -85,7 +85,7 @@ struct SwiftDocCoverage: ParsableCommand {
8585
var output: String?
8686

8787
private enum CodingKeys: String, CodingKey {
88-
case inputs, skipsHiddenFiles, ignoreFilenameRegex, minimumAccessLevel, report, output
88+
case inputs, skipsHiddenFiles, ignoreRegex, minimumAccessLevel, report, output
8989
}
9090

9191
var sources: [SwiftSource] = []
@@ -108,7 +108,7 @@ struct SwiftDocCoverage: ParsableCommand {
108108
}
109109

110110
let urls = try inputs.flatMap {
111-
try Self.files(path: $0, ext: ".swift", skipsHiddenFiles: skipsHiddenFiles, ignoreFilenameRegex: ignoreFilenameRegex)
111+
try Self.files(path: $0, ext: ".swift", skipsHiddenFiles: skipsHiddenFiles, ignoreRegex: ignoreRegex)
112112
}
113113

114114
guard urls.count > 0 else {
@@ -173,7 +173,7 @@ struct SwiftDocCoverage: ParsableCommand {
173173
return cmd
174174
}
175175

176-
static func files(path: String, ext: String, skipsHiddenFiles: Bool, ignoreFilenameRegex: String) throws -> [URL] {
176+
static func files(path: String, ext: String, skipsHiddenFiles: Bool, ignoreRegex: String) throws -> [URL] {
177177
var isDirectory: ObjCBool = false
178178
guard FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) else {
179179
throw Errors.pathNotFound
@@ -182,9 +182,9 @@ struct SwiftDocCoverage: ParsableCommand {
182182
if isDirectory.boolValue {
183183
var urls = [URL]()
184184

185-
let regex: NSRegularExpression? = ignoreFilenameRegex.isEmpty
185+
let regex: NSRegularExpression? = ignoreRegex.isEmpty
186186
? nil
187-
: try NSRegularExpression(pattern: ignoreFilenameRegex)
187+
: try NSRegularExpression(pattern: ignoreRegex)
188188

189189
let url = URL(fileURLWithPath: path)
190190
let resourceKeys = Set<URLResourceKey>([.nameKey, .isDirectoryKey])
@@ -201,9 +201,8 @@ struct SwiftDocCoverage: ParsableCommand {
201201

202202
// Skip by regex
203203
if let regex = regex {
204-
let fileName = fileURL.lastPathComponent
205-
let range = NSRange(fileName.startIndex..., in: fileName)
206-
if regex.firstMatch(in: fileName, range: range) != nil {
204+
let path = fileURL.path
205+
if regex.firstMatch(in: path, range: NSRange(path.startIndex..., in: path)) != nil {
207206
continue
208207
}
209208
}

Tests/SwiftDocCoverageTests/SwiftDocCoverageTests.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ final class SourceCodeTests: XCTestCase {
239239
func test_enum() {
240240
let code =
241241
"""
242-
enum CompassPoint {
242+
public enum CompassPoint {
243243
case north, south
244244
case east
245245
case west
@@ -249,12 +249,24 @@ final class SourceCodeTests: XCTestCase {
249249
"""
250250
let source = SwiftSource(source: code)
251251
XCTAssert(source.declarations.count == 6)
252+
252253
XCTAssert(source.declarations[0].name == "enum CompassPoint")
254+
XCTAssert(source.declarations[0].accessLevel == .public)
255+
253256
XCTAssert(source.declarations[1].name == "case CompassPoint.north,south")
257+
XCTAssert(source.declarations[1].accessLevel == .public)
258+
254259
XCTAssert(source.declarations[2].name == "case CompassPoint.east")
260+
XCTAssert(source.declarations[2].accessLevel == .public)
261+
255262
XCTAssert(source.declarations[3].name == "case CompassPoint.west")
263+
XCTAssert(source.declarations[3].accessLevel == .public)
264+
256265
XCTAssert(source.declarations[4].name == "case CompassPoint.upc(Int, Int, Int, Int)")
266+
XCTAssert(source.declarations[4].accessLevel == .public)
267+
257268
XCTAssert(source.declarations[5].name == "enum Planet<Item>")
269+
XCTAssert(source.declarations[5].accessLevel == .internal)
258270
}
259271

260272
func test_macro() {
@@ -496,7 +508,7 @@ final class SwiftDocCoverageTests: XCTestCase {
496508
}
497509

498510
func test_ignore_filename_regex() throws {
499-
let cmd = try SwiftDocCoverage.run(resourcesUrl.path, "--ignore-filename-regex", "Rect.swift")
511+
let cmd = try SwiftDocCoverage.run(resourcesUrl.path, "--ignore-regex", "Rect.swift")
500512
XCTAssert(cmd.sources.count == 2)
501513

502514
XCTAssert(cmd.sources[0].url?.lastPathComponent == "Size.swift")

0 commit comments

Comments
 (0)