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

Fix: Properly format switch expressions #1069

Merged
merged 2 commits into from
Apr 5, 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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1069.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Properly format switch expressions
links:
- https://github.com/palantir/palantir-java-format/pull/1069
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void reformat_a_subpath_of_a_git_directory_for_only_changed_lines() throws IOExc
runCommandInRepo("git", "init");
runCommandInRepo("git", "config", "user.name", "Test User");
runCommandInRepo("git", "config", "user.email", "[email protected]");
runCommandInRepo("git", "config", "commit.gpgsign", "false");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by improvement: Didn't see it as necessary to sign the commits made by the test

runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init");

Path subdir = repo.resolve("subdir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ public Void visitCase(CaseTree node, Void unused) {
scan(node.getBody(), null);
}
builder.guessToken(";");
builder.forcedBreak(minusTwo);
break;
default:
throw new IllegalArgumentException(node.getCaseKind().name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ class ExpressionSwitch {
default -> false;
};
}

public void test1(int y) {
int x =
switch (y) {
case 1 -> 1;
case 2 -> throw new IllegalArgumentException();
default -> throw new IllegalStateException();
};
}

public void test2(int y) {
int x;
x = switch (y) {
case 1 -> 1;
case 2 -> throw new IllegalArgumentException();
default -> throw new IllegalStateException();};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,22 @@ class ExpressionSwitch {
default -> false;
};
}

public void test1(int y) {
int x =
switch (y) {
case 1 -> 1;
case 2 -> throw new IllegalArgumentException();
default -> throw new IllegalStateException();
};
Comment on lines +52 to +57
Copy link
Member Author

@johnhany97 johnhany97 Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would like to fix this in the future FWIW to instead be:

        int x = switch (y) {
            case 1 -> 1;
            case 2 -> throw new IllegalArgumentException();
            default -> throw new IllegalStateException();
        };

But that requires more complicated changes to happen to the visitAssignment method which should be left to a separate PR

}

public void test2(int y) {
int x;
x = switch (y) {
case 1 -> 1;
case 2 -> throw new IllegalArgumentException();
default -> throw new IllegalStateException();
};
}
}
Loading