Skip to content

Commit 329193f

Browse files
committed
Handle object literal's rewriting of the first argument label
The macro signature of the object literals is different from the initializer that's used for the rewritten version. Account for this in the macro's definition.
1 parent 07aa98a commit 329193f

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

Sources/_SwiftSyntaxMacros/MacroSystem+Builtin.swift

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ struct FunctionMacro: ExpressionMacro {
177177
}
178178
}
179179

180+
/// Replace the label of the first element in the tuple with the given
181+
/// new label.
182+
private func replaceFirstLabel(
183+
of tuple: TupleExprElementListSyntax, with newLabel: String
184+
) -> TupleExprElementListSyntax{
185+
guard let firstElement = tuple.first else {
186+
return tuple
187+
}
188+
189+
return tuple.replacing(
190+
childAt: 0, with: firstElement.withLabel(.identifier(newLabel)))
191+
}
192+
180193
struct ColorLiteralMacro: ExpressionMacro {
181194
static var name: String { "colorLiteral" }
182195

@@ -188,14 +201,17 @@ struct ColorLiteralMacro: ExpressionMacro {
188201
static var signature: TypeSyntax =
189202
"""
190203
(
191-
_colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float
204+
red: Float, green: Float, blue: Float, alpha: Float
192205
) -> T
193206
"""
194207

195208
static func apply(
196209
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
197210
) -> MacroResult<ExprSyntax> {
198-
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
211+
let argList = replaceFirstLabel(
212+
of: macro.argumentList, with: "_colorLiteralRed"
213+
)
214+
let initSyntax: ExprSyntax = ".init(\(argList))"
199215
if let leadingTrivia = macro.leadingTrivia {
200216
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
201217
}
@@ -212,12 +228,15 @@ struct FileLiteralMacro: ExpressionMacro {
212228
"""
213229

214230
static var signature: TypeSyntax =
215-
"(fileReferenceLiteralResourceName path: String) -> T"
231+
"(resourceName path: String) -> T"
216232

217233
static func apply(
218234
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
219235
) -> MacroResult<ExprSyntax> {
220-
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
236+
let argList = replaceFirstLabel(
237+
of: macro.argumentList, with: "fileReferenceLiteralResourceName"
238+
)
239+
let initSyntax: ExprSyntax = ".init(\(argList))"
221240
if let leadingTrivia = macro.leadingTrivia {
222241
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
223242
}
@@ -234,12 +253,15 @@ struct ImageLiteralMacro: ExpressionMacro {
234253
"""
235254

236255
static var signature: TypeSyntax =
237-
"(imageLiteralResourceName path: String) -> T"
256+
"(resourceName path: String) -> T"
238257

239258
static func apply(
240259
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
241260
) -> MacroResult<ExprSyntax> {
242-
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
261+
let argList = replaceFirstLabel(
262+
of: macro.argumentList, with: "imageLiteralResourceName"
263+
)
264+
let initSyntax: ExprSyntax = ".init(\(argList))"
243265
if let leadingTrivia = macro.leadingTrivia {
244266
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
245267
}

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class MacroSystemTests: XCTestCase {
3939
1
4040
let a = (2)
4141
let b = (x + y, #"x + y"#)
42-
.init(red: 0.5, green: 0.5, blue: 0.25, alpha: 1.0)
42+
.init(_colorLiteralRed: 0.5, green: 0.5, blue: 0.25, alpha: 1.0)
4343
let c = 9
4444
"""
4545
)

0 commit comments

Comments
 (0)