File tree 6 files changed +144
-0
lines changed
Tests/MacroExamples/Implementation/Accessor
6 files changed +144
-0
lines changed Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import SwiftSyntax
14
+ import SwiftSyntaxMacros
15
+
16
+ public struct EnvironmentValueMacro : AccessorMacro {
17
+ public static func expansion(
18
+ of node: AttributeSyntax ,
19
+ providingAccessorsOf declaration: some DeclSyntaxProtocol ,
20
+ in context: some MacroExpansionContext
21
+ ) throws -> [ AccessorDeclSyntax ] {
22
+ guard
23
+ case let . argumentList( arguments) = node. arguments,
24
+ let argument = arguments. first
25
+ else { return [ ] }
26
+
27
+ return [
28
+ """
29
+ get { self[ \( raw: argument. expression) ] }
30
+ """ ,
31
+ """
32
+ set { self[ \( raw: argument. expression) ] = newValue }
33
+ """ ,
34
+ ]
35
+ }
36
+ }
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ struct MyPlugin: CompilerPlugin {
26
26
DefaultFatalErrorImplementationMacro . self,
27
27
DictionaryStorageMacro . self,
28
28
DictionaryStoragePropertyMacro . self,
29
+ EnvironmentValueMacro . self,
29
30
EquatableExtensionMacro . self,
30
31
FontLiteralMacro . self,
31
32
FuncUniqueMacro . self,
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ // MARK: - EnvironmentValue Accessor
14
+
15
+ import SwiftUI
16
+
17
+ @attached ( accessor)
18
+ public macro EnvironmentValue( for key: any EnvironmentKey . Type ) =
19
+ #externalMacro( module: " MacroExamplesImplementation " , type: " EnvironmentValueMacro " )
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ // MARK: - EnvironmentValue Accessor
14
+
15
+ import MacroExamplesInterface
16
+ import SwiftUI
17
+
18
+ private struct MyEnvironmentKey : EnvironmentKey {
19
+ static let defaultValue : String = " Default value "
20
+ }
21
+
22
+ extension EnvironmentValues {
23
+ @EnvironmentValue ( for: MyEnvironmentKey . self)
24
+ var myCustomValue : String
25
+ }
26
+
27
+ func runEnvironmentValueAccessorMacroPlayground( ) {
28
+ var environmentValues = EnvironmentValues ( )
29
+ print ( " Default myCustomValue: \( environmentValues. myCustomValue) " )
30
+ environmentValues. myCustomValue = " New value "
31
+ print ( " New myCustomValue: \( environmentValues. myCustomValue) " )
32
+ }
Original file line number Diff line number Diff line change @@ -47,3 +47,7 @@ runMemberMacrosPlayground()
47
47
// MARK: - Peer Macros
48
48
49
49
runPeerMacrosPlayground ( )
50
+
51
+ // MARKL - Accessor Macros
52
+
53
+ runEnvironmentValueAccessorMacroPlayground ( )
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import MacroExamplesImplementation
14
+ import SwiftSyntaxMacros
15
+ import SwiftSyntaxMacrosTestSupport
16
+ import XCTest
17
+
18
+ final class EnvironmentValueMacroMacroTests : XCTestCase {
19
+ private let macros = [ " EnvironmentValue " : EnvironmentValueMacro . self]
20
+
21
+ func testEnvironmentValue( ) {
22
+ assertMacroExpansion (
23
+ """
24
+ private struct MyEnvironmentKey: EnvironmentKey {
25
+ static let defaultValue: String = " Default value "
26
+ }
27
+
28
+ extension EnvironmentValues {
29
+ @EnvironmentValue(for: MyEnvironmentKey.self)
30
+ var myCustomValue: String
31
+ }
32
+ """ ,
33
+ expandedSource: """
34
+ private struct MyEnvironmentKey: EnvironmentKey {
35
+ static let defaultValue: String = " Default value "
36
+ }
37
+
38
+ extension EnvironmentValues {
39
+ var myCustomValue: String {
40
+ get {
41
+ self[MyEnvironmentKey.self]
42
+ }
43
+ set {
44
+ self[MyEnvironmentKey.self] = newValue
45
+ }
46
+ }
47
+ }
48
+ """ ,
49
+ macros: macros
50
+ )
51
+ }
52
+ }
You can’t perform that action at this time.
0 commit comments