File tree 4 files changed +93
-1
lines changed
Tests/FoundationMacrosTests
4 files changed +93
-1
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) 2025 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 BundleMacro : SwiftSyntaxMacros . ExpressionMacro , Sendable {
17
+ public static func expansion( of node: some FreestandingMacroExpansionSyntax , in context: some MacroExpansionContext ) throws -> ExprSyntax {
18
+ """
19
+ {
20
+ #if SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE
21
+ return Bundle.module
22
+ #elseif SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE
23
+ #error( " No resource bundle is available for this module. If resources are included elsewhere, specify the bundle manually. " )
24
+ #else
25
+ return Bundle(_dsoHandle: #dsohandle) ?? .main
26
+ #endif
27
+ }()
28
+ """
29
+ }
30
+ }
Original file line number Diff line number Diff line change @@ -63,6 +63,7 @@ target_compile_options(FoundationMacros PRIVATE -parse-as-library)
63
63
64
64
target_sources (FoundationMacros PRIVATE
65
65
FoundationMacros.swift
66
+ BundleMacro.swift
66
67
PredicateMacro.swift)
67
68
68
69
target_compile_options (FoundationMacros PRIVATE
Original file line number Diff line number Diff line change @@ -17,7 +17,11 @@ import SwiftCompilerPlugin
17
17
18
18
@main
19
19
struct FoundationMacros : CompilerPlugin {
20
- var providingMacros : [ Macro . Type ] = [ PredicateMacro . self, ExpressionMacro . self]
20
+ var providingMacros : [ Macro . Type ] = [
21
+ PredicateMacro . self,
22
+ ExpressionMacro . self,
23
+ BundleMacro . self
24
+ ]
21
25
}
22
26
23
27
#endif
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) 2022-2025 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 XCTest
14
+ import FoundationMacros
15
+
16
+ final class BundleMacroTests : XCTestCase {
17
+
18
+ func testSimple( ) {
19
+ AssertMacroExpansion (
20
+ macros: [ " bundle " : BundleMacro . self] ,
21
+ """
22
+ #bundle
23
+ """ ,
24
+ """
25
+ {
26
+ #if SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE
27
+ return Bundle.module
28
+ #elseif SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE
29
+ #error( " No resource bundle is available for this module. If resources are included elsewhere, specify the bundle manually. " )
30
+ #else
31
+ return Bundle(_dsoHandle: #dsohandle) ?? .main
32
+ #endif
33
+ }()
34
+ """
35
+ )
36
+ }
37
+
38
+ func testUsingParenthesis( ) {
39
+ AssertMacroExpansion (
40
+ macros: [ " bundle " : BundleMacro . self] ,
41
+ """
42
+ #bundle()
43
+ """ ,
44
+ """
45
+ {
46
+ #if SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE
47
+ return Bundle.module
48
+ #elseif SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE
49
+ #error( " No resource bundle is available for this module. If resources are included elsewhere, specify the bundle manually. " )
50
+ #else
51
+ return Bundle(_dsoHandle: #dsohandle) ?? .main
52
+ #endif
53
+ }()
54
+ """
55
+ )
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments