prettier-java
prettier-java copied to clipboard
prettier-ignore not working as expected
// 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
}
}
Oh indeed ! I'll have a look
Thanks for the confirmation. Adding a bounty as it's a bug, and because this project is so nice. It deserves more contributions :)
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...
Hi, I tried this version of the plugin
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
any update here?
@pascalgrimaud Bounty claimed: https://opencollective.com/generator-jhipster/expenses/171756
@jtkiesel : approved, thanks for your work