Skip to content

Commit 080033a

Browse files
committed
Rewrite comments for correctness and clarity in AttributeRemover
- Improved comment clarity and accuracy regarding the conditions for not leaving an empty line after removing an attribute. - Corrected the discrepancy between the comment and the implementation concerning the presence of leading trivia in the next token. - Refined the explanation about the significance of trailing trivia when attributes are not separated by trivia.
1 parent 250e9f2 commit 080033a

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,28 +398,33 @@ private class AttributeRemover: SyntaxRewriter {
398398
for case .attribute(let attribute) in node {
399399
if attributesToRemove.contains(attribute) {
400400
var leadingTrivia = attribute.leadingTrivia
401+
402+
// Don't leave behind an empty line when the attribute being removed is on its own line,
403+
// based on the following conditions:
404+
// - Leading trivia ends with a newline followed by arbitrary number of spaces or tabs
405+
// - All leading trivia pieces after the last newline are just whitespace, ensuring
406+
// there are no comments or other non-whitespace characters on the same line
407+
// preceding the attribute.
408+
// - There is no trailing trivia and the next token has leading trivia.
401409
if let lastNewline = leadingTrivia.pieces.lastIndex(where: \.isNewline),
402410
leadingTrivia.pieces[lastNewline...].allSatisfy(\.isWhitespace),
403411
attribute.trailingTrivia.isEmpty,
404412
let nextToken = attribute.nextToken(viewMode: .sourceAccurate),
405413
!nextToken.leadingTrivia.isEmpty
406414
{
407-
// If the attribute is on its own line based on the following conditions,
408-
// remove the newline from it so we don’t end up with an empty line
409-
// - Leading trivia ends with a newline followed by arbitrary number of spaces or tabs
410-
// - There is no trailing trivia and the next token has no leading trivia
411415
leadingTrivia = Trivia(pieces: leadingTrivia.pieces[..<lastNewline])
412416
}
417+
413418
// Drop any spaces or tabs from the trailing trivia because there’s no
414419
// more attribute they need to separate.
415420
let trailingTrivia = attribute.trailingTrivia.trimmingPrefix(while: \.isSpaceOrTab)
416421
triviaToAttachToNextToken += leadingTrivia + trailingTrivia
417422

418-
// If the attribute is not separated by the previous attribute using trivia, e.g.
419-
// `@First@Second var x: Int` (yes, that's valid Swift), and removing the `@Second`
420-
// attribute then dropping all trivia of this attribute would join `@First` and `var`
421-
// without any trivia in between, which is invalid. In this case the trailing trivia of the
422-
// attribute is significant and we need to keep it.
423+
// If the attribute is not separated from the previous attribute by trivia, as in
424+
// `@First@Second var x: Int` (yes, that's valid Swift), removing the `@Second`
425+
// attribute and dropping all its trivia would cause `@First` and `var` to join
426+
// without any trivia in between, which is invalid. In such cases, the trailing trivia
427+
// of the attribute is significant and must be retained.
423428
if triviaToAttachToNextToken.isEmpty,
424429
let previousToken = attribute.previousToken(viewMode: .sourceAccurate),
425430
previousToken.trailingTrivia.isEmpty

0 commit comments

Comments
 (0)