From 2c80bead4e4128d4271e5264ee356a1f13459c39 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Tue, 11 Apr 2023 11:49:18 -0700 Subject: [PATCH 1/4] Use 4 spaces instead of 2 in macro template This is consistent with the other templates. --- Sources/Workspace/InitPackage.swift | 76 ++++++++++++++--------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/Sources/Workspace/InitPackage.swift b/Sources/Workspace/InitPackage.swift index 5b8797426c4..73a2d43b76e 100644 --- a/Sources/Workspace/InitPackage.swift +++ b/Sources/Workspace/InitPackage.swift @@ -591,32 +591,32 @@ public final class InitPackage { ] final class \##(moduleName)Tests: XCTestCase { - func testMacro() { - // XCTest Documentation - // https://developer.apple.com/documentation/xctest - - // Test input is a source file containing uses of the macro. - let sf: SourceFileSyntax = - #""" - let a = #stringify(x + y) - let b = #stringify("Hello, \(name)") - """# - let context = BasicMacroExpansionContext.init( - sourceFiles: [sf: .init(moduleName: "MyModule", fullFilePath: "test.swift")] - ) - - // Expand the macro to produce a new source file with the - // result of the expansion, and ensure that it has the - // expected source code. - let transformedSF = sf.expand(macros: testMacros, in: context) - XCTAssertEqual( - transformedSF.description, - #""" - let a = (x + y, "x + y") - let b = ("Hello, \(name)", #""Hello, \(name)""#) - """# - ) - } + func testMacro() { + // XCTest Documentation + // https://developer.apple.com/documentation/xctest + + // Test input is a source file containing uses of the macro. + let sf: SourceFileSyntax = + #""" + let a = #stringify(x + y) + let b = #stringify("Hello, \(name)") + """# + let context = BasicMacroExpansionContext.init( + sourceFiles: [sf: .init(moduleName: "MyModule", fullFilePath: "test.swift")] + ) + + // Expand the macro to produce a new source file with the + // result of the expansion, and ensure that it has the + // expected source code. + let transformedSF = sf.expand(macros: testMacros, in: context) + XCTAssertEqual( + transformedSF.description, + #""" + let a = (x + y, "x + y") + let b = ("Hello, \(name)", #""Hello, \(name)""#) + """# + ) + } } """## @@ -643,23 +643,23 @@ public final class InitPackage { /// /// (x + y, "x + y") public struct StringifyMacro: ExpressionMacro { - public static func expansion( - of node: some FreestandingMacroExpansionSyntax, - in context: some MacroExpansionContext - ) -> ExprSyntax { - guard let argument = node.argumentList.first?.expression else { - fatalError("compiler bug: the macro does not have any arguments") - } + public static func expansion( + of node: some FreestandingMacroExpansionSyntax, + in context: some MacroExpansionContext + ) -> ExprSyntax { + guard let argument = node.argumentList.first?.expression else { + fatalError("compiler bug: the macro does not have any arguments") + } - return "(\(argument), \(literal: argument.description))" - } + return "(\(argument), \(literal: argument.description))" + } } @main struct \##(moduleName)Plugin: CompilerPlugin { - let providingMacros: [Macro.Type] = [ - StringifyMacro.self, - ] + let providingMacros: [Macro.Type] = [ + StringifyMacro.self, + ] } """## From 3936cf791162e046f2b8c7c4dae472e4ec8168e6 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Tue, 11 Apr 2023 11:50:48 -0700 Subject: [PATCH 2/4] =?UTF-8?q?Don=E2=80=99t=20explicitly=20call=20`init`?= =?UTF-8?q?=20on=20`BasicMacroExpansionContext`=20in=20the=20macro=20templ?= =?UTF-8?q?ate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Workspace/InitPackage.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Workspace/InitPackage.swift b/Sources/Workspace/InitPackage.swift index 73a2d43b76e..3b43c455808 100644 --- a/Sources/Workspace/InitPackage.swift +++ b/Sources/Workspace/InitPackage.swift @@ -601,7 +601,7 @@ public final class InitPackage { let a = #stringify(x + y) let b = #stringify("Hello, \(name)") """# - let context = BasicMacroExpansionContext.init( + let context = BasicMacroExpansionContext( sourceFiles: [sf: .init(moduleName: "MyModule", fullFilePath: "test.swift")] ) From 6b654dbb5051f0f7ed2f649dc8eecb2c182e15e8 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Tue, 11 Apr 2023 11:51:08 -0700 Subject: [PATCH 3/4] Add empty lines to help with the formatting of the macro template --- Sources/Workspace/InitPackage.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Workspace/InitPackage.swift b/Sources/Workspace/InitPackage.swift index 3b43c455808..f649fc1ec0f 100644 --- a/Sources/Workspace/InitPackage.swift +++ b/Sources/Workspace/InitPackage.swift @@ -601,6 +601,7 @@ public final class InitPackage { let a = #stringify(x + y) let b = #stringify("Hello, \(name)") """# + let context = BasicMacroExpansionContext( sourceFiles: [sf: .init(moduleName: "MyModule", fullFilePath: "test.swift")] ) @@ -609,6 +610,7 @@ public final class InitPackage { // result of the expansion, and ensure that it has the // expected source code. let transformedSF = sf.expand(macros: testMacros, in: context) + XCTAssertEqual( transformedSF.description, #""" From 51b826c7ea4dd2b7e5415a11dd870184fc138c31 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Tue, 11 Apr 2023 11:53:06 -0700 Subject: [PATCH 4/4] Depend on the latest 5.9 prerelease of SwiftSyntax instead of `main` --- Sources/Workspace/InitPackage.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/Workspace/InitPackage.swift b/Sources/Workspace/InitPackage.swift index f649fc1ec0f..64caca8e9a6 100644 --- a/Sources/Workspace/InitPackage.swift +++ b/Sources/Workspace/InitPackage.swift @@ -251,7 +251,8 @@ public final class InitPackage { } else if packageType == .macro { pkgParams.append(""" dependencies: [ - .package(url: "https://github.com/apple/swift-syntax.git", branch: "main"), + // Depend on the latest Swift 5.9 prerelease of SwiftSyntax + .package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-10-a"), ] """) }