diff --git a/src/main/java/org/openrewrite/featureflags/RemoveBooleanFlag.java b/src/main/java/org/openrewrite/featureflags/RemoveBooleanFlag.java index 2b3109d..460dc78 100644 --- a/src/main/java/org/openrewrite/featureflags/RemoveBooleanFlag.java +++ b/src/main/java/org/openrewrite/featureflags/RemoveBooleanFlag.java @@ -70,7 +70,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); if (methodMatcher.matches(mi) && isFeatureKey(mi.getArguments().get(0))) { doAfterVisit(new SimplifyConstantIfBranchExecution().getVisitor()); - doAfterVisit(new RemoveUnusedLocalVariables(null, null).getVisitor()); + doAfterVisit(Repeat.repeatUntilStable(new RemoveUnusedLocalVariables(null, true).getVisitor(), 3)); doAfterVisit(new RemoveUnusedPrivateFields().getVisitor()); J.Literal literal = new J.Literal(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, replacementValue, String.valueOf(replacementValue), null, JavaType.Primitive.Boolean); return literal.withPrefix(mi.getPrefix()); diff --git a/src/main/java/org/openrewrite/featureflags/RemoveStringFlag.java b/src/main/java/org/openrewrite/featureflags/RemoveStringFlag.java index a3e9aec..292577f 100644 --- a/src/main/java/org/openrewrite/featureflags/RemoveStringFlag.java +++ b/src/main/java/org/openrewrite/featureflags/RemoveStringFlag.java @@ -70,7 +70,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); if (methodMatcher.matches(mi) && isFeatureKey(mi.getArguments().get(0))) { doAfterVisit(new SimplifyConstantIfBranchExecution().getVisitor()); - doAfterVisit(new RemoveUnusedLocalVariables(null, null).getVisitor()); + doAfterVisit(Repeat.repeatUntilStable(new RemoveUnusedLocalVariables(null, true).getVisitor(), 3)); doAfterVisit(new RemoveUnusedPrivateFields().getVisitor()); J.Literal literal = new J.Literal(Tree.randomId(), Space.SINGLE_SPACE, Markers.EMPTY, replacementValue, '"' + replacementValue + '"', null, JavaType.Primitive.String); return literal.withPrefix(mi.getPrefix()); diff --git a/src/test/java/org/openrewrite/featureflags/launchdarkly/RemoveBoolVariationTest.java b/src/test/java/org/openrewrite/featureflags/launchdarkly/RemoveBoolVariationTest.java index b2bd753..ed4f7cc 100644 --- a/src/test/java/org/openrewrite/featureflags/launchdarkly/RemoveBoolVariationTest.java +++ b/src/test/java/org/openrewrite/featureflags/launchdarkly/RemoveBoolVariationTest.java @@ -178,6 +178,87 @@ void bar() { ); } + @Test + void removeUnusedLDContextWithBuilder() { + rewriteRun( + // language=java + java( + """ + import com.launchdarkly.sdk.*; + import com.launchdarkly.sdk.server.*; + class Foo { + LDClient client = new LDClient("sdk-key-123abc"); + void bar() { + LDContext context = LDContext.builder("context-key-123abc") + .name("Sandy") + .build(); + if (client.boolVariation("flag-key-123abc", context, false)) { + // Application code to show the feature + System.out.println("Feature is on"); + } + else { + // The code to run if the feature is off + System.out.println("Feature is off"); + } + } + } + """, + """ + import com.launchdarkly.sdk.server.*; + class Foo { + LDClient client = new LDClient("sdk-key-123abc"); + void bar() { + // Application code to show the feature + System.out.println("Feature is on"); + } + } + """ + ) + ); + } + + @Test + void removeUnusedLDContextWithBuilderContext() { + rewriteRun( + // language=java + java( + """ + import com.launchdarkly.sdk.*; + import com.launchdarkly.sdk.server.*; + class Foo { + LDClient client = new LDClient("sdk-key-123abc"); + void bar() { + LDContext ldContext = LDContext.create("newValue"); + LDContext context = LDContext.builderFromContext(ldContext) + .anonymous(false) + .name("name") + .set("email", "email@gmail.com") + .build(); + if (client.boolVariation("flag-key-123abc", context, false)) { + // Application code to show the feature + System.out.println("Feature is on"); + } + else { + // The code to run if the feature is off + System.out.println("Feature is off"); + } + } + } + """, + """ + import com.launchdarkly.sdk.server.*; + class Foo { + LDClient client = new LDClient("sdk-key-123abc"); + void bar() { + // Application code to show the feature + System.out.println("Feature is on"); + } + } + """ + ) + ); + } + @Test void enablePermanentlyWithParameters() { rewriteRun(