Skip to content

Remove spurious leading newline in AttributeRemover#3335

Open
broken-circle wants to merge 1 commit into
swiftlang:mainfrom
broken-circle:attribute-remover-newline-2269
Open

Remove spurious leading newline in AttributeRemover#3335
broken-circle wants to merge 1 commit into
swiftlang:mainfrom
broken-circle:attribute-remover-newline-2269

Conversation

@broken-circle
Copy link
Copy Markdown
Contributor

Closes #2269.

AttributeRemover left behind a leading newline when the removed attribute had no leading trivia of its own. The existing cleanup logic trims back through the attribute's leading trivia up to its last newline, which works when the attribute carries the line-terminating newline (e.g. removing @Test from @State\n@Test\nvar x), but does nothing when the newline lives downstream on a following node (e.g. removing @Test from @Test\nvar x, where the newline lives on the leading trivia of var).

This PR adds a complementary forward-trim signal. When an attribute that occupies its own line is removed, but it contributes no line-terminating newline of its own, a flag is set that causes the next prependAndClearAccumulatedTrivia(to:) call to drop one leading newline from the combined triviaToAttachToNextToken + node.leadingTrivia. A kept attribute encountered before the next token consumes the flag, so the newline is dropped from whichever node the trivia attaches to.

Testing

This PR updates 5 existing tests in AttributeRemoverTests to expect the correct reduction.

When the removed attribute's leading trivia contains the newline that
ends its line, the existing backward-trim drops the newline and the
trailing indentation. But when the attribute has no leading trivia of
its own (e.g. it's the first token of the file), the line-terminating
newline lives on the following node, out of reach of the backward-trim,
and is preserved as a spurious leading newline.

Detect that case and record that the next call to attach accumulated
trivia should also drop one leading newline from the combined result,
whether it ends up on the next kept attribute or the next token.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeRemover outputs a leading newline in some cases

1 participant