Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

💭 Formatter feature request: comment before else keyword #1024

@AlchemistCamp

Description

@AlchemistCamp

One of the most contentious decisions in Prettier and the reason I personally no longer use it for any projects, was its forcing the removal of breaks before else blocks: prettier/prettier#840

To illustrate two issues with this, consider the following code:

  // comment for if block
  if (cond1) {
    doFoo(tree, data);
    if (cond2) {
      collect(tree.left, data, depth + 1);
      collect(tree.right, data, depth + 1);
    }
  }
  // comment for else block
  else {
    doBar(data);
  }

As it is now, Rome will throw errors suggesting the following format

  // comment for if block
  if (cond1) {
    doFoo(tree, data);
    if (cond2) {
      collect(tree.left, data, depth + 1);
      collect(tree.right, data, depth + 1);
    }
  } else {
    // comment for else block
    doBar(data);
  }

The problems with eliminating the break before the else are that it now lines up the else block with the wrong conditional and that it eliminates the clearest place for the else block comments.

I don't see much upside in a linter being rigid to this degree, and based on the the discussion on the Prettier issue linked above, many others would agree.


Edit: Since the title was changed, I'll reiterate:

I strongly prefer not removing the break before else, even in cases where there are no comments. The else should line up with the if it corresponds to, not an inner one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FormatterArea: formatterenhancementNew feature request or improvement to existing functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions