Skip to content

Commit edf12c8

Browse files
committed
Address code review comments
1 parent aac8d68 commit edf12c8

File tree

8 files changed

+40
-29
lines changed

8 files changed

+40
-29
lines changed

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import SwiftBasicFormat
1414
import SwiftDiagnostics
1515
import SwiftOperators
1616
import SwiftSyntax
17-
import SwiftSyntaxMacroExpansion
18-
import SwiftSyntaxMacros
17+
@_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacroExpansion
18+
@_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacros
1919

2020
extension CompilerPluginMessageHandler {
2121
/// Get concrete macro type from a pair of module name and type name.

Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public enum PluginMessage {
124124
case conformance
125125
case codeItem
126126
case `extension`
127-
case preamble
128-
case body
127+
@_spi(ExperimentalLanguageFeature) case preamble
128+
@_spi(ExperimentalLanguageFeature) case body
129129
}
130130

131131
public struct SourceLocation: Codable {

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SwiftBasicFormat
1414
import SwiftSyntax
15-
@_spi(MacroExpansion) import SwiftSyntaxMacros
15+
@_spi(MacroExpansion) @_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacros
1616

1717
public enum MacroRole {
1818
case expression
@@ -24,8 +24,8 @@ public enum MacroRole {
2424
case conformance
2525
case codeItem
2626
case `extension`
27-
case preamble
28-
case body
27+
@_spi(ExperimentalLanguageFeature) case preamble
28+
@_spi(ExperimentalLanguageFeature) case body
2929
}
3030

3131
extension MacroRole {
@@ -55,7 +55,7 @@ enum MacroExpansionError: Error, CustomStringConvertible {
5555
case declarationHasNoBody
5656
case noExtendedTypeSyntax
5757
case noFreestandingMacroRoles(Macro.Type)
58-
case tooManyBodyMacros
58+
case moreThanOneBodyMacro
5959
case preambleWithoutBody
6060

6161
var description: String {
@@ -81,7 +81,7 @@ enum MacroExpansionError: Error, CustomStringConvertible {
8181
case .noFreestandingMacroRoles(let type):
8282
return "macro implementation type '\(type)' does not conform to any freestanding macro protocol"
8383

84-
case .tooManyBodyMacros:
84+
case .moreThanOneBodyMacro:
8585
return "function can not have more than one body macro applied to it"
8686

8787
case .preambleWithoutBody:

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import SwiftOperators
1515
@_spi(MacroExpansion) import SwiftParser
1616
import SwiftSyntax
1717
import SwiftSyntaxBuilder
18-
@_spi(MacroExpansion) import SwiftSyntaxMacros
18+
@_spi(MacroExpansion) @_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacros
1919

2020
// MARK: - Public entry function
2121

@@ -379,7 +379,7 @@ private func expandPreambleMacro(
379379
// Match the indentation of the statements if we can, and put newlines around
380380
// the preamble to separate it from the rest of the body.
381381
let indentation = decl.body?.statements.indentationOfFirstLine ?? (decl.indentationOfFirstLine + indentationWidth)
382-
let indentedSource = "\n" + expanded.indented(by: indentation) + "\n\n\n"
382+
let indentedSource = "\n" + expanded.indented(by: indentation) + "\n\n"
383383
return "\(raw: indentedSource)"
384384
}
385385

@@ -410,7 +410,8 @@ private func expandBodyMacro(
410410
}
411411

412412
// Wrap the body in braces.
413-
let indentedSource = " {\n" + expanded.indented(by: decl.indentationOfFirstLine + indentationWidth) + "\n}\n"
413+
let beforeBody = decl.body == nil ? " " : "";
414+
let indentedSource = beforeBody + "{\n" + expanded.indented(by: decl.indentationOfFirstLine + indentationWidth) + "\n}\n"
414415
return "\(raw: indentedSource)" as CodeBlockSyntax
415416
}
416417

@@ -704,7 +705,7 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
704705
body = expandedBodies[0]
705706

706707
default:
707-
context.addDiagnostics(from: MacroExpansionError.tooManyBodyMacros, node: node)
708+
context.addDiagnostics(from: MacroExpansionError.moreThanOneBodyMacro, node: node)
708709
body = expandedBodies[0]
709710
}
710711

@@ -713,9 +714,7 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
713714
return node.with(\.body, body)
714715
}
715716

716-
var statements = body.statements
717-
statements.insert(contentsOf: preamble, at: statements.startIndex)
718-
return node.with(\.body, body.with(\.statements, statements))
717+
return node.with(\.body, body.with(\.statements, preamble + body.statements))
719718
}
720719

721720
override func visit(_ node: CodeBlockItemListSyntax) -> CodeBlockItemListSyntax {

Sources/SwiftSyntaxMacros/MacroProtocols/BodyMacro.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import SwiftSyntax
1111

1212
/// Describes a macro that can create the body for a function that does not
1313
/// have one.
14+
@_spi(ExperimentalLanguageFeature)
1415
public protocol BodyMacro: AttachedMacro {
1516
/// Expand a macro described by the given custom attribute and
1617
/// attached to the given declaration and evaluated within a

Sources/SwiftSyntaxMacros/MacroProtocols/PreambleMacro.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import SwiftSyntax
1111

1212
/// Describes a macro that can introduce "preamble" code into an existing
1313
/// function body.
14+
@_spi(ExperimentalLanguageFeature)
1415
public protocol PreambleMacro: AttachedMacro {
1516
/// Expand a macro described by the given custom attribute and
1617
/// attached to the given declaration and evaluated within a

Tests/SwiftSyntaxMacroExpansionTest/BodyMacroTests.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import SwiftDiagnostics
2222
import SwiftSyntax
2323
@_spi(Testing) import SwiftSyntaxMacroExpansion
24-
import SwiftSyntaxMacros
24+
@_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacros
2525
import SwiftSyntaxMacrosTestSupport
2626
import XCTest
2727

@@ -39,11 +39,7 @@ struct RemoteBodyMacro: BodyMacro {
3939

4040
let funcBaseName = funcDecl.name.text
4141
let paramNames = funcDecl.signature.parameterClause.parameters.map { param in
42-
if let name = param.parameterName {
43-
return name
44-
} else {
45-
return TokenSyntax(.wildcard, presence: .present)
46-
}
42+
param.parameterName ?? TokenSyntax(.wildcard, presence: .present)
4743
}
4844

4945
let passedArgs = DictionaryExprSyntax(
@@ -109,6 +105,24 @@ final class BodyMacroTests: XCTestCase {
109105
macros: ["Remote": RemoteBodyMacro.self],
110106
indentationWidth: indentationWidth
111107
)
108+
}
109+
110+
func testBodyExpansionReplacement() {
111+
assertMacroExpansion(
112+
"""
113+
@Remote
114+
func f(a: Int, b: String) async throws -> String {
115+
this; code; is; unused
116+
}
117+
""",
118+
expandedSource: """
112119
120+
func f(a: Int, b: String) async throws -> String {
121+
return try await remoteCall(function: "f", arguments: ["a": a, "b": b])
122+
}
123+
""",
124+
macros: ["Remote": RemoteBodyMacro.self],
125+
indentationWidth: indentationWidth
126+
)
113127
}
114128
}

Tests/SwiftSyntaxMacroExpansionTest/PreambleMacroTests.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import SwiftDiagnostics
2222
import SwiftSyntax
2323
@_spi(Testing) import SwiftSyntaxMacroExpansion
24-
import SwiftSyntaxMacros
24+
@_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacros
2525
import SwiftSyntaxMacrosTestSupport
2626
import XCTest
2727

@@ -39,17 +39,13 @@ struct TracedPreambleMacro: PreambleMacro {
3939

4040
let funcBaseName = funcDecl.name
4141
let paramNames = funcDecl.signature.parameterClause.parameters.map { param in
42-
if let name = param.parameterName {
43-
return name.text
44-
} else {
45-
return "_"
46-
}
42+
param.parameterName?.text ?? "_"
4743
}
4844

4945
let passedArgs = paramNames.map { "\($0): \\(\($0))" }.joined(separator: ", ")
5046

5147
let entry: CodeBlockItemSyntax = """
52-
log("Entering \(raw: funcBaseName)(\(raw: passedArgs))")
48+
log("Entering \(funcBaseName)(\(raw: passedArgs))")
5349
"""
5450

5551
let argLabels = paramNames.map { "\($0):" }.joined()

0 commit comments

Comments
 (0)