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: improve throws formatting #591

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jtkiesel
Copy link
Contributor

@jtkiesel jtkiesel commented Jul 29, 2023

What changed with this PR:

Constructors/methods with throws clauses now break on their comma-separated list of exception classes rather than violating printWidth, and the left curly brace of the body is broken when this happens in order to separate the exception classes from the body (since they will be at the same indentation).

Example

Input

class Example {

  Example(String arg1, String arg2)
    throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  Example(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  Example(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  )
    throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  void example1(String arg1, String arg2)
    throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  void example2(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  void example3(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  )
    throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }
}

Output

class Example {

  Example(String arg1, String arg2) throws
    RuntimeException,
    RuntimeException,
    RuntimeException,
    RuntimeException
  {
    throw new RuntimeException();
  }

  Example(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  Example(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws
    RuntimeException,
    RuntimeException,
    RuntimeException,
    RuntimeException
  {
    throw new RuntimeException();
  }

  void example1(String arg1, String arg2) throws
    RuntimeException,
    RuntimeException,
    RuntimeException,
    RuntimeException
  {
    throw new RuntimeException();
  }

  void example2(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws RuntimeException, RuntimeException, RuntimeException {
    throw new RuntimeException();
  }

  void example3(
    String arg1,
    String arg2,
    String arg3,
    String arg4,
    String arg5,
    String arg6
  ) throws
    RuntimeException,
    RuntimeException,
    RuntimeException,
    RuntimeException
  {
    throw new RuntimeException();
  }
}

Relative issues or prs:

Closes #286
Closes #429

@DanielFran DanielFran added $100 https://www.jhipster.tech/bug-bounties/ $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ labels Aug 12, 2023
@jtkiesel jtkiesel force-pushed the fix/improve-throws-formatting branch 3 times, most recently from e46ab97 to ea90004 Compare January 16, 2024 05:43
@jtkiesel jtkiesel force-pushed the fix/improve-throws-formatting branch from ea90004 to e379f32 Compare February 10, 2024 23:46
@jtkiesel
Copy link
Contributor Author

@clementdessoude I'm curious what your thoughts are on the formatting decisions I made here. Could you take a look, whenever you have the time?

@@ -652,15 +658,16 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
return printTokenWithComments(this.getSingle(ctx) as IToken);
}

throws(ctx: ThrowsCtx) {
throws(ctx: ThrowsCtx, opts?: { throwsGroupId?: symbol }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should throwsGroupId really be optional, or should we force it to be defined so we are "forced" to align throws formating for constructor/interface methods, etc. as well ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I totally forgot to implement this for interface methods. I went ahead and made this required as you suggested, and implemented the same throws formatting for interface methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
$$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $100 https://www.jhipster.tech/bug-bounties/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Continuation Indent Improve formatting of method will lot of parameters and with exception
3 participants