|
| 1 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. |
| 3 | +// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 17 | + |
| 18 | +package org.checkstyle.autofix.recipe; |
| 19 | + |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.function.UnaryOperator; |
| 24 | + |
| 25 | +import org.checkstyle.autofix.parser.CheckConfiguration; |
| 26 | +import org.checkstyle.autofix.parser.CheckstyleViolation; |
| 27 | +import org.openrewrite.ExecutionContext; |
| 28 | +import org.openrewrite.Recipe; |
| 29 | +import org.openrewrite.Tree; |
| 30 | +import org.openrewrite.TreeVisitor; |
| 31 | +import org.openrewrite.java.JavaIsoVisitor; |
| 32 | +import org.openrewrite.java.format.AutodetectGeneralFormatStyle; |
| 33 | +import org.openrewrite.java.tree.Comment; |
| 34 | +import org.openrewrite.java.tree.J; |
| 35 | +import org.openrewrite.java.tree.JavaSourceFile; |
| 36 | +import org.openrewrite.java.tree.Space; |
| 37 | +import org.openrewrite.style.GeneralFormatStyle; |
| 38 | +import org.openrewrite.style.Style; |
| 39 | + |
| 40 | +public class NewlineAtEndOfFile extends Recipe { |
| 41 | + |
| 42 | + private final List<CheckstyleViolation> violations; |
| 43 | + private final CheckConfiguration config; |
| 44 | + |
| 45 | + public NewlineAtEndOfFile(List<CheckstyleViolation> violations, CheckConfiguration config) { |
| 46 | + this.violations = violations; |
| 47 | + this.config = config; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getDisplayName() { |
| 52 | + return "End files with a single newline"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String getDescription() { |
| 57 | + return "Some tools work better when files end with an empty line."; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 62 | + final String lineSeparator = config.getProperty("lineSeparator"); |
| 63 | + return new NewLineAtEndOfFileVisitor(violations, lineSeparator); |
| 64 | + } |
| 65 | + |
| 66 | + private static class NewLineAtEndOfFileVisitor extends JavaIsoVisitor<ExecutionContext> { |
| 67 | + private final List<CheckstyleViolation> violations; |
| 68 | + private final String lineSeparatorConfig; |
| 69 | + |
| 70 | + NewLineAtEndOfFileVisitor(List<CheckstyleViolation> violations, |
| 71 | + String lineSeparatorConfig) { |
| 72 | + this.violations = violations; |
| 73 | + this.lineSeparatorConfig = lineSeparatorConfig.toLowerCase(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public J visit(Tree tree, ExecutionContext executionContext) { |
| 78 | + J result = (J) tree; |
| 79 | + |
| 80 | + if (tree instanceof JavaSourceFile sourceFile) { |
| 81 | + |
| 82 | + final Path filePath = sourceFile.getSourcePath().toAbsolutePath(); |
| 83 | + |
| 84 | + if (hasViolation(filePath)) { |
| 85 | + final String expectedLineEnding = determineLineEnding(sourceFile); |
| 86 | + |
| 87 | + final Space eof = sourceFile.getEof(); |
| 88 | + final String lastWhitespace = eof.getLastWhitespace(); |
| 89 | + |
| 90 | + if (!expectedLineEnding.equals(lastWhitespace)) { |
| 91 | + final List<Comment> comments = eof.getComments(); |
| 92 | + if (comments.isEmpty()) { |
| 93 | + result = sourceFile.withEof(Space.format(expectedLineEnding)); |
| 94 | + } |
| 95 | + else { |
| 96 | + result = sourceFile.withEof(sourceFile.getEof().withComments(mapLast( |
| 97 | + comments, comment -> comment.withSuffix(expectedLineEnding)))); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return result; |
| 104 | + } |
| 105 | + |
| 106 | + private String determineLineEnding(JavaSourceFile sourceFile) { |
| 107 | + return switch (lineSeparatorConfig) { |
| 108 | + case "lf" -> "\n"; |
| 109 | + case "crlf" -> "\r\n"; |
| 110 | + case "cr" -> "\r"; |
| 111 | + case "system" -> System.lineSeparator(); |
| 112 | + case "lf_cr_crlf" -> getAutodetectedLineEnding(sourceFile); |
| 113 | + default -> { |
| 114 | + throw new IllegalStateException("Unexpected value: " + lineSeparatorConfig); |
| 115 | + } |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + private String getAutodetectedLineEnding(JavaSourceFile sourceFile) { |
| 120 | + final GeneralFormatStyle generalFormatStyle = |
| 121 | + Style.from(GeneralFormatStyle.class, sourceFile, () -> { |
| 122 | + return AutodetectGeneralFormatStyle |
| 123 | + .autodetectGeneralFormatStyle(sourceFile); |
| 124 | + }); |
| 125 | + return generalFormatStyle.newLine(); |
| 126 | + } |
| 127 | + |
| 128 | + private static List<Comment> mapLast(List<Comment> comments, |
| 129 | + UnaryOperator<Comment> mapper) { |
| 130 | + List<Comment> result = comments; |
| 131 | + |
| 132 | + if (comments != null && !comments.isEmpty()) { |
| 133 | + final int lastIndex = comments.size() - 1; |
| 134 | + final Comment last = comments.get(lastIndex); |
| 135 | + final Comment newLast = mapper.apply(last); |
| 136 | + |
| 137 | + if (last != newLast) { |
| 138 | + result = new ArrayList<>(comments); |
| 139 | + result.set(lastIndex, newLast); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return result; |
| 144 | + } |
| 145 | + |
| 146 | + private boolean hasViolation(Path filePath) { |
| 147 | + return violations.stream() |
| 148 | + .anyMatch(violation -> violation.getFilePath().endsWith(filePath)); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments