10000 Fix added switch entries printed by LPP by johannescoetzee · Pull Request #4679 · javaparser/javaparser · GitHub
[go: up one dir, main page]

Skip to content

Conversation

johannescoetzee
Copy link
Contributor

Fixes #4646.

This PR includes 2 changes to fix the token range of switch expression entries. Before, the token range of the ExpressionStmt (which is added to the switch entry) was set to the token range of the Expression contained in it. This was incorrect, since the token range for the ExpressionStmt should include the ; at the end. This could be fixed in the SwitchEntry production/method, but I decided to move it into a separate method is a cleaner solution overall.

After fixing the token range, the LPP output was better, but still formatted strangely:

case 2023 -> new Object();
case 2024 ->
    new java.lang.Object();

This was because of shared logic between the older switch statement entries with : and the newer switch expression entries in the ConcreteSyntaxModel. To fix this, I effectively moved a shared newline() after the :/-> to only affect the switch statement entries and added a space after the -> for switch expressions. I also refactored everything a bit to avoid duplicating the space(), token(ARROW), space() at the end of each -> branch and the token(COLON), newline() at the end of each : branch.

Copy link
codecov bot commented Feb 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 58.143%. Comparing base (4090a69) to head (cafc01f).
Report is 5 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #4679   +/-   ##
=========================================
  Coverage   58.143%   58.143%           
=========================================
  Files          508       508           
  Lines        29771     29771           
  Branches      5251      5251           
=========================================
  Hits         17310     17310           
  Misses       10323     10323           
  Partials      2138      2138           
Flag Coverage Δ
AlsoSlowTests 58.143% <100.000%> (ø)
javaparser-core 58.143% <100.000%> (ø)
javaparser-symbol-solver 58.143% <100.000%> (ø)
jdk-10 58.110% <100.000%> (ø)
jdk-11 58.110% <100.000%> (ø)
jdk-12 58.110% <100.000%> (ø)
jdk-13 58.110% <100.000%> (ø)
jdk-14 58.110% <100.000%> (ø)
jdk-15 58.110% <100.000%> (ø)
jdk-16 58.110% <100.000%> (ø)
jdk-17 58.110% <100.000%> (ø)
jdk-18 58.110% <100.000%> (ø)
jdk-8 58.113% <100.000%> (ø)
jdk-9 58.106% <100.000%> (ø)
macos-latest 58.133% <100.000%> (ø)
ubuntu-latest 58.133% <100.000%> (ø)
windows-latest 58.120% <100.000%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...github/javaparser/printer/ConcreteSyntaxModel.java 99.257% <100.000%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c8fc989...cafc01f. Read the comment docs.

* SwitchLabel -> Expression ;
* }</pre}
*
* for switch entries in the JLS. The expression in the entry body is
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't this be done in a Validator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem isn't that the input code is incorrect, but rather that the JavaParser representation for SwitchEntry is (slightly) incorrect. JP Assumes that the body of a SwitchEntry will be a list of statements which was true before switch expressions were added, but is no longer the case for this rule.

The workaround for this (I assume) was to wrap the expression in an ExpressionStmt node which works well, but is not entirely correct according to the JLS since the ExpressionStmt in this specific case can contain any expression, not just those which are legal expression statements according to the JLS, for example below (a lambda is not a valid expression statement as far as I can tell, but the below snippet is still legal Java code):

        return switch (o) {
            case String s -> (arg) -> System.out.println(arg + s);
            case null, default -> (arg) -> {};
        };

If I understood the situation correctly, then this means that this case does require some special handling, because JP is breaking the rules slightly.


String output = LexicalPreservingPrinter.print(switchExpr);

assertEquals(
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can replace assertEquals by TestUtils.assertEqualsStringIgnoringEol

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the PR with this suggestion

@johannescoetzee johannescoetzee force-pushed the johannes/fix-switch-entry-lpp branch from 3343a18 to 4090a69 Compare February 14, 2025 16:03
@jlerbsc jlerbsc merged commit b9c0ba2 into javaparser:master Feb 22, 2025
35 checks passed
@jlerbsc
Copy link
Collaborator
jlerbsc commented Feb 22, 2025

Once again, thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding new SwitchEntry to SwitchExpr resultis in illeagal code
2 participants
0