Skip to content

Conversation

@Anmol202005
Copy link
Collaborator

Fixes: #124

Implemented recipe for NewlineAtEndOfFile: https://checkstyle.sourceforge.io/checks/misc/newlineatendoffile.html

@Anmol202005 Anmol202005 force-pushed the eol branch 15 times, most recently from 8b311bc to e5cc5a3 Compare October 30, 2025 19:29
@Anmol202005
Copy link
Collaborator Author

Finally got CI GREEN.

@rdiachenko pls review :)

@Anmol202005
Copy link
Collaborator Author

@romani @rdiachenko ping.

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for delay ...

items:

.gitattributes Outdated
src/test/resources/org/checkstyle/autofix/recipe/newlineatendoffile/** -text

# Force LF line endings for diff test files
*.diff text eol=lf No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add \n to end of this file,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

J result = (J) tree;

if (tree instanceof JavaSourceFile) {
final JavaSourceFile sourceFile = (JavaSourceFile) tree;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

            if (tree instanceof JavaSourceFile sourceFile) {
                

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

}

private String determineLineEnding(JavaSourceFile sourceFile) {
return switch (lineSeparatorConfig.toLowerCase()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do toLowerCase() in constructor to do this only in whole execution.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

final Path filePath = sourceFile.getSourcePath().toAbsolutePath();

if (hasViolation(filePath)) {
final String lineEnding = determineLineEnding(sourceFile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reanem lineEnding to expectedLineEnding

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

final Space eof = sourceFile.getEof();
final String lastWhitespace = eof.getLastWhitespace();

if (!lineEnding.equals(lastWhitespace)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not trust checkstyle violation ? or it is save check in case of second execution.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a check for second execution.


private boolean hasViolation(Path filePath) {
return violations.removeIf(violation -> {
return violation.getFilePath().endsWith(filePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we do removal from collection of violation ?
is this prevention of double execution ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do this since we already do a check. reverted this.

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.

Implement recipe for NewlineAtEndOfFile

2 participants