Skip to content

Commit

Permalink
Rewrite rewrites Rewrite (openrewrite#3149)
Browse files Browse the repository at this point in the history
This adds an EndOfLineAtEndOfFile recipe, this recipe is applied
to this recpie using the rewrite plugin.

Dogfood 🎉

Signed-off-by: Jonathan Leitschuh <[email protected]>
  • Loading branch information
JLLeitschuh authored Apr 21, 2023
1 parent fbe00fa commit ca1cdbd
Show file tree
Hide file tree
Showing 91 changed files with 236 additions and 86 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ out/
.DS_Store
**/node_modules/
IDE.properties
# ASDF https://asdf-vm.com/
.tool-versions
10 changes: 10 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
plugins {
id("org.openrewrite.build.root") version("latest.release")
id("org.openrewrite.build.java-base") version("latest.release")
id("org.openrewrite.rewrite") version("latest.release")
}

repositories {
mavenCentral()
}

dependencies {
rewrite(project(":rewrite-core"))
}

rewrite {
failOnDryRunResults = true
activeRecipe("org.openrewrite.self.Rewrite")
}


allprojects {
group = "org.openrewrite"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <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.text;

import org.openrewrite.*;
import org.openrewrite.binary.Binary;
import org.openrewrite.quark.Quark;
import org.openrewrite.remote.Remote;

public class EndOfLineAtEndOfFile extends Recipe {

@Override
public String getDisplayName() {
return "End of Line @ End of File (EOL @ EOF)";
}

@Override
public String getDescription() {
return "Ensure that the file ends with the newline character.\n\n" +
"*Note*: If this recipe modifies a file, it converts the file into plain text. " +
"As such, this recipe should be run after any recipe that modifies the language-specific LST.";
}

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree visitSourceFile(SourceFile sourceFile, ExecutionContext executionContext) {
if (sourceFile instanceof Quark || sourceFile instanceof Remote || sourceFile instanceof Binary) {
return sourceFile;
}
PlainText plainText = PlainTextParser.convert(sourceFile);
boolean whitespaceContainsCRLF = plainText.getText().contains("\r\n");
if (!plainText.getText().endsWith("\n")) {
if (whitespaceContainsCRLF) {
return plainText.withText(plainText.getText() + "\r\n");
} else {
return plainText.withText(plainText.getText() + '\n');
}
}
return sourceFile;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ DefaultCodeFormatter has been compiled by a more recent version of the Java Runt
void determineRequiredClassFileVersion(String logFileContents, String expected) {
assertThat(FailureLogAnalyzer.requiredJavaVersion(logFileContents)).isEqualTo(expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ void twoResultsOnly() {
)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <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.text;

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.test.SourceSpecs.text;

public class EndOfLineAtEndOfFileTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new EndOfLineAtEndOfFile());
}

@Test
void nonTxtExtension() {
rewriteRun(
text(
"""
This is text.
""",
"""
This is text.
""",
spec -> spec.path("test.yml")
)
);
}

@Test
void defaultText() {
rewriteRun(
text(
"""
This is text.
""",
"""
This is text.
"""
)
);
}

@Test
void fileContainingCLRFWillEndWithCLRF() {
rewriteRun(
text(
"Test\r\nLine",
"Test\r\nLine\r\n\n" // The last newline gets removed by the test framework
)
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,4 @@ public void accept(CompilerConfiguration config) {
imports.addStarImports(DEFAULT_IMPORTS);
config.addCompilationCustomizers(imports);
}
}
}
2 changes: 1 addition & 1 deletion rewrite-hcl/src/main/antlr/input.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"abc${"a${b}"}"
"abc${"a${b}"}"
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,4 @@ private void HTemplateLiteral_action(RuleContext _localctx, int actionIndex) {
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3501,4 +3501,4 @@ private boolean exprTerm_sempred(ExprTermContext _localctx, int predIndex) {
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,4 @@ public class HCLParserBaseListener implements HCLParserListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void visitErrorNode(ErrorNode node) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,4 @@ public class HCLParserBaseVisitor<T> extends AbstractParseTreeVisitor<T> impleme
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitTemplateInterpolation(HCLParser.TemplateInterpolationContext ctx) { return visitChildren(ctx); }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,4 @@ public interface HCLParserListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitTemplateInterpolation(HCLParser.TemplateInterpolationContext ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,4 @@ public interface HCLParserVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitTemplateInterpolation(HCLParser.TemplateInterpolationContext ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,4 @@ public JsonPathLexer(CharStream input) {
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1652,4 +1652,4 @@ private boolean binaryExpression_sempred(BinaryExpressionContext _localctx, int
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,4 @@ public class JsonPathParserBaseListener implements JsonPathParserListener {
* <p>The default implementation does nothing.</p>
*/
@Override public void visitErrorNode(ErrorNode node) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,4 @@ public class JsonPathParserBaseVisitor<T> extends AbstractParseTreeVisitor<T> im
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitIndexes(JsonPathParser.IndexesContext ctx) { return visitChildren(ctx); }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ public interface JsonPathParserListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitIndexes(JsonPathParser.IndexesContext ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ public interface JsonPathParserVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitIndexes(JsonPathParser.IndexesContext ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ void foo() {
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,4 @@ static void assertTypeAttribution(J sf) {
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,4 @@ static JavaTemplate.Builder getTemplate(JavaVisitor<?> visitor) {
return JavaTemplate.builder(visitor::getCursor, "1 == #{any(int)}");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,4 @@ public class A {
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,4 @@ public String getSubeventNameForCategoryGroupId(final String subeventName, final
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -813,4 +813,4 @@ String test(Object o) {
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,4 @@ public J.Empty visitEmpty(J.Empty empty, J j) {
return empty;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ public static <J2 extends J> J2 buildPart(@Language("java") String codeToProvide

return parts.get(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,4 @@ private boolean hasFinalModifiers(final List<Statement> parameters) {
private boolean isEmpty(final List<Statement> parameters) {
return parameters.size() == 1 && (parameters.get(0) instanceof Empty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -467,4 +467,4 @@ private boolean JavaLetterOrDigit_sempred(RuleContext _localctx, int predIndex)
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -815,4 +815,4 @@ public final LiteralContext literal() throws RecognitionException {
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@ public class AnnotationSignatureParserBaseListener implements AnnotationSignatur
* <p>The default implementation does nothing.</p>
*/
@Override public void visitErrorNode(ErrorNode node) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ public class AnnotationSignatureParserBaseVisitor<T> extends AbstractParseTreeVi
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitLiteral(AnnotationSignatureParser.LiteralContext ctx) { return visitChildren(ctx); }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ public interface AnnotationSignatureParserListener extends ParseTreeListener {
* @param ctx the parse tree
*/
void exitLiteral(AnnotationSignatureParser.LiteralContext ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ public interface AnnotationSignatureParserVisitor<T> extends ParseTreeVisitor<T>
* @return the visitor result
*/
T visitLiteral(AnnotationSignatureParser.LiteralContext ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,4 @@ private boolean JavaLetterOrDigit_sempred(RuleContext _localctx, int predIndex)
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1301,4 +1301,4 @@ private boolean formalTypePattern_sempred(FormalTypePatternContext _localctx, in
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@ public class MethodSignatureParserBaseListener implements MethodSignatureParserL
* <p>The default implementation does nothing.</p>
*/
@Override public void visitErrorNode(ErrorNode node) { }
}
}
Loading

0 comments on commit ca1cdbd

Please sign in to comment.