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

prettier-ignore not working as expected #505

Closed
pascalgrimaud opened this issue Sep 26, 2021 · 7 comments · Fixed by #603
Closed

prettier-ignore not working as expected #505

pascalgrimaud opened this issue Sep 26, 2021 · 7 comments · Fixed by #603
Labels
area: bug 🐛 Something isn't working $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $100 https://www.jhipster.tech/bug-bounties/

Comments

@pascalgrimaud
Copy link
Member

// 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
  }
}
@clementdessoude clementdessoude added the area: bug 🐛 Something isn't working label Oct 2, 2021
@clementdessoude
Copy link
Contributor

Oh indeed ! I'll have a look

@pascalgrimaud pascalgrimaud added $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $100 https://www.jhipster.tech/bug-bounties/ labels Oct 2, 2021
@pascalgrimaud
Copy link
Member Author

pascalgrimaud commented Oct 2, 2021

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

@clementdessoude
Copy link
Contributor

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...

@somayaj
Copy link

somayaj commented Oct 30, 2021

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
Copy link

somayaj commented Nov 3, 2021

any update here?

@jtkiesel
Copy link
Contributor

@pascalgrimaud
Copy link
Member Author

@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: bug 🐛 Something isn't working $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $100 https://www.jhipster.tech/bug-bounties/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants