Skip to content

Commit ff59a5d

Browse files
committed
Fix missing newline in member macro
1 parent b73118c commit ff59a5d

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Sources/SwiftParser/ParseSourceFile.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ extension Parser {
152152
}
153153

154154
let remainingTokens = self.consumeRemainingTokens()
155+
156+
if let endOfFileToken = self.consume(if: .endOfFile),
157+
!endOfFileToken.leadingTriviaPieces.isEmpty,
158+
let raw = into.raw.withTrailingTrivia(endOfFileToken.leadingTriviaPieces, arena: self.arena)
159+
{
160+
return R.init(raw)!
161+
}
162+
155163
if remainingTokens.isEmpty {
156164
return into
157165
}

Sources/SwiftSyntax/Raw/RawSyntax.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ extension RawSyntax {
328328
/// - Parameters:
329329
/// - trailingTrivia: The trivia to attach.
330330
/// - arena: SyntaxArena to the result node data resides.
331-
func withTrailingTrivia(_ trailingTrivia: Trivia, arena: SyntaxArena) -> RawSyntax? {
331+
@_spi(RawSyntax)
332+
public func withTrailingTrivia(_ trailingTrivia: Trivia, arena: SyntaxArena) -> RawSyntax? {
332333
switch view {
333334
case .token(let tokenView):
334335
return .makeMaterializedToken(

Tests/SwiftSyntaxMacroExpansionTest/MemberMacroTests.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,64 @@ final class MemberMacroTests: XCTestCase {
299299
]
300300
)
301301
}
302+
303+
func testAddMemberToEmptyDeclaration() {
304+
struct TestMacro: MemberMacro {
305+
static func expansion(
306+
of node: AttributeSyntax,
307+
providingMembersOf declaration: some DeclGroupSyntax,
308+
conformingTo protocols: [TypeSyntax],
309+
in context: some MacroExpansionContext
310+
) throws -> [DeclSyntax] {
311+
return [DeclSyntax("var x = 0")]
312+
}
313+
}
314+
315+
assertMacroExpansion(
316+
"""
317+
@Test
318+
struct Foo {}
319+
""",
320+
expandedSource: """
321+
struct Foo {
322+
323+
var x = 0
324+
}
325+
""",
326+
macros: [
327+
"Test": TestMacro.self
328+
],
329+
indentationWidth: indentationWidth
330+
)
331+
}
332+
333+
func testAddMemberToEmptyDeclarationWithEndingNewline() {
334+
struct TestMacro: MemberMacro {
335+
static func expansion(
336+
of node: AttributeSyntax,
337+
providingMembersOf declaration: some DeclGroupSyntax,
338+
conformingTo protocols: [TypeSyntax],
339+
in context: some MacroExpansionContext
340+
) throws -> [DeclSyntax] {
341+
return [DeclSyntax("var x = 0\n")]
342+
}
343+
}
344+
345+
assertMacroExpansion(
346+
"""
347+
@Test
348+
struct Foo {}
349+
""",
350+
expandedSource: """
351+
struct Foo {
352+
353+
var x = 0
354+
}
355+
""",
356+
macros: [
357+
"Test": TestMacro.self
358+
],
359+
indentationWidth: indentationWidth
360+
)
361+
}
302362
}

0 commit comments

Comments
 (0)