FG
🛠️ Developer Tools

Feature Request: breakBeforeElse

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

I know the goal of prettier is to standardize on a style, but it is also difficult since this did not come out at the same time as the language. Our team uses the exact same style as prettier except placement for `else` statements. It is effectively blocking us from adopting this awesome library. We prefer break before `else` to allow for cleaner comments. [code block] Which is nicer than. [code block] However this is preference and would be awesome if this was added as an option to prettier. @vjeux showed how easy it is to do https://github.com/prettier/prettier/pull/837 and apparently @jlongster likes it :) https://twitter.com/jlongster/status/834407714965057540

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Add breakBeforeElse Option to Prettier

Medium Risk

Prettier currently enforces a specific style for formatting `else` statements, which does not align with the preferences of some teams. The lack of an option to configure this behavior prevents teams from fully adopting Prettier despite its other formatting benefits.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Fork Prettier Repository

    Create a fork of the Prettier repository on GitHub to implement the new feature.

    bash
    git clone https://github.com/YOUR_USERNAME/prettier.git
  2. 2

    Implement breakBeforeElse Option

    Modify the Prettier codebase to introduce a new configuration option `breakBeforeElse`. Update the formatting logic to check for this option and format `else` statements accordingly.

    javascript
    // Example of new option in options.js
    breakBeforeElse: { type: 'boolean', default: false },
  3. 3

    Update Tests

    Add unit tests to ensure that the new `breakBeforeElse` option works correctly. Verify that when the option is set to true, `else` statements are formatted with a line break before them.

    javascript
    // Example test case
    expect(formatCode('if (true) {} else {}', { breakBeforeElse: true })).toEqual('if (true) {}
    else {}');
  4. 4

    Submit Pull Request

    Once the implementation and tests are complete, submit a pull request to the main Prettier repository for review.

    bash
    git push origin feature/breakBeforeElse
    # Then create a pull request on GitHub
  5. 5

    Engage with Community

    Discuss the feature with the Prettier community to gather feedback and address any concerns. Be prepared to make adjustments based on community input.

Validation

To confirm the fix worked, run the Prettier formatter with the new `breakBeforeElse` option set to true. Check that the output correctly formats `else` statements with a line break before them. Additionally, ensure all existing tests pass.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

prettierformattingjavascriptstatus:needs-discussiontype:option-request