Skip to content

Commit 39bedd1

Browse files
committed
-
1 parent a37ce11 commit 39bedd1

2 files changed

Lines changed: 75 additions & 5 deletions

File tree

Sources/PrincipleMacros/Syntax/Extensions/IfConfigDeclSyntax+Availability.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@ import SwiftSyntaxMacros
1111
extension IfConfigDeclSyntax {
1212

1313
public var availability: Self? {
14-
guard clauses.contains(where: { $0.availability != nil }) else {
15-
return nil
14+
var elements = [IfConfigClauseSyntax]()
15+
var isEmpty = true
16+
17+
for clause in clauses {
18+
if let availability = clause.availability {
19+
elements.append(availability)
20+
isEmpty = false
21+
} else {
22+
elements.append(clause.with(\.elements, .attributes([])))
23+
}
1624
}
17-
let elements = clauses.compactMap { clause in
18-
clause.availability ?? clause.with(\.elements, .attributes([]))
25+
26+
guard !isEmpty else {
27+
return nil
1928
}
20-
return with(\.clauses, IfConfigClauseListSyntax(elements))
29+
30+
let clauses = IfConfigClauseListSyntax(elements)
31+
return with(\.clauses, clauses)
2132
}
2233
}
2334

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// AvailabilityTests.swift
3+
// PrincipleMacros
4+
//
5+
// Created by Kamil Strzelecki on 22/11/2025.
6+
// Copyright © 2025 Kamil Strzelecki. All rights reserved.
7+
//
8+
9+
@testable import PrincipleMacros
10+
import Testing
11+
12+
internal struct AvailabilityTests {
13+
14+
@Test
15+
func withoutAvailability() throws {
16+
let decl: DeclSyntax = """
17+
@MainActor @Observable
18+
class MyClass {}
19+
"""
20+
21+
let classDecl = try #require(decl.as(ClassDeclSyntax.self))
22+
#expect(classDecl.availability == nil)
23+
}
24+
25+
@Test
26+
func withAvailability() throws {
27+
let decl: DeclSyntax = """
28+
@MainActor @Observable
29+
@available(iOS 26, *)
30+
class MyClass {}
31+
"""
32+
33+
let classDecl = try #require(decl.as(ClassDeclSyntax.self))
34+
#expect(classDecl.availability?.trimmedDescription == "@available(iOS 26, *)")
35+
}
36+
37+
@Test
38+
func withIfConfig() throws {
39+
let decl: DeclSyntax = """
40+
#if os(macOS)
41+
@MainActor
42+
#else
43+
@Observable
44+
@available(iOS 26, *)
45+
#endif
46+
class MyClass {}
47+
"""
48+
49+
let expectation = """
50+
#if os(macOS)
51+
#else
52+
@available(iOS 26, *)
53+
#endif
54+
"""
55+
56+
let classDecl = try #require(decl.as(ClassDeclSyntax.self))
57+
#expect(classDecl.availability?.trimmedDescription == expectation)
58+
}
59+
}

0 commit comments

Comments
 (0)