Skip to content

Automatic do expressions if expression starts with if, for, switch, try etc. #9

@kasperpeulen

Description

@kasperpeulen

I was playing with this plugin a bit, and this is very related to #7, I don't like excessive indentation for a simple if else statement in JSX. I use prettier, and maybe there could be something changed there as well, but look at this. I have a button here:

<div className="col">
  <button className="btn btn-info btn-lg" onClick={this.endTimer}>
    End timer
   </button>
<div>

Now I realise, that, before I end the timer, I need to check if the timer is started. So I use an if else construct:

<div className="col">
  {
    do {
      if (timer == null) {
        <button className="btn btn-info btn-lg" onClick={this.startTimer}>
          Start Timer
        </button>;
      } else {
        <button className="btn btn-danger btn-lg" onClick={this.endTimer}>
          End Timer
        </button>;
      }
    }
  }
</div>

Suddenly, the button is 6 spaces more indented! And a lot of more extra visual noise. Now since keywords like if, switch, for, try can't be used in an expression context, can't we automatically (behind the scenes), make a do expression construction of this if we encounter such keywords in a expression context. What I mean, could we allow to write the above like this:

<div className="col">
  {if (timer == null) {
    <button className="btn btn-info btn-lg" onClick={this.startTimer}>
      Start Timer
    </button>;
  } else {
    <button className="btn btn-danger btn-lg" onClick={this.endTimer}>
      End Timer
    </button>;
  }}
</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions