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

[prettierX] better class & function formatting options #444

Open
brodybits opened this issue Jan 19, 2021 · 3 comments
Open

[prettierX] better class & function formatting options #444

brodybits opened this issue Jan 19, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@brodybits
Copy link
Owner

From prettier/prettier#10018 I am thinking it would be a nice solution to always put the opening brace ({) on its own line. This would be "solution 2" in prettier/prettier#10018 but for all ES classes. For example:

class SimpleClass
{
  method1() {}
  method2() {}

  method3() {}
}

class HigherLevelClassWithLongNameAndExtendedFunctionality
  extends SuperMajorBaseClassWithHeavyFunctionality
  implements SuperLargeGenericInterface
{
  method1() {}
  method2() {}

  method3() {}
}

I think this would be better than the request in prettier/prettier#4870 to "respect/enforce" line break after class declaration.

@ristomatti
Copy link

ristomatti commented Feb 7, 2021

One option I haven't seen mentioned would be to use double indent for a multi line class (or function) definition. This would be the most preferable to me and I believe it's an option in JetBrains IDE's also.

class HigherLevelClassWithLongNameAndExtendedFunctionality
    extends SuperMajorBaseClassWithHeavyFunctionality
    implements SuperLargeGenericInterface {
  method1() {}
  method2() {}

  method3() {}
}

This would work also for long constructor/method/function parameters.

Input:

function someVeryLongName(param1: QuiteLongType, param2: AnotherLongType, param3: string) {
  doSomeThing();
} 

Current output:

function someVeryLongName(
  param1: QuiteLongType,
  param2: AnotherLongType,
  param3: string) {
  doSomeThing();
} 

Suggested output:

function someVeryLongName(
    param1: QuiteLongType,
    param2: AnotherLongType,
    param3: string) {
  doSomeThing();
} 

Edit: Added missing commas and semicolons.

@brodybits
Copy link
Owner Author

use double indent for a multi line class (or function) definition

My apologies for the delay, definitely looks like a nice option.

class HigherLevelClassWithLongNameAndExtendedFunctionality
    extends SuperMajorBaseClassWithHeavyFunctionality
    implements SuperLargeGenericInterface {
  method1() {}
  method2() {}

  method3() {}
}

Here is how Prettier 2.3.2 & prettierX 0.19.0 would currently format it:

Prettier 2.3.2
Playground link

--parser babel

Input:

class HigherLevelClassWithLongNameAndExtendedFunctionality
    extends SuperMajorBaseClassWithHeavyFunctionality
    implements SuperLargeGenericInterface {
  method1() {}
  method2() {}

  method3() {}
}

Output:

class HigherLevelClassWithLongNameAndExtendedFunctionality
  extends SuperMajorBaseClassWithHeavyFunctionality
  implements SuperLargeGenericInterface
{
  method1() {}
  method2() {}

  method3() {}
}

This would work also for long constructor/method/function parameters.

👍

@brodybits brodybits changed the title improve class formatting improve class & function formatting Jul 8, 2021
@brodybits brodybits changed the title improve class & function formatting [prettierX] better class & function formatting options Jul 8, 2021
@brodybits
Copy link
Owner Author

This would work also for long constructor/method/function parameters.

👍

Input:

function someVeryLongName(param1: QuiteLongType, param2: AnotherLongType, param3: string) {
  doSomeThing();
} 

Current formatting behavior by Prettier 2.3.2 / prettierX 0.19.0 (don't think which parser will make a difference here):

Prettier 2.3.2
Playground link

--parser babel

Input:

function someVeryLongName(param1: QuiteLongType, param2: AnotherLongType, param3: string) {
  doSomeThing();
} 

Output:

function someVeryLongName(
  param1: QuiteLongType,
  param2: AnotherLongType,
  param3: string
) {
  doSomeThing();
}

Suggested output:

function someVeryLongName(
    param1: QuiteLongType,
    param2: AnotherLongType,
    param3: string) {
  doSomeThing();
} 

That would definitely be more compact.

Edit: Added missing commas and semicolons.

Thanks for the edits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants