Skip to content

Commit 3c98f24

Browse files
committed
Address more comments
1 parent 4bd569a commit 3c98f24

File tree

8 files changed

+145
-159
lines changed

8 files changed

+145
-159
lines changed

Sources/SwiftIfConfig/ConfiguredRegions.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ import SwiftSyntax
1515

1616
extension SyntaxProtocol {
1717
/// Find all of the #if/#elseif/#else clauses within the given syntax node,
18-
/// indicating their active state. This operation will recurse into active
19-
/// clauses to represent the flattened nested structure, while nonactive
20-
/// clauses need no recursion (because there is no relevant structure in
21-
/// them).
18+
/// indicating their active state. This operation will recurse into all
19+
/// clauses to indicate regions of active / inactive / unparsed code.
2220
///
2321
/// For example, given code like the following:
2422
/// #if DEBUG

Sources/SwiftIfConfig/IfConfigDecl+IfConfig.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension IfConfigDeclSyntax {
2626
/// ```
2727
///
2828
/// If the `A` configuration option was passed on the command line (e.g. via `-DA`), the first clause
29-
/// (containing `func f()`) would be returned. If not, and if the `B`configuration was passed on the
29+
/// (containing `func f()`) would be returned. If not, and if the `B` configuration was passed on the
3030
/// command line, the second clause (containing `func g()`) would be returned. If neither was
3131
/// passed, this function will return `nil` to indicate that none of the regions are active.
3232
///

Sources/SwiftIfConfig/SyntaxProtocol+IfConfig.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension SyntaxProtocol {
4040
while let parent = currentNode.parent {
4141
// If the parent is an `#if` configuration, check whether our current
4242
// clause is active. If not, we're in an inactive region. We also
43-
// need to determine whether
43+
// need to determine whether an inactive region should be parsed or not.
4444
if let ifConfigClause = currentNode.as(IfConfigClauseSyntax.self),
4545
let ifConfigDecl = ifConfigClause.parent?.parent?.as(IfConfigDeclSyntax.self)
4646
{
@@ -73,8 +73,9 @@ extension SyntaxProtocol {
7373
/// Determine whether the given syntax node is active given a set of
7474
/// configured regions as produced by `configuredRegions(in:)`.
7575
///
76-
/// This is
77-
/// an approximation
76+
/// If you are querying whether many syntax nodes in a particular file are
77+
/// active, consider calling `configuredRegions(in:)` once and using
78+
/// this function. For occasional queries, use `isActive(in:)`.
7879
public func isActive(
7980
inConfiguredRegions regions: [(IfConfigClauseSyntax, IfConfigRegionState)]
8081
) -> IfConfigRegionState {

Sources/SwiftSyntaxMacrosGenericTestSupport/Assertions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ public enum DiagnosticAssertionContext {
365365
}
366366
}
367367

368+
@_spi(Testing)
368369
public func assertDiagnostic(
369370
_ diag: Diagnostic,
370371
in expansionContext: DiagnosticAssertionContext,

Tests/SwiftIfConfigTest/ActiveRegionTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import SwiftParser
1616
import SwiftSyntax
1717
import SwiftSyntaxMacrosGenericTestSupport
1818
import XCTest
19+
import _SwiftSyntaxGenericTestSupport
20+
import _SwiftSyntaxTestSupport
1921

2022
public class ActiveRegionTests: XCTestCase {
2123
let linuxBuildConfig = TestingBuildConfiguration(
@@ -99,3 +101,45 @@ public class ActiveRegionTests: XCTestCase {
99101
)
100102
}
101103
}
104+
105+
/// Assert that the various marked positions in the source code have the
106+
/// expected active states.
107+
func assertActiveCode(
108+
_ markedSource: String,
109+
configuration: some BuildConfiguration = TestingBuildConfiguration(),
110+
states: [String: IfConfigRegionState],
111+
file: StaticString = #filePath,
112+
line: UInt = #line
113+
) throws {
114+
// Pull out the markers that we'll use to dig out nodes to query.
115+
let (markerLocations, source) = extractMarkers(markedSource)
116+
117+
var parser = Parser(source)
118+
let tree = SourceFileSyntax.parse(from: &parser)
119+
120+
let configuredRegions = tree.configuredRegions(in: configuration)
121+
122+
for (marker, location) in markerLocations {
123+
guard let expectedState = states[marker] else {
124+
XCTFail("Missing marker \(marker) in expected states", file: file, line: line)
125+
continue
126+
}
127+
128+
guard let token = tree.token(at: AbsolutePosition(utf8Offset: location)) else {
129+
XCTFail("Unable to find token at location \(location)", file: file, line: line)
130+
continue
131+
}
132+
133+
let (actualState, _) = token.isActive(in: configuration)
134+
XCTAssertEqual(actualState, expectedState, "isActive(in:) at marker \(marker)", file: file, line: line)
135+
136+
let actualViaRegions = token.isActive(inConfiguredRegions: configuredRegions)
137+
XCTAssertEqual(
138+
actualViaRegions,
139+
expectedState,
140+
"isActive(inConfiguredRegions:) at marker \(marker)",
141+
file: file,
142+
line: line
143+
)
144+
}
145+
}

Tests/SwiftIfConfigTest/Assertions.swift

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

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import SwiftDiagnostics
1314
import SwiftIfConfig
1415
import SwiftParser
1516
import SwiftSyntax
16-
import SwiftSyntaxMacrosGenericTestSupport
17+
@_spi(XCTestFailureLocation) @_spi(Testing) import SwiftSyntaxMacrosGenericTestSupport
1718
import XCTest
1819
import _SwiftSyntaxTestSupport
20+
import _SwiftSyntaxGenericTestSupport
1921

2022
public class EvaluateTests: XCTestCase {
2123
func testLiterals() throws {
@@ -229,3 +231,43 @@ public class EvaluateTests: XCTestCase {
229231
)
230232
}
231233
}
234+
235+
/// Assert the results of evaluating the condition within an `#if` against the
236+
/// given build configuration.
237+
func assertIfConfig(
238+
_ condition: ExprSyntax,
239+
_ expectedState: IfConfigRegionState,
240+
configuration: some BuildConfiguration = TestingBuildConfiguration(),
241+
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
242+
file: StaticString = #filePath,
243+
line: UInt = #line
244+
) {
245+
// Evaluate the condition to check the state.
246+
let actualDiagnostics: [Diagnostic]
247+
let actualState: IfConfigRegionState
248+
(actualState, actualDiagnostics) = IfConfigRegionState.evaluating (condition, in: configuration)
249+
XCTAssertEqual(actualState, expectedState, file: file, line: line)
250+
251+
// Check the diagnostics.
252+
if actualDiagnostics.count != expectedDiagnostics.count {
253+
XCTFail(
254+
"""
255+
Expected \(expectedDiagnostics.count) diagnostics, but got \(actualDiagnostics.count):
256+
\(actualDiagnostics.map(\.debugDescription).joined(separator: "\n"))
257+
""",
258+
file: file,
259+
line: line
260+
)
261+
} else {
262+
for (actualDiag, expectedDiag) in zip(actualDiagnostics, expectedDiagnostics) {
263+
assertDiagnostic(
264+
actualDiag,
265+
in: .tree(condition),
266+
expected: expectedDiag,
267+
failureHandler: {
268+
XCTFail($0.message, file: $0.location.staticFilePath, line: $0.location.unsignedLine)
269+
}
270+
)
271+
}
272+
}
273+
}

Tests/SwiftIfConfigTest/VisitorTests.swift

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import SwiftDiagnostics
1414
import SwiftIfConfig
1515
import SwiftParser
1616
import SwiftSyntax
17-
import SwiftSyntaxMacrosGenericTestSupport
17+
@_spi(XCTestFailureLocation) @_spi(Testing) import SwiftSyntaxMacrosGenericTestSupport
1818
import XCTest
19+
import _SwiftSyntaxTestSupport
20+
import _SwiftSyntaxGenericTestSupport
1921

2022
/// Visitor that ensures that all of the nodes we visit are active.
2123
///
@@ -252,3 +254,50 @@ public class VisitorTests: XCTestCase {
252254
)
253255
}
254256
}
257+
258+
/// Assert that applying the given build configuration to the source code
259+
/// returns the expected source and diagnostics.
260+
func assertRemoveInactive(
261+
_ source: String,
262+
configuration: some BuildConfiguration,
263+
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
264+
expectedSource: String,
265+
file: StaticString = #filePath,
266+
line: UInt = #line
267+
) {
268+
var parser = Parser(source)
269+
let tree = SourceFileSyntax.parse(from: &parser)
270+
271+
let (treeWithoutInactive, actualDiagnostics) = tree.removingInactive(in: configuration)
272+
273+
// Check the resulting tree.
274+
assertStringsEqualWithDiff(
275+
treeWithoutInactive.description,
276+
expectedSource,
277+
file: file,
278+
line: line
279+
)
280+
281+
// Check the diagnostics.
282+
if actualDiagnostics.count != expectedDiagnostics.count {
283+
XCTFail(
284+
"""
285+
Expected \(expectedDiagnostics.count) diagnostics, but got \(actualDiagnostics.count):
286+
\(actualDiagnostics.map(\.debugDescription).joined(separator: "\n"))
287+
""",
288+
file: file,
289+
line: line
290+
)
291+
} else {
292+
for (actualDiag, expectedDiag) in zip(actualDiagnostics, expectedDiagnostics) {
293+
assertDiagnostic(
294+
actualDiag,
295+
in: .tree(tree),
296+
expected: expectedDiag,
297+
failureHandler: {
298+
XCTFail($0.message, file: $0.location.staticFilePath, line: $0.location.unsignedLine)
299+
}
300+
)
301+
}
302+
}
303+
}

0 commit comments

Comments
 (0)