Skip to content

Handle object literal's rewriting of the first argument label #1018

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 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
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
34 changes: 28 additions & 6 deletions Sources/_SwiftSyntaxMacros/MacroSystem+Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ struct FunctionMacro: ExpressionMacro {
}
}

/// Replace the label of the first element in the tuple with the given
/// new label.
private func replaceFirstLabel(
of tuple: TupleExprElementListSyntax, with newLabel: String
) -> TupleExprElementListSyntax{
guard let firstElement = tuple.first else {
return tuple
}

return tuple.replacing(
childAt: 0, with: firstElement.withLabel(.identifier(newLabel)))
}

struct ColorLiteralMacro: ExpressionMacro {
static var name: String { "colorLiteral" }

Expand All @@ -188,14 +201,17 @@ struct ColorLiteralMacro: ExpressionMacro {
static var signature: TypeSyntax =
"""
(
_colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float
red: Float, green: Float, blue: Float, alpha: Float
) -> T
"""

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
let argList = replaceFirstLabel(
of: macro.argumentList, with: "_colorLiteralRed"
)
let initSyntax: ExprSyntax = ".init(\(argList))"
if let leadingTrivia = macro.leadingTrivia {
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
}
Expand All @@ -212,12 +228,15 @@ struct FileLiteralMacro: ExpressionMacro {
"""

static var signature: TypeSyntax =
"(fileReferenceLiteralResourceName path: String) -> T"
"(resourceName path: String) -> T"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
let argList = replaceFirstLabel(
of: macro.argumentList, with: "fileReferenceLiteralResourceName"
)
let initSyntax: ExprSyntax = ".init(\(argList))"
if let leadingTrivia = macro.leadingTrivia {
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
}
Expand All @@ -234,12 +253,15 @@ struct ImageLiteralMacro: ExpressionMacro {
"""

static var signature: TypeSyntax =
"(imageLiteralResourceName path: String) -> T"
"(resourceName path: String) -> T"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
let initSyntax: ExprSyntax = ".init(\(macro.argumentList))"
let argList = replaceFirstLabel(
of: macro.argumentList, with: "imageLiteralResourceName"
)
let initSyntax: ExprSyntax = ".init(\(argList))"
if let leadingTrivia = macro.leadingTrivia {
return MacroResult(initSyntax.withLeadingTrivia(leadingTrivia))
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class MacroSystemTests: XCTestCase {
1
let a = (2)
let b = (x + y, #"x + y"#)
.init(red: 0.5, green: 0.5, blue: 0.25, alpha: 1.0)
.init(_colorLiteralRed: 0.5, green: 0.5, blue: 0.25, alpha: 1.0)
let c = 9
"""
)
Expand Down