-
Notifications
You must be signed in to change notification settings - Fork 90
732: Remove trailing whitespace #733
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
Draft
dsgrieve
wants to merge
6
commits into
main
Choose a base branch
from
5958-remove-trailing-whitespace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f930408
Skeleton for RemoveTrailingWhitespace
dsgrieve c72b3b2
remove trailing whitespace using pattern match
dsgrieve f8681a2
fix formatting
dsgrieve fa52fea
rework based on better regex
dsgrieve c8d3d52
Update src/main/java/org/openrewrite/staticanalysis/RemoveTrailingWhi…
dsgrieve 08dce6e
Update src/test/java/org/openrewrite/staticanalysis/RemoveTrailingWhi…
dsgrieve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/main/java/org/openrewrite/staticanalysis/RemoveTrailingWhitespace.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.staticanalysis; | ||
|
|
||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Value; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.text.PlainText; | ||
| import org.openrewrite.text.PlainTextVisitor; | ||
|
|
||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| @EqualsAndHashCode(callSuper = false) | ||
| @Value | ||
| public class RemoveTrailingWhitespace extends Recipe { | ||
|
|
||
| private static final Pattern TRAILING_WHITESPACE = Pattern.compile("[ \\t]+(?=\\r?\\n|$)", Pattern.MULTILINE); | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Remove trailing whitespace from text files"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Removes trailing whitespace (spaces and tabs) from the end of lines in text files. " + | ||
| "This helps maintain clean code formatting and prevents unnecessary whitespace in version control."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new PlainTextVisitor<ExecutionContext>() { | ||
| @Override | ||
| public PlainText visitText(PlainText text, ExecutionContext ctx) { | ||
| String content = text.getText(); | ||
| Matcher matcher = TRAILING_WHITESPACE.matcher(content); | ||
| if (matcher.find()) { | ||
| String cleanedContent = matcher.replaceAll(""); | ||
| return text.withText(cleanedContent); | ||
| } | ||
| return text; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
202 changes: 202 additions & 0 deletions
202
src/test/java/org/openrewrite/staticanalysis/RemoveTrailingWhitespaceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.staticanalysis; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.openrewrite.DocumentExample; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.test.SourceSpecs.text; | ||
|
|
||
| class RemoveTrailingWhitespaceTest implements RewriteTest { | ||
|
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new RemoveTrailingWhitespace()); | ||
| } | ||
|
|
||
| @DocumentExample | ||
| @Test | ||
| void removeTrailingSpaces() { | ||
| rewriteRun( | ||
| text( | ||
| "Line with trailing spaces \n" + | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "Line without trailing spaces\n" + | ||
| "Another line with spaces \n", | ||
| "Line with trailing spaces\n" + | ||
| "Line without trailing spaces\n" + | ||
| "Another line with spaces\n" | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void removeTrailingTabs() { | ||
| rewriteRun( | ||
| text( | ||
| "Line with trailing tabs\t\t\n" + | ||
| "Line without trailing tabs\n" + | ||
| "Mixed tabs and spaces\t \n", | ||
| "Line with trailing tabs\n" + | ||
| "Line without trailing tabs\n" + | ||
| "Mixed tabs and spaces\n" | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void removeTrailingWhitespaceFromLastLine() { | ||
| rewriteRun( | ||
| text( | ||
| "First line\n" + | ||
| "Second line ", | ||
| "First line\n" + | ||
| "Second line" | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void preserveEmptyLines() { | ||
| rewriteRun( | ||
| text( | ||
| "Line 1 \n" + | ||
| "\n" + | ||
| "Line 3\t\n" + | ||
| "\n" + | ||
| "Line 5", | ||
| "Line 1\n" + | ||
| "\n" + | ||
| "Line 3\n" + | ||
| "\n" + | ||
| "Line 5" | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void handleMultipleConsecutiveWhitespaceTypes() { | ||
| rewriteRun( | ||
| text( | ||
| "Spaces then tabs \t\t\n" + | ||
| "Tabs then spaces\t\t \n" + | ||
| "Mixed whitespace \t \t \n", | ||
| "Spaces then tabs\n" + | ||
| "Tabs then spaces\n" + | ||
| "Mixed whitespace\n" | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void noChangesWhenNoTrailingWhitespace() { | ||
| rewriteRun( | ||
| text( | ||
| "Clean line 1\n" + | ||
| "Clean line 2\n" + | ||
| "Clean line 3" | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void handleEmptyFile() { | ||
| rewriteRun( | ||
| text("") | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void handleLinesWithOnlyWhitespace() { | ||
| rewriteRun( | ||
| text( | ||
| "line with content\n" + | ||
| " \n" + | ||
| "another line", | ||
| "line with content\n" + | ||
| "\n" + | ||
| "another line" | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void preserveIndentationWhitespace() { | ||
| rewriteRun( | ||
| text( | ||
| " Indented line \n" + | ||
| "\tTab indented line\t\n" + | ||
| " Mixed indentation\t ", | ||
| " Indented line\n" + | ||
| "\tTab indented line\n" + | ||
| " Mixed indentation" | ||
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
dsgrieve marked this conversation as resolved.
Show resolved
Hide resolved
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void handleWindowsLineEndings() { | ||
| rewriteRun( | ||
| text( | ||
| "Windows line 1 \r\n" + | ||
| "Windows line 2\t\r\n" + | ||
| "Windows line 3", | ||
| "Windows line 1\r\n" + | ||
| "Windows line 2\r\n" + | ||
| "Windows line 3" | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void handleMacLineEndings() { | ||
| rewriteRun( | ||
| text( | ||
| "Mac line 1 \r" + | ||
| "Mac line 2\t\r" + | ||
| "Mac line 3", | ||
| "Mac line 1\r" + | ||
| "Mac line 2\r" + | ||
| "Mac line 3" | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void handleLargeAmountOfTrailingWhitespace() { | ||
| StringBuilder input = new StringBuilder(); | ||
| StringBuilder expected = new StringBuilder(); | ||
|
|
||
| // Create lines with varying amounts of trailing whitespace | ||
| for (int i = 1; i <= 10; i++) { | ||
| input.append("Line ").append(i).repeat(" ", i); | ||
| expected.append("Line ").append(i); | ||
|
|
||
| if (i < 10) { | ||
| input.append("\n"); | ||
| expected.append("\n"); | ||
| } | ||
| } | ||
|
|
||
| rewriteRun( | ||
| text( | ||
| input.toString(), | ||
| expected.toString() | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A PlainTextVisitor does not act on Java files. If you would want to "downcast" the Java lst as a text lst, you could build a generic TreeVisitor that does that conversion.
In this case though you do not want to do that as you will loose all rich information of the lst and every recipe that should run on java files after this one will not make any changes as the Lst's at that moment are no longer JavaSourceFiles but PlainText Sourcefiles causing the isAcceptable of the JavaVisitor to skip the converted files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see https://moderneinc.slack.com/archives/C088P8067NH/p1757442137104209