Skip to content

Commit ff0f955

Browse files
Tantalum73Andreas Neusuess
and
Andreas Neusuess
authored
Implementation of #bundle (#1274)
* Implementation of #bundle macro * separate macro declaration from implementation of bundle expansion * Remove CurrentBundle.swift --------- Co-authored-by: Andreas Neusuess <[email protected]>
1 parent 80588e9 commit ff0f955

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

Sources/FoundationMacros/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ target_compile_options(FoundationMacros PRIVATE -parse-as-library)
6363

6464
target_sources(FoundationMacros PRIVATE
6565
FoundationMacros.swift
66+
BundleMacro.swift
6667
PredicateMacro.swift)
6768

6869
target_compile_options(FoundationMacros PRIVATE

Sources/FoundationMacros/FoundationMacros.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import SwiftCompilerPlugin
1717

1818
@main
1919
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+
]
2125
}
2226

2327
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

0 commit comments

Comments
 (0)