Skip to content

Commit 4a1ae7e

Browse files
authored
Merge pull request #1018 from DougGregor/object-literals-rewrite
2 parents 990b24d + 329193f commit 4a1ae7e

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
@@ -189,6 +189,19 @@ struct FunctionMacro: ExpressionMacro {
189189
}
190190
}
191191

192+
/// Replace the label of the first element in the tuple with the given
193+
/// new label.
194+
private func replaceFirstLabel(
195+
of tuple: TupleExprElementListSyntax, with newLabel: String
196+
) -> TupleExprElementListSyntax{
197+
guard let firstElement = tuple.first else {
198+
return tuple
199+
}
200+
201+
return tuple.replacing(
202+
childAt: 0, with: firstElement.withLabel(.identifier(newLabel)))
203+
}
204+
192205
struct ColorLiteralMacro: ExpressionMacro {
193206
static var name: String { "colorLiteral" }
194207

@@ -204,14 +217,17 @@ struct ColorLiteralMacro: ExpressionMacro {
204217
static var signature: TypeSyntax =
205218
"""
206219
(
207-
_colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float
220+
red: Float, green: Float, blue: Float, alpha: Float
208221
) -> T
209222
"""
210223

211224
static func apply(
212225
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
213226
) -> MacroResult<ExprSyntax> {
214-
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
227+
let argList = replaceFirstLabel(
228+
of: macro.argumentList, with: "_colorLiteralRed"
229+
)
230+
let initSyntax: ExprSyntax = ".init(\(argList))"
215231
if let leadingTrivia = macro.leadingTrivia {
216232
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
217233
}
@@ -232,12 +248,15 @@ struct FileLiteralMacro: ExpressionMacro {
232248
"""
233249

234250
static var signature: TypeSyntax =
235-
"(fileReferenceLiteralResourceName path: String) -> T"
251+
"(resourceName path: String) -> T"
236252

237253
static func apply(
238254
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
239255
) -> MacroResult<ExprSyntax> {
240-
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
256+
let argList = replaceFirstLabel(
257+
of: macro.argumentList, with: "fileReferenceLiteralResourceName"
258+
)
259+
let initSyntax: ExprSyntax = ".init(\(argList))"
241260
if let leadingTrivia = macro.leadingTrivia {
242261
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
243262
}
@@ -258,12 +277,15 @@ struct ImageLiteralMacro: ExpressionMacro {
258277
"""
259278

260279
static var signature: TypeSyntax =
261-
"(imageLiteralResourceName path: String) -> T"
280+
"(resourceName path: String) -> T"
262281

263282
static func apply(
264283
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
265284
) -> MacroResult<ExprSyntax> {
266-
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
285+
let argList = replaceFirstLabel(
286+
of: macro.argumentList, with: "imageLiteralResourceName"
287+
)
288+
let initSyntax: ExprSyntax = ".init(\(argList))"
267289
if let leadingTrivia = macro.leadingTrivia {
268290
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
269291
}

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)