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

Record pattern in exhaustive switch syntax unsupported #610

Closed
NolwennD opened this issue Oct 25, 2023 · 4 comments · Fixed by #611
Closed

Record pattern in exhaustive switch syntax unsupported #610

NolwennD opened this issue Oct 25, 2023 · 4 comments · Fixed by #611
Labels
area: enhancement 🔧 $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $200

Comments

@NolwennD
Copy link

Record pattern appears in java 21 and prettier is unable to parse it

Prettier-Java 2.3.1

Input:

package monad;

import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

public sealed interface Maybe<T> {

  static <T> Maybe<T> of(T value) {
    if (value == null) {
      return new None<>();
    }
    return new Some<>(value);
  }

  default T valueOr(T defaultValue) {
    return switch (this) {
      case Some<T> some -> some.value();
      case None() -> defaultValue;
    };
  }

  default boolean isPresent() {
    return switch (this) {
      case Some<T> __ -> true;
      case None() -> false;
    };
  }

  default <R> Maybe<R> map(Function<T, R> mapper) {
    Objects.requireNonNull(mapper);

    return switch (this) {
      case Some(T value) -> of(mapper.apply(value)); // prettier fails here
      case None() -> new None<>();
    };
  }

  default Maybe<T> filter(Predicate<T> predicate) {
    Objects.requireNonNull(predicate);

    return switch (this) {
      case Some(T value) when predicate.test(value) -> new Some<>(value);
      default -> new None<>();
    };
  }
}

record Some<T>(T value) implements Maybe<T> {
}

record None<T>() implements Maybe<T> {
}

Output:

npx prettier --write src/main/java/monad/Maybe.java 
src/main/java/monad/Maybe.java
[error] src/main/java/monad/Maybe.java: Error: Sad sad panda, parsing errors detected in line: 34, column: 19!
[error] Expecting --> ')' <-- but found --> 'value' <--!
[error]         ->compilationUnit
[error]         ->ordinaryCompilationUnit
[error]         ->typeDeclaration
[error]         ->interfaceDeclaration
[error]         ->normalInterfaceDeclaration
[error]         ->interfaceBody
[error]         ->interfaceMemberDeclaration
[error]         ->interfaceMethodDeclaration
[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]         ->switchLabel
[error]         ->caseOrDefaultLabel
[error]         ->caseLabelElement
[error]         ->caseConstant
[error]         ->ternaryExpression
[error]         ->binaryExpression
[error]         ->unaryExpression
[error]         ->primary
[error]         ->primarySuffix
[error]         ->methodInvocationSuffix
@NolwennD
Copy link
Author

case Some(T value) when predicate.test(value) -> new Some<>(value);

when is not recognize as keyword by prettier.

@pascalgrimaud pascalgrimaud added $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ area: enhancement 🔧 $200 labels Oct 25, 2023
@pascalgrimaud
Copy link
Member

Adding a bounty to have this

cc @clementdessoude in case you are available :)

jtkiesel added a commit to jtkiesel/prettier-java that referenced this issue Oct 27, 2023
jtkiesel added a commit to jtkiesel/prettier-java that referenced this issue Oct 27, 2023
jtkiesel added a commit to jtkiesel/prettier-java that referenced this issue Oct 29, 2023
jtkiesel added a commit to jtkiesel/prettier-java that referenced this issue Oct 29, 2023
jtkiesel added a commit to jtkiesel/prettier-java that referenced this issue Oct 29, 2023
jtkiesel added a commit to jtkiesel/prettier-java that referenced this issue Oct 29, 2023
jtkiesel added a commit to jtkiesel/prettier-java that referenced this issue Oct 29, 2023
clementdessoude pushed a commit that referenced this issue Nov 11, 2023
@jtkiesel
Copy link
Contributor

@pascalgrimaud
Copy link
Member

@jtkiesel : approved, thanks for your work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: enhancement 🔧 $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $200
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants