Skip to content

Commit 9c7315d

Browse files
authored
Refactored GlobalActorIsolation type (#14)
1 parent 8eccf67 commit 9c7315d

6 files changed

Lines changed: 58 additions & 50 deletions

File tree

Sources/PrincipleMacros/Builders/Declarations/Common/DeclBuilder.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ extension DeclBuilder {
2626
)
2727
}
2828

29-
public var inheritedGlobalActorIsolation: AttributeSyntax? {
30-
let globalActor: AttributeSyntax? = switch settings.globalActorIsolationPreference {
31-
case .nonisolated:
32-
nil
33-
case let .isolated(globalActor):
34-
"@\(globalActor)"
35-
case .none:
36-
basicDeclaration.globalActor?.trimmed
29+
public var inheritedGlobalActorIsolation: GlobalActorIsolation? {
30+
if let preferred = settings.preferredGlobalActorIsolation {
31+
return preferred
3732
}
38-
return globalActor?.withTrailingSpace
33+
if let inferredType = basicDeclaration.globalActor?.attributeName.trimmed {
34+
return .isolated(trimmedType: inferredType)
35+
}
36+
return .nonisolated
3937
}
4038
}

Sources/PrincipleMacros/Builders/Declarations/Common/DeclBuilderSettings.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import SwiftSyntax
1111
public struct DeclBuilderSettings {
1212

1313
public var accessControlLevel: AccessControlLevel
14-
public var globalActorIsolationPreference: GlobalActorIsolationPreference?
14+
public var preferredGlobalActorIsolation: GlobalActorIsolation?
1515

1616
public init(
1717
accessControlLevel: AccessControlLevel,
18-
globalActorIsolationPreference: GlobalActorIsolationPreference? = nil
18+
preferredGlobalActorIsolation: GlobalActorIsolation? = nil
1919
) {
2020
self.accessControlLevel = accessControlLevel
21-
self.globalActorIsolationPreference = globalActorIsolationPreference
21+
self.preferredGlobalActorIsolation = preferredGlobalActorIsolation
2222
}
2323
}
2424

Sources/PrincipleMacros/Parameters/ParameterExtractor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public struct ParameterExtractor {
6464
return rawString
6565
}
6666

67-
public func globalActorIsolationPreference(
67+
public func preferredGlobalActorIsolation(
6868
withLabel label: TokenSyntax?
69-
) throws -> GlobalActorIsolationPreference? {
69+
) throws -> GlobalActorIsolation? {
7070
guard let expression = expression(withLabel: label) else {
7171
return nil
7272
}
@@ -77,8 +77,8 @@ public struct ParameterExtractor {
7777

7878
if let memberAccessExpression = MemberAccessExprSyntax(expression),
7979
memberAccessExpression.declName.baseName.tokenKind == .keyword(.self),
80-
let baseType = memberAccessExpression.base {
81-
return .isolated("\(baseType)")
80+
let explicitType = memberAccessExpression.base?.trimmed {
81+
return .isolated(trimmedType: "\(explicitType)")
8282
}
8383

8484
throw ParameterExtractionError.unexpectedSyntaxType
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// GlobalActorIsolation.swift
3+
// PrincipleMacros
4+
//
5+
// Created by Kamil Strzelecki on 18/08/2025.
6+
// Copyright © 2025 Kamil Strzelecki. All rights reserved.
7+
//
8+
9+
import SwiftSyntax
10+
11+
public enum GlobalActorIsolation: Hashable {
12+
13+
case nonisolated
14+
case isolated(trimmedType: TypeSyntax)
15+
16+
public var trimmedType: TypeSyntax? {
17+
switch self {
18+
case let .isolated(type):
19+
type
20+
case .nonisolated:
21+
nil
22+
}
23+
}
24+
25+
public var trimmedAttribute: AttributeSyntax? {
26+
guard let trimmedType else {
27+
return nil
28+
}
29+
return AttributeSyntax(
30+
attributeName: trimmedType
31+
)
32+
}
33+
}

Sources/PrincipleMacros/Syntax/Helpers/GlobalActorIsolationPreference.swift

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

Tests/PrincipleMacrosTests/Parameters/ParameterExtractorTests.swift

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,14 @@ extension ParameterExtractorTests {
4646
func testTrailingClosureExtraction() throws {
4747
let extractor = try makeExtractor(from: "#MyMacro { _ in }")
4848
let extracted = try extractor.trailingClosure(withLabel: "operation")
49-
let expected: ExprSyntax = "{ _ in }"
50-
#expect(extracted?.description == expected.description)
49+
#expect(extracted?.description == "{ _ in }")
5150
}
5251

5352
@Test
5453
func testTrailingClosureReferenceExtraction() throws {
5554
let extractor = try makeExtractor(from: "#MyMacro(operation: perform)")
5655
let extracted = try extractor.trailingClosure(withLabel: "operation")
57-
let expected: ExprSyntax = "perform"
58-
#expect(extracted?.description == expected.description)
56+
#expect(extracted?.description == "perform")
5957
}
6058
}
6159

@@ -80,16 +78,16 @@ extension ParameterExtractorTests {
8078
extension ParameterExtractorTests {
8179

8280
@Test
83-
func testMissingGlobalActorPreferenceExtraction() throws {
81+
func testMissingPreferredGlobalActorExtraction() throws {
8482
let extractor = try makeExtractor(from: "#MyMacro()")
85-
let extracted = try extractor.globalActorIsolationPreference(withLabel: "isolation")
83+
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
8684
#expect(extracted == nil)
8785
}
8886

8987
@Test
90-
func testNonisolatedGlobalActorPreferenceExtraction() throws {
88+
func testNonisolatedPreferredGlobalActorExtraction() throws {
9189
let extractor = try makeExtractor(from: "#MyMacro(isolation: nil)")
92-
let extracted = try extractor.globalActorIsolationPreference(withLabel: "isolation")
90+
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
9391
#expect(extracted == .nonisolated)
9492
}
9593

@@ -99,23 +97,17 @@ extension ParameterExtractorTests {
9997
"SomeType.SomeActor"
10098
]
10199
)
102-
func testIsolatedGlobalActorPreferenceExtraction(isolation: String) throws {
100+
func testIsolatedPreferredGlobalActorExtraction(isolation: String) throws {
103101
let extractor = try makeExtractor(from: "#MyMacro(isolation: \(raw: isolation).self)")
104-
let extracted = try extractor.globalActorIsolationPreference(withLabel: "isolation")
105-
106-
switch extracted {
107-
case let .isolated(globalActor):
108-
#expect(globalActor.description == isolation)
109-
default:
110-
Issue.record()
111-
}
102+
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
103+
#expect(extracted?.attribute?.description == "@\(isolation)")
112104
}
113105

114106
@Test
115-
func testUnexpectedSyntaxWhenPerformingGlobalActorPreferenceExtraction() throws {
107+
func testUnexpectedSyntaxWhenPerformingPreferredGlobalActorExtraction() throws {
116108
let extractor = try makeExtractor(from: #"#MyMacro(isolation: MainActor.Type)"#)
117109
#expect(throws: ParameterExtractionError.unexpectedSyntaxType) {
118-
try extractor.globalActorIsolationPreference(withLabel: "isolation")
110+
try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
119111
}
120112
}
121113
}

0 commit comments

Comments
 (0)