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

fix: hug lambdas without blocks #653

Merged
merged 1 commit into from
Jun 10, 2024

Conversation

jtkiesel
Copy link
Contributor

@jtkiesel jtkiesel commented Mar 5, 2024

What changed with this PR:

Argument "hugging" is now aligned much more closely with Prettier JavaScript. Specifically, lambdas without blocks are "huggable" again.

Example

Input

class T {

  public Semver getMaven() {
    return describe()
      .map(v -> null == v ? PRE_VERSION : v)
      .map(Semver::coerce)
      .map(v -> Objects.equals(v.getVersion(), PRE_VERSION) ? v.withPreRelease(SNAPSHOT) : v)
      .map(
        v ->
          v
            .getPreRelease()
            .stream()
            .filter(p -> p.matches("^\\d+-+g\\p{XDigit}{7}$"))
            .findFirst()
            .map(p -> v.withClearedPreRelease().withPreRelease(SNAPSHOT).withBuild(p))
            .orElse(v)
      )
      .map(v -> new MavenSemver(v.getVersion()))
      .get();
  }

  void t() {
    aaaaaaaaaaaaaaaaaaaaaaaa((bbbbbbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccccccc, dddddddddddddddddddddddd) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff());

    a.b(c -> d -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk));

    a.b(c -> d && eeeeeeeeee.ffffffffff() ? g && hhhhhhhhhh.iiiiiiiiii() : j && kkkkkkkkkk.llllllllll());

    a.b(c -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0);

    a.b(c, (c0, c1) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0);

    a.b(c -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0);

    a.b(c, (c0, c1) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk) > 0);

    a.b(c -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk));

    a.b(c, (c0, c1) -> d && eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk));

    a.b(c -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk));

    a.b(c -> {
      eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk);
    });

    a.b((c0, c1) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk));

    a.b(c, (c0, c1) -> eeeeeeeeee.ffffffffff(gggggggggg, hhhhhhhhhh, iiiiiiiiii, jjjjjjjjjj, kkkkkkkkkk));
  }
}

Output

class T {

  public Semver getMaven() {
    return describe()
      .map(v -> null == v ? PRE_VERSION : v)
      .map(Semver::coerce)
      .map(v ->
        Objects.equals(v.getVersion(), PRE_VERSION)
          ? v.withPreRelease(SNAPSHOT)
          : v
      )
      .map(v ->
        v
          .getPreRelease()
          .stream()
          .filter(p -> p.matches("^\\d+-+g\\p{XDigit}{7}$"))
          .findFirst()
          .map(p ->
            v.withClearedPreRelease().withPreRelease(SNAPSHOT).withBuild(p)
          )
          .orElse(v)
      )
      .map(v -> new MavenSemver(v.getVersion()))
      .get();
  }

  void t() {
    aaaaaaaaaaaaaaaaaaaaaaaa(
      (
        bbbbbbbbbbbbbbbbbbbbbbbb,
        cccccccccccccccccccccccc,
        dddddddddddddddddddddddd
      ) -> eeeeeeeeeeeeeeeeeeeeeeee.ffffffffffffffffffffffff()
    );

    a.b(
      c ->
        d ->
          eeeeeeeeee.ffffffffff(
            gggggggggg,
            hhhhhhhhhh,
            iiiiiiiiii,
            jjjjjjjjjj,
            kkkkkkkkkk
          )
    );

    a.b(c ->
      d && eeeeeeeeee.ffffffffff()
        ? g && hhhhhhhhhh.iiiiiiiiii()
        : j && kkkkkkkkkk.llllllllll()
    );

    a.b(
      c ->
        d &&
        eeeeeeeeee.ffffffffff(
          gggggggggg,
          hhhhhhhhhh,
          iiiiiiiiii,
          jjjjjjjjjj,
          kkkkkkkkkk
        ) >
        0
    );

    a.b(
      c,
      (c0, c1) ->
        d &&
        eeeeeeeeee.ffffffffff(
          gggggggggg,
          hhhhhhhhhh,
          iiiiiiiiii,
          jjjjjjjjjj,
          kkkkkkkkkk
        ) >
        0
    );

    a.b(
      c ->
        eeeeeeeeee.ffffffffff(
          gggggggggg,
          hhhhhhhhhh,
          iiiiiiiiii,
          jjjjjjjjjj,
          kkkkkkkkkk
        ) >
        0
    );

    a.b(
      c,
      (c0, c1) ->
        eeeeeeeeee.ffffffffff(
          gggggggggg,
          hhhhhhhhhh,
          iiiiiiiiii,
          jjjjjjjjjj,
          kkkkkkkkkk
        ) >
        0
    );

    a.b(
      c ->
        d &&
        eeeeeeeeee.ffffffffff(
          gggggggggg,
          hhhhhhhhhh,
          iiiiiiiiii,
          jjjjjjjjjj,
          kkkkkkkkkk
        )
    );

    a.b(
      c,
      (c0, c1) ->
        d &&
        eeeeeeeeee.ffffffffff(
          gggggggggg,
          hhhhhhhhhh,
          iiiiiiiiii,
          jjjjjjjjjj,
          kkkkkkkkkk
        )
    );

    a.b(c ->
      eeeeeeeeee.ffffffffff(
        gggggggggg,
        hhhhhhhhhh,
        iiiiiiiiii,
        jjjjjjjjjj,
        kkkkkkkkkk
      )
    );

    a.b(c -> {
      eeeeeeeeee.ffffffffff(
        gggggggggg,
        hhhhhhhhhh,
        iiiiiiiiii,
        jjjjjjjjjj,
        kkkkkkkkkk
      );
    });

    a.b((c0, c1) ->
      eeeeeeeeee.ffffffffff(
        gggggggggg,
        hhhhhhhhhh,
        iiiiiiiiii,
        jjjjjjjjjj,
        kkkkkkkkkk
      )
    );

    a.b(c, (c0, c1) ->
      eeeeeeeeee.ffffffffff(
        gggggggggg,
        hhhhhhhhhh,
        iiiiiiiiii,
        jjjjjjjjjj,
        kkkkkkkkkk
      )
    );
  }
}

Relative issues or prs:

Closes #650

) {
const lambdaParameterList = this.visit(ctx.lambdaParameterList, params);

if (findDeepElementInPartsArray(lambdaParameterList, ",")) {
const separator = params?.shouldBreak === false ? "" : softline;
const content = putIntoBraces(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that there is some logic handling comments around braces in the putIntoBraces function: does it still works well if you remove the putIntoBraces method ?

Copy link
Contributor Author

@jtkiesel jtkiesel Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're very right! This ended up sending me down quite a bit of a rabbit hole.

I realized that, because of the way putIntoBraces removes comments and puts them directly into the Doc it returns (rather than moving them to another token/node), it meant that certain code paths in which I leveraged the conditionalGroup function (introduced in #632) could result in comments being removed from the code entirely (😱).

I believe that I have now fixed that problem, and have added a bunch of tests for comments around methods and lambdas to ensure I handled all those cases properly.

@jtkiesel
Copy link
Contributor Author

jtkiesel commented Jun 7, 2024

@clementdessoude Apologies for the ping, but just wanted to check in to ask if you could take another look at this, whenever you have the time. Thanks in advance!

@clementdessoude
Copy link
Contributor

Hello @jtkiesel ! No worry for the ping, it is totally normal. Would you be able to have a chat (through mail/twitter/linkedin/any media you want) ? I tried to contact you via linkedin a few weeks ago to discuss about the project, but you may have missed it :)

@clementdessoude clementdessoude merged commit 16c90ba into jhipster:main Jun 10, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: more builder chaining being off
2 participants