Skip to content

Commit

Permalink
Remove unused local variables despite potential side effects after re…
Browse files Browse the repository at this point in the history
…moving feature flag (#35)

* feat(test): added failing test for RemoveUnusedLocalVariables

* Enable removal of local variables with side effects

* Repeat removal of unused local variables if needed

---------

Co-authored-by: Shannon Pamperl <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent 91d1862 commit e481645
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]")
.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(
Expand Down

0 comments on commit e481645

Please sign in to comment.