facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.07k stars 1.85k forks source link

Support "Do expressions" #6417

Open callumlocke opened 6 years ago

callumlocke commented 6 years ago

do { ... } expressions are now stage 1 and have been supported by Babel for a while.

They're especially useful in JSX, as they help you avoid nested ternaries. It would be great to be able to use them in a Flow project.

const View = ({ loading, error, ...otherProps }: Props) => (
  <div>
    {do {
      if (loading) {
        <Loading />
      } else if (error) {
        <Error error={error} />
      } else {
        <MyLoadedComponent {...otherProps} />
      };
    }}
  </div>
);

NB. This feature was being tracked as part of a long list in #560, but that issue seems to have been forgotten and buried since 2017. I suggest closing that one and opening individual issues for the few remaining ES-next features if they're still wanted.

fishythefish commented 6 years ago

Stage 1 is still pretty early, and that proposal very much needs to be fleshed out. It doesn't seem to be a very active proposal. Babel tends to be the playground/reference implementation for new proposals, so I'm not surprised that they have a plugin for it, but I'd like to see at least a draft of a formal semantics before beginning to implement this.