Skip to content

[5.9] Improve Swift Macro template #6411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 42 additions & 39 deletions Sources/Workspace/InitPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this version what we want from a semantic versioning pov?

cc @neonichu

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neonichu and I discussed this and it’s what we want for now. As we approach the release, we want to change this to 509.0.0.

]
""")
}
Expand Down Expand Up @@ -591,32 +592,34 @@ 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(
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)""#)
"""#
)
}
}

"""##
Expand All @@ -643,23 +646,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,
]
}

"""##
Expand Down