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

violates JLS formatting for type annotations

Open xenoterracide opened this issue 3 years ago • 13 comments

prettier violates the JLS which states that a type annotation should be just before the type

Prettier-Java 0.5.1

Input:

  private @Nullable Neo4j neo4j;

Output:

  @Nullable
  private Neo4j neo4j;

Expected behavior:

  private @Nullable Neo4j neo4j;

Screenshot from 2022-01-10 13-00-02

xenoterracide avatar Jan 10 '22 18:01 xenoterracide

Which section of the JLS are you referring to?

jhaber avatar Jan 10 '22 18:01 jhaber

https://docs.oracle.com/javase/specs/jls/se12/html/jls-9.html#jls-9.7.4

xenoterracide avatar Jan 10 '22 18:01 xenoterracide

Interesting, it seems like prettier-java would have no way to tell whether annotated fields are declaration annotations, type annotations, or both:

For example, given the field declaration:

@Foo int f;

@Foo is a declaration annotation on f if Foo is meta-annotated by @Target(ElementType.FIELD), and a type annotation on int if Foo is meta-annotated by @Target(ElementType.TYPE_USE). It is possible for @Foo to be both a declaration annotation and a type annotation simultaneously.

Later on it states:

It is customary, though not required, to write declaration annotations before all other modifiers, and type annotations immediately before the type to which they apply.

The current prettier-java behavior seems to match the JLS recommendation for declaration annotations, which I think is reasonable (assuming that prettier-java doesn't have enough information to distinguish between declaration and type annotations). I think declaration annotations are still much more prevalent, and putting annotations before modifiers also formats better when linebreaks are needed, for example:

@AnnotationNumberOne
@AnnotationNumberTwo
@AnnotationNumberThree
@AnnotationNumberFour
public MyClass myClass;

vs.

public @AnnotationNumberOne @AnnotationNumberTwo @AnnotationNumberThree @AnnotationNumberFour MyClass myClass;

(I'm not sure how you could even line-break this)

jhaber avatar Jan 10 '22 18:01 jhaber

Yeah, type annotations aren't that common, honestly I only see it related to @Nullable other nullity-related annotations. To be honest you could just support it via checks for a handful of static ones, and provide functionality for people to provide a list. I'm just annoyed because I have another checker telling me I suck.

xenoterracide avatar Jan 10 '22 18:01 xenoterracide

Yeah I feel your pain, we had some similar struggles at first with prettier clashing with other tools. Eventually we got rigorous about splitting our validation tools into syntactic (how the code looks) vs. semantic (what the code does). We decided to standardize on prettier for all syntactic validation, and disable anything else that was conflicting with it. And for semantic validation, we're trying to standardize on error-prone for everything (although we still have some other tools in the mix at the moment).

Given prettier's option philosophy, it doesn't seem like this would be worth adding an option for

jhaber avatar Jan 10 '22 19:01 jhaber

ironic you should mention error prone, because that's what's checking this error

/home/xeno/IdeaProjects/pm/backend/module/util/model-graph/src/testFixtures/java/com/xenoterracide/ppm/util/modelgraph/Neo4jExtension.java:18: error: [AnnotationPosition] @Nullable is a TYPE_USE annotation, so should appear after modifiers and directly before the type.
  private Neo4j neo4j;
                ^
    (see https://google.github.io/styleguide/javaguide.html#s4.8.5-annotations)
  Did you mean to remove this line?
1 error

xenoterracide avatar Jan 10 '22 19:01 xenoterracide

https://errorprone.info/bugpattern/AnnotationPosition

xenoterracide avatar Jan 10 '22 19:01 xenoterracide

We don't use any of the checks with tags = Style, but it's a bummer they're even adding those sorts of checks. That sort of check doesn't seem to meet their own criteria, even for a warning:

https://errorprone.info/docs/criteria

The warning should be for something that has the potential for significant impact. We want the warnings to be important enough so that when developers see them they take them seriously and often choose to fix them.

jhaber avatar Jan 10 '22 19:01 jhaber

I can just disable it 🤷🏻‍♂️, I don't have more arguments than these ;)

xenoterracide avatar Jan 10 '22 19:01 xenoterracide

One quick solution would be:

// prettier-ignore
private @Nullable Neo4j neo4j;

pascalgrimaud avatar Jan 10 '22 19:01 pascalgrimaud

yeah, but that's kinda painful to do for every Nullable annotation

xenoterracide avatar Jan 10 '22 19:01 xenoterracide

I can just disable it 🤷🏻‍♂️, I don't have more arguments than these ;)

Yeah given that prettier-java is in charge of all the annotation positions, it seems like that check will just keep badgering you about stylistic stuff you can't fix

jhaber avatar Jan 10 '22 20:01 jhaber

perhaps not moving annotations out of that position would be a good workaround per #548 I don't notice this problem with getter/setters, so what's being done there?

xenoterracide avatar Aug 26 '22 17:08 xenoterracide