Skip to content

Commit beed42f

Browse files
authored
Revert "Refactored GlobalActorIsolation type (#14)"
This reverts commit 9c7315d.
1 parent 9c7315d commit beed42f

6 files changed

Lines changed: 50 additions & 58 deletions

File tree

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

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

29-
public var inheritedGlobalActorIsolation: GlobalActorIsolation? {
30-
if let preferred = settings.preferredGlobalActorIsolation {
31-
return preferred
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
3237
}
33-
if let inferredType = basicDeclaration.globalActor?.attributeName.trimmed {
34-
return .isolated(trimmedType: inferredType)
35-
}
36-
return .nonisolated
38+
return globalActor?.withTrailingSpace
3739
}
3840
}

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 preferredGlobalActorIsolation: GlobalActorIsolation?
14+
public var globalActorIsolationPreference: GlobalActorIsolationPreference?
1515

1616
public init(
1717
accessControlLevel: AccessControlLevel,
18-
preferredGlobalActorIsolation: GlobalActorIsolation? = nil
18+
globalActorIsolationPreference: GlobalActorIsolationPreference? = nil
1919
) {
2020
self.accessControlLevel = accessControlLevel
21-
self.preferredGlobalActorIsolation = preferredGlobalActorIsolation
21+
self.globalActorIsolationPreference = globalActorIsolationPreference
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 preferredGlobalActorIsolation(
67+
public func globalActorIsolationPreference(
6868
withLabel label: TokenSyntax?
69-
) throws -> GlobalActorIsolation? {
69+
) throws -> GlobalActorIsolationPreference? {
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 explicitType = memberAccessExpression.base?.trimmed {
81-
return .isolated(trimmedType: "\(explicitType)")
80+
let baseType = memberAccessExpression.base {
81+
return .isolated("\(baseType)")
8282
}
8383

8484
throw ParameterExtractionError.unexpectedSyntaxType

Sources/PrincipleMacros/Syntax/Helpers/GlobalActorIsolation.swift

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// GlobalActorIsolationPreference.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 GlobalActorIsolationPreference: Hashable {
12+
13+
case nonisolated
14+
case isolated(TypeSyntax)
15+
}

Tests/PrincipleMacrosTests/Parameters/ParameterExtractorTests.swift

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

5253
@Test
5354
func testTrailingClosureReferenceExtraction() throws {
5455
let extractor = try makeExtractor(from: "#MyMacro(operation: perform)")
5556
let extracted = try extractor.trailingClosure(withLabel: "operation")
56-
#expect(extracted?.description == "perform")
57+
let expected: ExprSyntax = "perform"
58+
#expect(extracted?.description == expected.description)
5759
}
5860
}
5961

@@ -78,16 +80,16 @@ extension ParameterExtractorTests {
7880
extension ParameterExtractorTests {
7981

8082
@Test
81-
func testMissingPreferredGlobalActorExtraction() throws {
83+
func testMissingGlobalActorPreferenceExtraction() throws {
8284
let extractor = try makeExtractor(from: "#MyMacro()")
83-
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
85+
let extracted = try extractor.globalActorIsolationPreference(withLabel: "isolation")
8486
#expect(extracted == nil)
8587
}
8688

8789
@Test
88-
func testNonisolatedPreferredGlobalActorExtraction() throws {
90+
func testNonisolatedGlobalActorPreferenceExtraction() throws {
8991
let extractor = try makeExtractor(from: "#MyMacro(isolation: nil)")
90-
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
92+
let extracted = try extractor.globalActorIsolationPreference(withLabel: "isolation")
9193
#expect(extracted == .nonisolated)
9294
}
9395

@@ -97,17 +99,23 @@ extension ParameterExtractorTests {
9799
"SomeType.SomeActor"
98100
]
99101
)
100-
func testIsolatedPreferredGlobalActorExtraction(isolation: String) throws {
102+
func testIsolatedGlobalActorPreferenceExtraction(isolation: String) throws {
101103
let extractor = try makeExtractor(from: "#MyMacro(isolation: \(raw: isolation).self)")
102-
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
103-
#expect(extracted?.attribute?.description == "@\(isolation)")
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+
}
104112
}
105113

106114
@Test
107-
func testUnexpectedSyntaxWhenPerformingPreferredGlobalActorExtraction() throws {
115+
func testUnexpectedSyntaxWhenPerformingGlobalActorPreferenceExtraction() throws {
108116
let extractor = try makeExtractor(from: #"#MyMacro(isolation: MainActor.Type)"#)
109117
#expect(throws: ParameterExtractionError.unexpectedSyntaxType) {
110-
try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
118+
try extractor.globalActorIsolationPreference(withLabel: "isolation")
111119
}
112120
}
113121
}

0 commit comments

Comments
 (0)