Skip to content

Commit e1fd996

Browse files
authored
[5.9] Improve Swift Macro template (#6411)
* Use 4 spaces instead of 2 in macro template This is consistent with the other templates. * Don’t explicitly call `init` on `BasicMacroExpansionContext` in the macro template * Add empty lines to help with the formatting of the macro template * Depend on the latest 5.9 prerelease of SwiftSyntax instead of `main`
1 parent 505ef24 commit e1fd996

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

Sources/Workspace/InitPackage.swift

+42-39
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ public final class InitPackage {
251251
} else if packageType == .macro {
252252
pkgParams.append("""
253253
dependencies: [
254-
.package(url: "https://github.com/apple/swift-syntax.git", branch: "main"),
254+
// Depend on the latest Swift 5.9 prerelease of SwiftSyntax
255+
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-10-a"),
255256
]
256257
""")
257258
}
@@ -591,32 +592,34 @@ public final class InitPackage {
591592
]
592593
593594
final class \##(moduleName)Tests: XCTestCase {
594-
func testMacro() {
595-
// XCTest Documentation
596-
// https://developer.apple.com/documentation/xctest
597-
598-
// Test input is a source file containing uses of the macro.
599-
let sf: SourceFileSyntax =
600-
#"""
601-
let a = #stringify(x + y)
602-
let b = #stringify("Hello, \(name)")
603-
"""#
604-
let context = BasicMacroExpansionContext.init(
605-
sourceFiles: [sf: .init(moduleName: "MyModule", fullFilePath: "test.swift")]
606-
)
607-
608-
// Expand the macro to produce a new source file with the
609-
// result of the expansion, and ensure that it has the
610-
// expected source code.
611-
let transformedSF = sf.expand(macros: testMacros, in: context)
612-
XCTAssertEqual(
613-
transformedSF.description,
614-
#"""
615-
let a = (x + y, "x + y")
616-
let b = ("Hello, \(name)", #""Hello, \(name)""#)
617-
"""#
618-
)
619-
}
595+
func testMacro() {
596+
// XCTest Documentation
597+
// https://developer.apple.com/documentation/xctest
598+
599+
// Test input is a source file containing uses of the macro.
600+
let sf: SourceFileSyntax =
601+
#"""
602+
let a = #stringify(x + y)
603+
let b = #stringify("Hello, \(name)")
604+
"""#
605+
606+
let context = BasicMacroExpansionContext(
607+
sourceFiles: [sf: .init(moduleName: "MyModule", fullFilePath: "test.swift")]
608+
)
609+
610+
// Expand the macro to produce a new source file with the
611+
// result of the expansion, and ensure that it has the
612+
// expected source code.
613+
let transformedSF = sf.expand(macros: testMacros, in: context)
614+
615+
XCTAssertEqual(
616+
transformedSF.description,
617+
#"""
618+
let a = (x + y, "x + y")
619+
let b = ("Hello, \(name)", #""Hello, \(name)""#)
620+
"""#
621+
)
622+
}
620623
}
621624
622625
"""##
@@ -643,23 +646,23 @@ public final class InitPackage {
643646
///
644647
/// (x + y, "x + y")
645648
public struct StringifyMacro: ExpressionMacro {
646-
public static func expansion(
647-
of node: some FreestandingMacroExpansionSyntax,
648-
in context: some MacroExpansionContext
649-
) -> ExprSyntax {
650-
guard let argument = node.argumentList.first?.expression else {
651-
fatalError("compiler bug: the macro does not have any arguments")
652-
}
649+
public static func expansion(
650+
of node: some FreestandingMacroExpansionSyntax,
651+
in context: some MacroExpansionContext
652+
) -> ExprSyntax {
653+
guard let argument = node.argumentList.first?.expression else {
654+
fatalError("compiler bug: the macro does not have any arguments")
655+
}
653656
654-
return "(\(argument), \(literal: argument.description))"
655-
}
657+
return "(\(argument), \(literal: argument.description))"
658+
}
656659
}
657660
658661
@main
659662
struct \##(moduleName)Plugin: CompilerPlugin {
660-
let providingMacros: [Macro.Type] = [
661-
StringifyMacro.self,
662-
]
663+
let providingMacros: [Macro.Type] = [
664+
StringifyMacro.self,
665+
]
663666
}
664667
665668
"""##

0 commit comments

Comments
 (0)