Skip to content

Fix Accessor Macros Attached to Properties With Trailing Comments #2722

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
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
22 changes: 17 additions & 5 deletions Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -923,25 +923,37 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
return DeclSyntax(node)
}

guard node.bindings.count == 1, let binding = node.bindings.first else {
guard node.bindings.count == 1,
var binding = node.bindings.first
else {
contextGenerator(Syntax(node)).addDiagnostics(
from: MacroApplicationError.accessorMacroOnVariableWithMultipleBindings,
node: node
)
return DeclSyntax(node)
}

let expansion = expandAccessors(of: node, existingAccessors: binding.accessorBlock)
var expansion = expandAccessors(of: node, existingAccessors: binding.accessorBlock)

if expansion.accessors != binding.accessorBlock {
if binding.accessorBlock == nil {
// remove the trailing trivia of the variable declaration and move it
// to the trailing trivia of the left brace of the newly created accessor block
expansion.accessors?.leftBrace.trailingTrivia = binding.trailingTrivia
binding.trailingTrivia = []
}

if binding.initializer != nil, expansion.expandsGetSet {
// The accessor block will have a leading space, but there will already be a
// space between the variable and the to-be-removed initializer. Remove the
// leading trivia on the accessor block so we don't double up.
node.bindings[node.bindings.startIndex].accessorBlock = expansion.accessors?.with(\.leadingTrivia, [])
node.bindings[node.bindings.startIndex].initializer = nil
binding.accessorBlock = expansion.accessors?.with(\.leadingTrivia, [])
binding.initializer = nil
} else {
node.bindings[node.bindings.startIndex].accessorBlock = expansion.accessors
binding.accessorBlock = expansion.accessors
}

node.bindings = [binding]
}

return DeclSyntax(node)
Expand Down
121 changes: 121 additions & 0 deletions Tests/SwiftSyntaxMacroExpansionTest/AccessorMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,127 @@ fileprivate struct ConstantOneGetter: AccessorMacro {
final class AccessorMacroTests: XCTestCase {
private let indentationWidth: Trivia = .spaces(2)

func testAccessorOnVariableDeclWithTrailingLineCommentAndNoAccessorBlock() {
assertMacroExpansion(
"""
@constantOne
var x: Int /*1*/ // hello
""",
expandedSource: """
var x: Int { /*1*/ // hello
get {
return 1
}
}
""",
macros: ["constantOne": ConstantOneGetter.self],
indentationWidth: indentationWidth
)

assertMacroExpansion(
"""
@constantOne
var x: Int /// hello
""",
expandedSource: """
var x: Int { /// hello
get {
return 1
}
}
""",
macros: ["constantOne": ConstantOneGetter.self],
indentationWidth: indentationWidth
)

assertMacroExpansion(
"""
@constantOne
var x: Int = 1 /// hello
""",
expandedSource: """
var x: Int { /// hello
get {
return 1
}
}
""",
macros: ["constantOne": ConstantOneGetter.self],
indentationWidth: indentationWidth
)

assertMacroExpansion(
"""
@constantOne
var x /// hello
""",
expandedSource: """
var x { /// hello
get {
return 1
}
}
""",
macros: ["constantOne": ConstantOneGetter.self],
indentationWidth: indentationWidth
)
}

func testAccessorOnVariableDeclWithTrailingLineCommentAndAccessorBlock() {
assertMacroExpansion(
"""
@constantOne
var x: Int /*h*/ { // hello
1
}
""",
expandedSource: """
var x: Int /*h*/ { // hello
get {
1
}
get {
return 1
}
}
""",
macros: ["constantOne": ConstantOneGetter.self],
indentationWidth: indentationWidth
)
}

func testAccessorOnVariableDeclWithTrailingCommentsAndSingleLineGetterExpansion() {
struct ConstantOneSingleLineGetter: AccessorMacro {
static let formatMode: FormatMode = .disabled

static func expansion(
of node: AttributeSyntax,
providingAccessorsOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [AccessorDeclSyntax] {
return [
"""
get { 1 }
"""
]
}
}

assertMacroExpansion(
"""
@constantOne
var x: Int // hello
""",
expandedSource: """
var x: Int { // hello
get { 1 }
}
""",
macros: ["constantOne": ConstantOneSingleLineGetter.self],
indentationWidth: indentationWidth
)
}

func testAccessorOnVariableDeclWithExistingGetter() {
assertMacroExpansion(
"""
Expand Down