Skip to content

Commit b7ed43b

Browse files
committed
Address a bunch of minor code review comments
1 parent dd1c226 commit b7ed43b

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ let package = Package(
143143

144144
.target(
145145
name: "SwiftIfConfig",
146-
dependencies: ["SwiftSyntax", "SwiftOperators"],
146+
dependencies: ["SwiftSyntax", "SwiftDiagnostics", "SwiftOperators"],
147147
exclude: ["CMakeLists.txt"]
148148
),
149149

Sources/SwiftIfConfig/ActiveSyntaxRewriter.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ extension SyntaxProtocol {
2525
/// clauses, e.g., `#if FOO > 10`, then the condition will be
2626
/// considered to have failed and the clauses's elements will be
2727
/// removed.
28-
public func removingInactive(in configuration: some BuildConfiguration) -> (Syntax, [Diagnostic]) {
28+
public func removingInactive(
29+
in configuration: some BuildConfiguration
30+
) -> (result: Syntax, diagnostics: [Diagnostic]) {
2931
// First pass: Find all of the active clauses for the #ifs we need to
3032
// visit, along with any diagnostics produced along the way. This process
3133
// does not change the tree in any way.
3234
let visitor = ActiveSyntaxVisitor(viewMode: .sourceAccurate, configuration: configuration)
3335
visitor.walk(self)
3436

3537
// If there were no active clauses to visit, we're done!
36-
if visitor.numIfClausesVisited == 0 {
38+
if !visitor.visitedAnyIfClauses {
3739
return (Syntax(self), visitor.diagnostics)
3840
}
3941

Sources/SwiftIfConfig/ActiveSyntaxVisitor.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,35 @@ import SwiftSyntax
3737
/// it would not visit either `f` or `g`.
3838
///
3939
/// All notes visited by this visitor will have the "active" state, i.e.,
40-
/// `node.isActive(in: configuration)` will evaluate to `.active` or will
41-
/// throw. When errors occur, they will be recorded in the set of
40+
/// `node.isActive(in: configuration)` will have evaluated to `.active`
41+
/// When errors occur, they will be recorded in the set of
4242
/// diagnostics.
4343
open class ActiveSyntaxVisitor<Configuration: BuildConfiguration>: SyntaxVisitor {
4444
/// The build configuration, which will be queried for each relevant `#if`.
4545
public let configuration: Configuration
4646

47-
/// The set of diagnostics accumulated during this walk of active syntax.
48-
public var diagnostics: [Diagnostic] = []
47+
/// The diagnostics accumulated during this walk of active syntax.
48+
public private(set) var diagnostics: [Diagnostic] = []
4949

50-
/// The number of "#if" clauses that were visited.
51-
var numIfClausesVisited: Int = 0
50+
/// Whether we visited any "#if" clauses.
51+
var visitedAnyIfClauses: Bool = false
5252

5353
public init(viewMode: SyntaxTreeViewMode, configuration: Configuration) {
5454
self.configuration = configuration
5555
super.init(viewMode: viewMode)
5656
}
5757

5858
open override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
59+
// Note: there is a clone of this code in ActiveSyntaxAnyVisitor. If you
60+
// change one, please also change the other.
5961
let (activeClause, localDiagnostics) = node.activeClause(in: configuration)
6062
diagnostics.append(contentsOf: localDiagnostics)
6163

62-
numIfClausesVisited += 1
64+
visitedAnyIfClauses = true
6365

6466
// If there is an active clause, visit it's children.
6567
if let activeClause, let elements = activeClause.elements {
66-
walk(Syntax(elements))
68+
walk(elements)
6769
}
6870

6971
// Skip everything else in the #if.
@@ -95,32 +97,30 @@ open class ActiveSyntaxVisitor<Configuration: BuildConfiguration>: SyntaxVisitor
9597
/// it would not visit either `f` or `g`.
9698
///
9799
/// All notes visited by this visitor will have the "active" state, i.e.,
98-
/// `node.isActive(in: configuration)` will evaluate to `.active` or will
99-
/// throw.
100-
///
101-
/// All notes visited by this visitor will have the "active" state, i.e.,
102-
/// `node.isActive(in: configuration)` will evaluate to `.active` or will
103-
/// throw. When errors occur, they will be recorded in the set of
104-
/// diagnostivs.
100+
/// `node.isActive(in: configuration)` will have evaluated to `.active`.
101+
/// When errors occur, they will be recorded in the array of diagnostics.
105102
open class ActiveSyntaxAnyVisitor<Configuration: BuildConfiguration>: SyntaxAnyVisitor {
106103
/// The build configuration, which will be queried for each relevant `#if`.
107104
public let configuration: Configuration
108105

109-
/// The set of diagnostics accumulated during this walk of active syntax.
110-
public var diagnostics: [Diagnostic] = []
106+
/// The diagnostics accumulated during this walk of active syntax.
107+
public private(set) var diagnostics: [Diagnostic] = []
111108

112109
public init(viewMode: SyntaxTreeViewMode, configuration: Configuration) {
113110
self.configuration = configuration
114111
super.init(viewMode: viewMode)
115112
}
116113

117114
open override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
115+
// Note: there is a clone of this code in ActiveSyntaxVisitor. If you
116+
// change one, please also change the other.
117+
118118
// If there is an active clause, visit it's children.
119119
let (activeClause, localDiagnostics) = node.activeClause(in: configuration)
120120
diagnostics.append(contentsOf: localDiagnostics)
121121

122122
if let activeClause, let elements = activeClause.elements {
123-
walk(Syntax(elements))
123+
walk(elements)
124124
}
125125

126126
// Skip everything else in the #if.

Sources/SwiftIfConfig/BuildConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public protocol BuildConfiguration {
253253
///
254254
/// The language version can be queried with the `swift` directive that checks
255255
/// how the supported language version compares, as described by
256-
/// [SE-0212](https://github.com/apple/swift-evolution/blob/main/proposals/0212-compiler-version-directive.md). For example:
256+
/// [SE-0212](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0212-compiler-version-directive.md). For example:
257257
///
258258
/// ```swift
259259
/// #if swift(>=5.5)

Sources/SwiftIfConfig/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ add_swift_syntax_library(SwiftIfConfig
2525
target_link_swift_syntax_libraries(SwiftIfConfig PUBLIC
2626
SwiftSyntax
2727
SwiftDiagnostics
28-
SwiftOperators
29-
SwiftParser)
28+
SwiftOperators)

0 commit comments

Comments
 (0)