-
Notifications
You must be signed in to change notification settings - Fork 6
Issue #124: Implement recipe for NewlineAtEndOfFile #125
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
Open
Anmol202005
wants to merge
1
commit into
checkstyle:main
Choose a base branch
from
Anmol202005:eol
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.
+291
−14
Open
Changes from all commits
Commits
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
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,5 @@ | ||
| # Test resources that need specific eol | ||
| src/test/resources/org/checkstyle/autofix/recipe/newlineatendoffile/** -text | ||
|
|
||
| # Force LF line endings for diff test files | ||
| *.diff text eol=lf |
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
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
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
151 changes: 151 additions & 0 deletions
151
src/main/java/org/checkstyle/autofix/recipe/NewlineAtEndOfFile.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,151 @@ | ||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. | ||
| // Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // 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.checkstyle.autofix.recipe; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.UnaryOperator; | ||
|
|
||
| import org.checkstyle.autofix.parser.CheckConfiguration; | ||
| import org.checkstyle.autofix.parser.CheckstyleViolation; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.Tree; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.format.AutodetectGeneralFormatStyle; | ||
| import org.openrewrite.java.tree.Comment; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaSourceFile; | ||
| import org.openrewrite.java.tree.Space; | ||
| import org.openrewrite.style.GeneralFormatStyle; | ||
| import org.openrewrite.style.Style; | ||
|
|
||
| public class NewlineAtEndOfFile extends Recipe { | ||
|
|
||
| private final List<CheckstyleViolation> violations; | ||
| private final CheckConfiguration config; | ||
|
|
||
| public NewlineAtEndOfFile(List<CheckstyleViolation> violations, CheckConfiguration config) { | ||
| this.violations = violations; | ||
| this.config = config; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "End files with a single newline"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Some tools work better when files end with an empty line."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| final String lineSeparator = config.getProperty("lineSeparator"); | ||
| return new NewLineAtEndOfFileVisitor(violations, lineSeparator); | ||
| } | ||
|
|
||
| private static class NewLineAtEndOfFileVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
| private final List<CheckstyleViolation> violations; | ||
| private final String lineSeparatorConfig; | ||
|
|
||
| NewLineAtEndOfFileVisitor(List<CheckstyleViolation> violations, | ||
| String lineSeparatorConfig) { | ||
| this.violations = violations; | ||
| this.lineSeparatorConfig = lineSeparatorConfig.toLowerCase(); | ||
| } | ||
|
|
||
| @Override | ||
| public J visit(Tree tree, ExecutionContext executionContext) { | ||
| J result = (J) tree; | ||
|
|
||
| if (tree instanceof JavaSourceFile sourceFile) { | ||
|
|
||
| final Path filePath = sourceFile.getSourcePath().toAbsolutePath(); | ||
|
|
||
| if (hasViolation(filePath)) { | ||
| final String expectedLineEnding = determineLineEnding(sourceFile); | ||
|
|
||
| final Space eof = sourceFile.getEof(); | ||
| final String lastWhitespace = eof.getLastWhitespace(); | ||
|
|
||
| if (!expectedLineEnding.equals(lastWhitespace)) { | ||
| final List<Comment> comments = eof.getComments(); | ||
| if (comments.isEmpty()) { | ||
| result = sourceFile.withEof(Space.format(expectedLineEnding)); | ||
| } | ||
| else { | ||
| result = sourceFile.withEof(sourceFile.getEof().withComments(mapLast( | ||
| comments, comment -> comment.withSuffix(expectedLineEnding)))); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private String determineLineEnding(JavaSourceFile sourceFile) { | ||
| return switch (lineSeparatorConfig) { | ||
| case "lf" -> "\n"; | ||
| case "crlf" -> "\r\n"; | ||
| case "cr" -> "\r"; | ||
| case "system" -> System.lineSeparator(); | ||
| case "lf_cr_crlf" -> getAutodetectedLineEnding(sourceFile); | ||
| default -> { | ||
| throw new IllegalStateException("Unexpected value: " + lineSeparatorConfig); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private String getAutodetectedLineEnding(JavaSourceFile sourceFile) { | ||
| final GeneralFormatStyle generalFormatStyle = | ||
| Style.from(GeneralFormatStyle.class, sourceFile, () -> { | ||
| return AutodetectGeneralFormatStyle | ||
| .autodetectGeneralFormatStyle(sourceFile); | ||
| }); | ||
| return generalFormatStyle.newLine(); | ||
| } | ||
|
|
||
| private static List<Comment> mapLast(List<Comment> comments, | ||
| UnaryOperator<Comment> mapper) { | ||
| List<Comment> result = comments; | ||
|
|
||
| if (comments != null && !comments.isEmpty()) { | ||
| final int lastIndex = comments.size() - 1; | ||
| final Comment last = comments.get(lastIndex); | ||
| final Comment newLast = mapper.apply(last); | ||
|
|
||
| if (last != newLast) { | ||
| result = new ArrayList<>(comments); | ||
| result.set(lastIndex, newLast); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private boolean hasViolation(Path filePath) { | ||
| return violations.stream() | ||
| .anyMatch(violation -> violation.getFilePath().endsWith(filePath)); | ||
| } | ||
| } | ||
| } | ||
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
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
37 changes: 37 additions & 0 deletions
37
src/test/java/org/checkstyle/autofix/recipe/NewLineAtEndOfFileTest.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,37 @@ | ||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. | ||
| // Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // 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.checkstyle.autofix.recipe; | ||
|
|
||
| import org.checkstyle.autofix.parser.ReportParser; | ||
|
|
||
| public class NewLineAtEndOfFileTest extends AbstractRecipeTestSupport { | ||
| @Override | ||
| protected String getSubpackage() { | ||
| return "newlineatendoffile"; | ||
| } | ||
|
|
||
| @RecipeTest | ||
| void newLineCrlf(ReportParser parser) throws Exception { | ||
| verify(parser, "NewlineAtEndOfFileCrlf"); | ||
| } | ||
|
|
||
| @RecipeTest | ||
| void noNewLine(ReportParser parser) throws Exception { | ||
| verify(parser, "NewlineAtEndOfFileNoNewLine"); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
.../autofix/recipe/newlineatendoffile/newlineatendoffilecrlf/DiffNewlineAtEndOfFileCrlf.diff
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,14 @@ | ||
| --- src/test/resources/org/checkstyle/autofix/recipe/newlineatendoffile/newlineatendoffilecrlf/InputNewlineAtEndOfFileCrlf.java | ||
| +++ src/test/resources/org/checkstyle/autofix/recipe/newlineatendoffile/newlineatendoffilecrlf/OutputNewlineAtEndOfFileCrlf.java | ||
| @@ -4,9 +4,8 @@ | ||
| fileExtensions = (default)"" | ||
|
|
||
| */ | ||
| -// violation 6 lines above 'Expected line ending for file is LF(\\n), but CRLF(\\r\\n) is detected.' | ||
|
|
||
| package org.checkstyle.autofix.recipe.newlineatendoffile.newlineatendoffilecrlf; | ||
|
|
||
| -public class InputNewlineAtEndOfFileCrlf { | ||
| -} | ||
| +public class OutputNewlineAtEndOfFileCrlf { | ||
| +} |
12 changes: 12 additions & 0 deletions
12
...autofix/recipe/newlineatendoffile/newlineatendoffilecrlf/InputNewlineAtEndOfFileCrlf.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,12 @@ | ||
| /* | ||
| com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck | ||
| lineSeparator = lf | ||
| fileExtensions = (default)"" | ||
|
|
||
| */ | ||
| // violation 6 lines above 'Expected line ending for file is LF(\\n), but CRLF(\\r\\n) is detected.' | ||
|
|
||
| package org.checkstyle.autofix.recipe.newlineatendoffile.newlineatendoffilecrlf; | ||
|
|
||
| public class InputNewlineAtEndOfFileCrlf { | ||
| } |
11 changes: 11 additions & 0 deletions
11
...utofix/recipe/newlineatendoffile/newlineatendoffilecrlf/OutputNewlineAtEndOfFileCrlf.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,11 @@ | ||
| /* | ||
| com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck | ||
| lineSeparator = lf | ||
| fileExtensions = (default)"" | ||
|
|
||
| */ | ||
|
|
||
| package org.checkstyle.autofix.recipe.newlineatendoffile.newlineatendoffilecrlf; | ||
|
|
||
| public class OutputNewlineAtEndOfFileCrlf { | ||
| } |
16 changes: 16 additions & 0 deletions
16
...ecipe/newlineatendoffile/newlineatendoffilenonewline/DiffNewlineAtEndOfFileNoNewLine.diff
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,16 @@ | ||
| --- src/test/resources/org/checkstyle/autofix/recipe/newlineatendoffile/newlineatendoffilenonewline/InputNewlineAtEndOfFileNoNewLine.java | ||
| +++ src/test/resources/org/checkstyle/autofix/recipe/newlineatendoffile/newlineatendoffilenonewline/OutputNewlineAtEndOfFileNoNewLine.java | ||
| @@ -4,10 +4,9 @@ | ||
| fileExtensions = (default)"" | ||
|
|
||
| */ | ||
| -// violation 6 lines above 'File does not end with a newline.' | ||
|
|
||
| package org.checkstyle.autofix.recipe.newlineatendoffile.newlineatendoffilenonewline; | ||
|
|
||
| -public interface InputNewlineAtEndOfFileNoNewLine | ||
| +public interface OutputNewlineAtEndOfFileNoNewLine | ||
| { | ||
| -} | ||
| \ No newline at end of file | ||
| +} |
13 changes: 13 additions & 0 deletions
13
...cipe/newlineatendoffile/newlineatendoffilenonewline/InputNewlineAtEndOfFileNoNewLine.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,13 @@ | ||
| /* | ||
| com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck | ||
| lineSeparator = LF | ||
| fileExtensions = (default)"" | ||
|
|
||
| */ | ||
| // violation 6 lines above 'File does not end with a newline.' | ||
|
|
||
| package org.checkstyle.autofix.recipe.newlineatendoffile.newlineatendoffilenonewline; | ||
|
|
||
| public interface InputNewlineAtEndOfFileNoNewLine | ||
| { | ||
| } |
12 changes: 12 additions & 0 deletions
12
...ipe/newlineatendoffile/newlineatendoffilenonewline/OutputNewlineAtEndOfFileNoNewLine.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,12 @@ | ||
| /* | ||
| com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck | ||
| lineSeparator = LF | ||
| fileExtensions = (default)"" | ||
|
|
||
| */ | ||
|
|
||
| package org.checkstyle.autofix.recipe.newlineatendoffile.newlineatendoffilenonewline; | ||
|
|
||
| public interface OutputNewlineAtEndOfFileNoNewLine | ||
| { | ||
| } |
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.
does it check comments at the end of file ?
if yes, we do not have test input for this, please add.
if no, please share details with me, I need you help here.