File tree Expand file tree Collapse file tree 3 files changed +70
-1
lines changed
Tests/SwiftSyntaxMacroExpansionTest Expand file tree Collapse file tree 3 files changed +70
-1
lines changed Original file line number Diff line number Diff line change @@ -152,6 +152,14 @@ extension Parser {
152
152
}
153
153
154
154
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
+
155
163
if remainingTokens. isEmpty {
156
164
return into
157
165
}
Original file line number Diff line number Diff line change @@ -328,7 +328,8 @@ extension RawSyntax {
328
328
/// - Parameters:
329
329
/// - trailingTrivia: The trivia to attach.
330
330
/// - 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 ? {
332
333
switch view {
333
334
case . token( let tokenView) :
334
335
return . makeMaterializedToken(
Original file line number Diff line number Diff line change @@ -299,4 +299,64 @@ final class MemberMacroTests: XCTestCase {
299
299
]
300
300
)
301
301
}
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
+ }
302
362
}
You can’t perform that action at this time.
0 commit comments