Skip to content

Support IfConfigDecl clauses in UseEnumForNamespacing #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Sources/SwiftFormatRules/UseEnumForNamespacing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ public final class UseEnumForNamespacing: SyntaxFormatRule {
}
// Do not append private initializer.

case let decl as IfConfigDeclSyntax:
let membersToKeep: [MemberDeclListSyntax] = decl.clauses
.compactMap { clause in
(clause.elements as? MemberDeclListSyntax).flatMap(membersToKeepIfUsedAsNamespace(_:))
}

if membersToKeep.count < decl.clauses.count {
return nil
} else {
declList.append(member)
}

default:
declList.append(member)
}
Expand Down
116 changes: 116 additions & 0 deletions Tests/SwiftFormatRulesTests/UseEnumForNamespacingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,64 @@ public class UseEnumForNamespacingTests: DiagnosingTestCase {
final class E {
static let a = 123
}
struct Structure {
#if canImport(AppKit)
var native: NSSomething
#elseif canImport(UIKit)
var native: UISomething
#endif
}
struct Structure {
#if canImport(AppKit)
static var native: NSSomething
#elseif canImport(UIKit)
static var native: UISomething
#endif
}
struct Structure {
#if canImport(AppKit)
var native: NSSomething
#elseif canImport(UIKit)
static var native: UISomething
#endif
}
struct Structure {
#if canImport(AppKit)
static var native: NSSomething
#else
static var native: UISomething
#endif
}
struct Structure {
#if canImport(AppKit)
#if swift(>=4.0)
static var native: NSSomething
#else
static var deprecated_native: NSSomething
#endif
#else
#if swift(>=4.0)
static var native: UISomething
#else
static var deprecated_native: UISomething
#endif
#endif
}
struct Structure {
#if canImport(AppKit)
#if swift(>=4.0)
static var native: NSSomething
#else
static var deprecated_native: NSSomething
#endif
#else
#if swift(>=4.0)
static var native: UISomething
#else
var deprecated_native: UISomething
#endif
#endif
}
""",
expected: """
enum A {
Expand All @@ -46,6 +104,64 @@ public class UseEnumForNamespacingTests: DiagnosingTestCase {
final class E {
static let a = 123
}
struct Structure {
#if canImport(AppKit)
var native: NSSomething
#elseif canImport(UIKit)
var native: UISomething
#endif
}
enum Structure {
#if canImport(AppKit)
static var native: NSSomething
#elseif canImport(UIKit)
static var native: UISomething
#endif
}
struct Structure {
#if canImport(AppKit)
var native: NSSomething
#elseif canImport(UIKit)
static var native: UISomething
#endif
}
enum Structure {
#if canImport(AppKit)
static var native: NSSomething
#else
static var native: UISomething
#endif
}
enum Structure {
#if canImport(AppKit)
#if swift(>=4.0)
static var native: NSSomething
#else
static var deprecated_native: NSSomething
#endif
#else
#if swift(>=4.0)
static var native: UISomething
#else
static var deprecated_native: UISomething
#endif
#endif
}
struct Structure {
#if canImport(AppKit)
#if swift(>=4.0)
static var native: NSSomething
#else
static var deprecated_native: NSSomething
#endif
#else
#if swift(>=4.0)
static var native: UISomething
#else
var deprecated_native: UISomething
#endif
#endif
}
""")
}
}