prettier-java icon indicating copy to clipboard operation
prettier-java copied to clipboard

Should support pattern matching guards

Open vincentditlevinz opened this issue 2 years ago • 1 comments

Prettier-Java 1.6.1

Input:

package com.vimat.model;

public record Buyer(String name, double bestPrice, double joker) {
  public boolean hasBestOffer(Buyer other) {
      return switch (other) { 
        case null -> true;
        case Buyer b && this.bestPrice > b.bestPrice -> true;
        default -> false;
      };
    }
}

Output:

[ERROR] src/main/java/com/vimat/model/Buyer.java: Error: Sad sad panda, parsing errors detected in line: 7, column: 20!
[ERROR] Expecting --> '->' <-- but found --> '&&' <--!
[ERROR]         ->compilationUnit
[ERROR]         ->ordinaryCompilationUnit
[ERROR]         ->typeDeclaration
[ERROR]         ->classDeclaration
[ERROR]         ->recordDeclaration
[ERROR]         ->recordBody
[ERROR]         ->recordBodyDeclaration
[ERROR]         ->classBodyDeclaration
[ERROR]         ->classMemberDeclaration
[ERROR]         ->methodDeclaration
[ERROR]         ->methodBody
[ERROR]         ->block
[ERROR]         ->blockStatements
[ERROR]         ->blockStatement
[ERROR]         ->statement
[ERROR]         ->statementWithoutTrailingSubstatement
[ERROR]         ->returnStatement
[ERROR]         ->expression
[ERROR]         ->ternaryExpression
[ERROR]         ->binaryExpression
[ERROR]         ->unaryExpression
[ERROR]         ->primary
[ERROR]         ->primaryPrefix
[ERROR]         ->switchStatement
[ERROR]         ->switchBlock
[ERROR]         ->switchRule
[ERROR]     at Object.parse (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/java-parser/src/index.js:41:11)
[ERROR]     at Object.parse (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/prettier-plugin-java/dist/parser.js:4:26)
[ERROR]     at Object.parse$a [as parse] (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/prettier/index.js:13652:19)
[ERROR]     at coreFormat (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/prettier/index.js:15183:16)
[ERROR]     at formatWithCursor$1 (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/prettier/index.js:15423:14)
[ERROR]     at Object.formatWithCursor (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/prettier/index.js:60171:12)
[ERROR]     at format$1 (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/prettier/bin-prettier.js:16640:21)
[ERROR]     at Object.formatFiles$1 [as formatFiles] (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/prettier/bin-prettier.js:16754:22)
[ERROR]     at async main (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/prettier/bin-prettier.js:18616:5)
[ERROR]     at async Object.run (/home/vinz/.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.17/prettier-maven-plugin-0.17-prettier-java-1.6.1/prettier-java/node_modules/prettier/bin-prettier.js:18559:5)

Expected behavior:

Should support pattern matching guards

vincentditlevinz avatar May 02 '22 07:05 vincentditlevinz

Thank you for reporting this @vincentditlevinz ! We indeed did not introduce this preview feature yet, but it should be feasible without breaking the existing !

clementdessoude avatar May 22 '22 06:05 clementdessoude

We are having the same issue. Is there any documentation which states that prettier-java is ready for Java 17+ ? We are having other issues with records as well.

fischermatte avatar Nov 14 '22 07:11 fischermatte

Hello @vincentditlevinz and @fischermatte ! The #559 should fix the issue !

@fischermatte could you create another issue for the records issue you have ? It would be very helpful in order to provide some fixes !

clementdessoude avatar Nov 26 '22 02:11 clementdessoude

@clementdessoude thx a lot! I will try v2 and let you know if there is still something breaking.

Update: I checked it and it looks like there are no troubles with records when running spotless:apply. So 👍for fixing this! What occurs strange to me is now with v2: When I run spotless:check after (!) I sucessfully ran spotless:apply it still fails with plenty of errors. Shouldn't both goals work the same? Anyway - this is another issue not related to this one here.

fischermatte avatar Dec 01 '22 08:12 fischermatte