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

prettier-ignore not working as expected

Open pascalgrimaud opened this issue 4 years ago • 5 comments
trafficstars

// prettier-ignore seems not working as expected, unless I don't know how to use it. If I use // @formatter:off and // @formatter:on, it works well.

Prettier-Java 1.4.0

Input:

package tech.jhipster;

import java.util.Map;

public class StrangePrettierIgnore {

  private StrangePrettierIgnore() {}

  public static void drinkBeers() {
    // prettier-ignore
    Map<String, String> beers = Map.of(
      "beer1", "Gulden Draak",
      "beer2", "Piraat",
      "beer3", "Kapittel"
    );

           System.out.println(beers); // not well formated here
  }
}

Output:

package tech.jhipster;

import java.util.Map;

public class StrangePrettierIgnore {

  private StrangePrettierIgnore() {}

  public static void drinkBeers() {
    // prettier-ignore
    Map<String, String> beers = Map.of(
      "beer1", "Gulden Draak",
      "beer2", "Piraat",
      "beer3", "Kapittel"
    );

           System.out.println(beers); // not well formated here
  }
}

Expected behavior:

package tech.jhipster;

import java.util.Map;

public class StrangePrettierIgnore {

  private StrangePrettierIgnore() {}

  public static void drinkBeers() {
    // prettier-ignore
    Map<String, String> beers = Map.of(
      "beer1", "Gulden Draak",
      "beer2", "Piraat",
      "beer3", "Kapittel"
    );

    System.out.println(beers); // not well formated here
  }
}

pascalgrimaud avatar Sep 26 '21 11:09 pascalgrimaud

Oh indeed ! I'll have a look

clementdessoude avatar Oct 02 '21 08:10 clementdessoude

Thanks for the confirmation. Adding a bounty as it's a bug, and because this project is so nice. It deserves more contributions :)

pascalgrimaud avatar Oct 02 '21 08:10 pascalgrimaud

I investigated a bit, and it is a bit tricky, as it is only happening if you put the prettier-ignore comment at the beginning of a block.

For instance, in the following snippet, the System.out.println(beers); should be correctly formatted:

package tech.jhipster;

import java.util.Map;

public class StrangePrettierIgnore {

  private StrangePrettierIgnore() {}

  public static void drinkBeers() {
    int i;

    // prettier-ignore
    Map<String, String> beers = Map.of(
      "beer1", "Gulden Draak",
      "beer2", "Piraat",
      "beer3", "Kapittel"
    );

           System.out.println(beers); // not well formated here
  }
}

This is caused by the way the comments are attached to the most enclosive node. Here, the prettier ignore comment is attached to the global blockStatements node, and to the first individual blockStatement.

I'm not sure how to handle this. One way could be to ignore some node types when we are trying to attach comments, but it could be a bit complex. I may need some time to find a correct solution...

clementdessoude avatar Oct 16 '21 09:10 clementdessoude

Hi, I tried this version of the plugin

com.hubspot.maven.plugins prettier-maven-plugin 0.8

case 1: without //prettier-ignore

package com.example.demo;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public class DemoApplication {

public static void main(String[] args) {
    var string = "";
    SpringApplication.run(DemoApplication.class, args);
            System.out.println("tesitng");
}

}

was formatted to: as expected. package com.example.demo;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public class DemoApplication {

public static void main(String[] args) {
    var string = "";
    SpringApplication.run(DemoApplication.class, args);
    System.out.println("tesitng");
}

} Case 2: with //prettier-ignore it was ignored as expected.

package com.example.demo;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

//prettier-ignore @SpringBootApplication public class DemoApplication {

public static void main(String[] args) {
    var string = "";
    SpringApplication.run(DemoApplication.class, args);
                System.out.println("tesitng");
}

}

is there a defect at all? maybe I'm missing something here?

Asha

somayaj avatar Oct 30 '21 20:10 somayaj

any update here?

somayaj avatar Nov 03 '21 11:11 somayaj

@pascalgrimaud Bounty claimed: https://opencollective.com/generator-jhipster/expenses/171756

jtkiesel avatar Nov 14 '23 04:11 jtkiesel

@jtkiesel : approved, thanks for your work

pascalgrimaud avatar Nov 14 '23 06:11 pascalgrimaud