Skip to content

Commit f7ec1b2

Browse files
OpenRewrite recipe best practices (#49)
* OpenRewrite recipe best practices Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.recipes.rewrite.OpenRewriteRecipeBestPractices?organizationId=QUxML09wZW4gU291cmNlL09wZW5SZXdyaXRl Co-authored-by: Moderne <[email protected]> * Organize imports * Apply suggestions from code review * Restore original order --------- Co-authored-by: Moderne <[email protected]>
1 parent d10bea4 commit f7ec1b2

File tree

13 files changed

+383
-685
lines changed

13 files changed

+383
-685
lines changed

rewrite-csharp-remote-server/src/main/java/org/openrewrite/csharp/remote/ExternalDotNetRemotingServerEngine.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ protected ProcessBuilder configureProcess(ProcessBuilder processBuilder) {
4141
ProcessBuilder result = super.configureProcess(processBuilder);
4242
List<String> commands = result.command();
4343
commands.add("--dummy");
44-
result = result.command(commands);
45-
return result;
44+
return result.command(commands);
4645
}
4746

4847
}

rewrite-csharp-remote-server/src/test/java/org/openrewrite/csharp/remote/ProjectParsingTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ public void testParse() {
5858
pathToSolution,
5959
pathToSolution.getParent(),
6060
ctx)
61-
.forEach(sf -> {
61+
.forEach(sf ->
6262
System.out.println(sf.print(new Cursor(new Cursor(
6363
null,
64-
Cursor.ROOT_VALUE), sf)));
65-
}));
64+
Cursor.ROOT_VALUE), sf)))));
6665
}
6766
}
6867

rewrite-csharp-remote-server/src/test/java/org/openrewrite/csharp/remote/RemotingRecipeRunTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,13 @@
1818
import lombok.extern.java.Log;
1919
import org.junit.jupiter.api.Test;
2020
import org.openrewrite.*;
21-
import org.openrewrite.config.RecipeDescriptor;
2221
import org.openrewrite.csharp.tree.Cs;
2322
import org.openrewrite.internal.InMemoryLargeSourceSet;
24-
import org.openrewrite.java.JavaParser;
2523
import org.openrewrite.java.tree.J;
2624
import org.openrewrite.java.tree.JRightPadded;
2725
import org.openrewrite.java.tree.Space;
2826
import org.openrewrite.marker.Markers;
29-
import org.openrewrite.remote.InstallableRemotingRecipe;
30-
import org.openrewrite.remote.PackageSource;
31-
import org.openrewrite.remote.RemotingContext;
32-
import org.openrewrite.remote.RemotingExecutionContextView;
33-
import org.openrewrite.remote.RemotingRecipe;
34-
import org.openrewrite.remote.RemotingRecipeManager;
35-
import org.openrewrite.remote.TcpUtils;
27+
import org.openrewrite.remote.*;
3628

3729
import java.io.File;
3830
import java.net.URL;
@@ -92,7 +84,7 @@ public void testRemotingRecipeInstallAndRun() throws Exception {
9284
ctx
9385
);
9486

95-
Recipe selectedRecipe = recipes.getRecipes().stream().filter(x -> x.getName().equals("Rewrite.Recipes.FindClass"))
87+
Recipe selectedRecipe = recipes.getRecipes().stream().filter(x -> "Rewrite.Recipes.FindClass".equals(x.getName()))
9688
.findFirst()
9789
.orElseThrow();
9890

rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpReceiver.java

Lines changed: 177 additions & 360 deletions
Large diffs are not rendered by default.

rewrite-csharp-remote/src/main/java/org/openrewrite/csharp/remote/CSharpSender.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
import org.openrewrite.Cursor;
2828
import org.openrewrite.Tree;
2929
import org.openrewrite.csharp.CSharpVisitor;
30-
import org.openrewrite.csharp.tree.*;
31-
import org.openrewrite.java.*;
30+
import org.openrewrite.csharp.tree.Cs;
3231
import org.openrewrite.java.tree.*;
3332
import org.openrewrite.remote.Sender;
3433
import org.openrewrite.remote.SenderContext;
@@ -1976,8 +1975,4 @@ private static void sendSpace(Space space, SenderContext ctx) {
19761975
Extensions.sendSpace(space, ctx);
19771976
}
19781977

1979-
private static void sendComment(Comment comment, SenderContext ctx) {
1980-
Extensions.sendComment(comment, ctx);
1981-
}
1982-
19831978
}

rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpIsoVisitor.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,8 @@
2222

2323
package org.openrewrite.csharp;
2424

25-
import org.jspecify.annotations.Nullable;
26-
import org.openrewrite.*;
27-
import org.openrewrite.internal.ListUtils;
28-
import org.openrewrite.marker.Markers;
29-
import org.openrewrite.tree.*;
30-
import org.openrewrite.java.JavaVisitor;
31-
import org.openrewrite.java.tree.*;
32-
import org.openrewrite.csharp.tree.*;
33-
34-
import java.util.List;
25+
import org.openrewrite.csharp.tree.Cs;
26+
import org.openrewrite.java.tree.J;
3527

3628
public class CSharpIsoVisitor<P> extends CSharpVisitor<P>
3729
{

rewrite-csharp/src/main/java/org/openrewrite/csharp/CSharpPrinter.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.openrewrite.marker.Markers;
3333

3434
import java.util.List;
35-
import java.util.Objects;
3635
import java.util.function.UnaryOperator;
3736

3837
public class CSharpPrinter<P> extends CSharpVisitor<PrintOutputCapture<P>> {
@@ -43,9 +42,8 @@ public J visit(@Nullable Tree tree, PrintOutputCapture<P> p) {
4342
if (!(tree instanceof Cs)) {
4443
// re-route printing to the java printer
4544
return delegate.visit(tree, p);
46-
} else {
47-
return super.visit(tree, p);
4845
}
46+
return super.visit(tree, p);
4947
}
5048

5149
@Override
@@ -92,13 +90,15 @@ public J visitOperatorDeclaration(Cs.OperatorDeclaration node, PrintOutputCaptur
9290
return node;
9391
}
9492

93+
@Override
9594
public J visitPointerType(Cs.PointerType node, PrintOutputCapture<P> p) {
9695
beforeSyntax(node, CsSpace.Location.POINTER_TYPE_PREFIX, p);
9796
visitRightPadded(node.getPadding().getElementType(), CsRightPadded.Location.POINTER_TYPE_ELEMENT_TYPE, "*", p);
9897
afterSyntax(node, p);
9998
return node;
10099
}
101100

101+
@Override
102102
public Cs visitTry(Cs.Try tryable, PrintOutputCapture<P> p) {
103103
beforeSyntax(tryable, Space.Location.TRY_PREFIX, p);
104104
p.append("try");
@@ -110,6 +110,7 @@ public Cs visitTry(Cs.Try tryable, PrintOutputCapture<P> p) {
110110
}
111111

112112

113+
@Override
113114
public Cs visitTryCatch(Cs.Try.Catch catch_, PrintOutputCapture<P> p) {
114115
beforeSyntax(catch_, Space.Location.CATCH_PREFIX, p);
115116
p.append("catch");
@@ -248,6 +249,7 @@ public J visitOrderByClause(Cs.OrderByClause node, PrintOutputCapture<P> p) {
248249
return node;
249250
}
250251

252+
@Override
251253
public J visitForEachVariableLoop(Cs.ForEachVariableLoop forEachLoop, PrintOutputCapture<P> p) {
252254
beforeSyntax(forEachLoop, Space.Location.FOR_EACH_LOOP_PREFIX, p);
253255
p.append("foreach");
@@ -306,6 +308,7 @@ protected <T extends J> void visitRightPadded(@Nullable JRightPadded<T> rightPad
306308
}
307309
}
308310

311+
@Override
309312
public J visitSwitchExpression(Cs.SwitchExpression node, PrintOutputCapture<P> p) {
310313
beforeSyntax(node, CsSpace.Location.SWITCH_EXPRESSION_PREFIX, p);
311314
visitRightPadded(node.getPadding().getExpression(), CsRightPadded.Location.SWITCH_EXPRESSION_EXPRESSION, p);
@@ -315,6 +318,7 @@ public J visitSwitchExpression(Cs.SwitchExpression node, PrintOutputCapture<P> p
315318
return node;
316319
}
317320

321+
@Override
318322
public J visitSwitchStatement(Cs.SwitchStatement node, PrintOutputCapture<P> p) {
319323
beforeSyntax(node, CsSpace.Location.SWITCH_STATEMENT_PREFIX, p);
320324
p.append("switch");
@@ -324,6 +328,7 @@ public J visitSwitchStatement(Cs.SwitchStatement node, PrintOutputCapture<P> p)
324328
return node;
325329
}
326330

331+
@Override
327332
public J visitSwitchSection(Cs.SwitchSection node, PrintOutputCapture<P> p) {
328333
beforeSyntax(node, CsSpace.Location.SWITCH_SECTION_PREFIX, p);
329334
visit(node.getLabels(), p);
@@ -332,6 +337,7 @@ public J visitSwitchSection(Cs.SwitchSection node, PrintOutputCapture<P> p) {
332337
return node;
333338
}
334339

340+
@Override
335341
public J visitUnsafeStatement(Cs.UnsafeStatement node, PrintOutputCapture<P> p) {
336342
beforeSyntax(node, CsSpace.Location.UNSAFE_STATEMENT_PREFIX, p);
337343
p.append("unsafe");
@@ -349,6 +355,7 @@ public J visitCheckedExpression(Cs.CheckedExpression node, PrintOutputCapture<P>
349355
return node;
350356
}
351357

358+
@Override
352359
public J visitCheckedStatement(Cs.CheckedStatement node, PrintOutputCapture<P> p) {
353360
beforeSyntax(node, CsSpace.Location.CHECKED_STATEMENT_PREFIX, p);
354361
visit(node.getKeyword(), p);
@@ -376,6 +383,7 @@ public J visitRefType(Cs.RefType node, PrintOutputCapture<P> p) {
376383
return node;
377384
}
378385

386+
@Override
379387
public J visitRangeExpression(Cs.RangeExpression node, PrintOutputCapture<P> p) {
380388
beforeSyntax(node, CsSpace.Location.RANGE_EXPRESSION_PREFIX, p);
381389
visitRightPadded(node.getPadding().getStart(), CsRightPadded.Location.RANGE_EXPRESSION_START, p);
@@ -385,6 +393,7 @@ public J visitRangeExpression(Cs.RangeExpression node, PrintOutputCapture<P> p)
385393
return node;
386394
}
387395

396+
@Override
388397
public J visitFixedStatement(Cs.FixedStatement node, PrintOutputCapture<P> p) {
389398
beforeSyntax(node, CsSpace.Location.FIXED_STATEMENT_PREFIX, p);
390399
p.append("fixed");
@@ -394,6 +403,7 @@ public J visitFixedStatement(Cs.FixedStatement node, PrintOutputCapture<P> p) {
394403
return node;
395404
}
396405

406+
@Override
397407
public J visitLockStatement(Cs.LockStatement node, PrintOutputCapture<P> p) {
398408
beforeSyntax(node, CsSpace.Location.LOCK_STATEMENT_PREFIX, p);
399409
p.append("lock");
@@ -403,6 +413,7 @@ public J visitLockStatement(Cs.LockStatement node, PrintOutputCapture<P> p) {
403413
return node;
404414
}
405415

416+
@Override
406417
public J visitCasePatternSwitchLabel(Cs.CasePatternSwitchLabel node, PrintOutputCapture<P> p) {
407418
beforeSyntax(node, CsSpace.Location.CASE_PATTERN_SWITCH_LABEL_PREFIX, p);
408419
p.append("case");
@@ -414,6 +425,7 @@ public J visitCasePatternSwitchLabel(Cs.CasePatternSwitchLabel node, PrintOutput
414425
return node;
415426
}
416427

428+
@Override
417429
public J visitDefaultSwitchLabel(Cs.DefaultSwitchLabel node, PrintOutputCapture<P> p) {
418430
beforeSyntax(node, CsSpace.Location.DEFAULT_SWITCH_LABEL_PREFIX, p);
419431
p.append("default");
@@ -423,6 +435,7 @@ public J visitDefaultSwitchLabel(Cs.DefaultSwitchLabel node, PrintOutputCapture<
423435
return node;
424436
}
425437

438+
@Override
426439
public J visitSwitchExpressionArm(Cs.SwitchExpressionArm node, PrintOutputCapture<P> p) {
427440
beforeSyntax(node, CsSpace.Location.SWITCH_EXPRESSION_ARM_PREFIX, p);
428441
visit(node.getPattern(), p);
@@ -1374,7 +1387,7 @@ public J visitTypeParameterConstraintClause(Cs.TypeParameterConstraintClause typ
13741387
public J visitClassOrStructConstraint(Cs.ClassOrStructConstraint classOrStructConstraint, PrintOutputCapture<P> p)
13751388
{
13761389
beforeSyntax(classOrStructConstraint, CsSpace.Location.TYPE_PARAMETERS_CONSTRAINT_PREFIX, p);
1377-
p.append(classOrStructConstraint.getKind().equals(Cs.ClassOrStructConstraint.TypeKind.Class) ? "class" : "struct");
1390+
p.append(classOrStructConstraint.getKind() == Cs.ClassOrStructConstraint.TypeKind.Class ? "class" : "struct");
13781391
return classOrStructConstraint;
13791392
}
13801393

@@ -1420,9 +1433,8 @@ public J visit(@Nullable Tree tree, PrintOutputCapture<P> p) {
14201433
if (tree instanceof Cs) {
14211434
// re-route printing back up to groovy
14221435
return CSharpPrinter.this.visit(tree, p);
1423-
} else {
1424-
return super.visit(tree, p);
14251436
}
1437+
return super.visit(tree, p);
14261438
}
14271439

14281440
@Override

0 commit comments

Comments
 (0)