Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused local variables despite potential side effects after removing feature flag #35

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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