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

@decorators on class are still being placed on separate line #5360

Closed
desbrowne opened this issue Nov 7, 2018 · 18 comments
Closed

@decorators on class are still being placed on separate line #5360

desbrowne opened this issue Nov 7, 2018 · 18 comments
Labels
lang:javascript Issues affecting JS lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. status:wontfix This will not be worked on

Comments

@desbrowne
Copy link

desbrowne commented Nov 7, 2018

Prettier 1.15.1
Playground link

--parser typescript

Input:

import { action, observable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { RouteComponentProps } from 'react-router';

interface CountHandlerProps extends RouteComponentProps<{}> { }
@observer export class CountHandler extends React.Component<CountHandlerProps> {
	@observable count = 0;
	render() {
      const { data } = this;
      return (
        <div>
          Count: {this.count}
          <button onClick={this.handleIncreaseClick}>Increase</button>
          <button onClick={handleResetClick}>Reset</button>
      	</div>
      );
	}
	@action handleIncreaseClick = () => this.count++;
	@action handleResetClick = () => {
      this.count = 0;
    }
}

Output:

import { action, observable } from "mobx";
import { observer } from "mobx-react";
import * as React from "react";
import { RouteComponentProps } from "react-router";

interface CountHandlerProps extends RouteComponentProps<{}> {}
@observer
export class CountHandler extends React.Component<CountHandlerProps> {
  @observable count = 0;
  render() {
    const { data } = this;
    return (
      <div>
        Count: {this.count}
        <button onClick={this.handleIncreaseClick}>Increase</button>
        <button onClick={handleResetClick}>Reset</button>
      </div>
    );
  }
  @action handleIncreaseClick = () => this.count++;
  @action
  handleResetClick = () => {
    this.count = 0;
  };
}

Expected behavior:
I would expect the @observer decorator and the second @action decorator(fixed in 1.15.2) to stay inline as discussed in #4924.

@desbrowne
Copy link
Author

Prettier 1.15.1
Playground link

--parser babylon

Input:

import { action, observable } from 'mobx';
import { observer } from 'mobx-react';
import React from 'react';
import { RouteComponentProps } from 'react-router';

export @observer class CountHandler extends React.Component {
	@observable count = 0;
	render() {
      const { data } = this;
      return (
        <div>
          Count: {this.count}
          <button onClick={this.handleIncreaseClick}>Increase</button>
          <button onClick={handleResetClick}>Reset</button>
      	</div>
      );
	}
	@action handleIncreaseClick = () => this.count++;
	@action handleResetClick = () => {
      this.count = 0;
    }
}

Output:

import { action, observable } from "mobx";
import { observer } from "mobx-react";
import React from "react";
import { RouteComponentProps } from "react-router";

export
@observer
class CountHandler extends React.Component {
  @observable count = 0;
  render() {
    const { data } = this;
    return (
      <div>
        Count: {this.count}
        <button onClick={this.handleIncreaseClick}>Increase</button>
        <button onClick={handleResetClick}>Reset</button>
      </div>
    );
  }
  @action handleIncreaseClick = () => this.count++;
  @action
  handleResetClick = () => {
    this.count = 0;
  };
}

Expected behavior:
Tried this with babylon parser to double check, seeing the same result.

@ikatyang
Copy link
Member

ikatyang commented Nov 7, 2018

cc @lydell @duailibe @action looks like a bug to me(fixed in #5364) but I'm not sure where should @observer go.

@desbrowne
Copy link
Author

desbrowne commented Nov 7, 2018

Thanks @ikatyang, placing @observer inline is pretty standard in the mobx community, for example the official docs: https://mobx.js.org/refguide/observer-component.html

@suchipi
Copy link
Member

suchipi commented Nov 7, 2018

According to the rationale doc, we decided to always multiline decorators on classes, so that'd be why @observer is where it is.

@suchipi suchipi added lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) lang:javascript Issues affecting JS labels Nov 7, 2018
@desbrowne
Copy link
Author

Thanks, @suchipi, at over 3000 components in our web app that would be a problem for us as it's inconsistent. This is also not consistent with the mobx community.

I see the statement "We don't think it ever makes sense to inline the decorators" but no explanation on why it doesn't make sense. Given the large mobx community (> 17k stars) this statement seem at odds?

@desbrowne
Copy link
Author

desbrowne commented Nov 7, 2018

After speaking with the rest of my team I think we'd be happy to migrate to the multi-line decorator for classes as it seems that there is no real consensus with mobx users (even the official docs have cases of multi-line @observer).

Keen to get the second @action issue fixed so we can enable prettier for our team. Not sure where to start or if @duailibe you may be more familiar with the code?

@desbrowne
Copy link
Author

desbrowne commented Nov 7, 2018

Have added a couple of extra cases

It seems that when a line is long the @decorator on line 25 in linked example is wrapped to the next line, not the expression body

Actually line 26 of this example shows this better

@lydell
Copy link
Member

lydell commented Nov 7, 2018

Thanks, it's unfortunate we didn't know about it being common to inline decorators for classes in mobx. Coming from Python, that looked foreign to me. I guess we can simply remove that special case in a patch release.

@desbrowne
Copy link
Author

Obviously the other cases should work but I'd suggest it's left as is for classes unless others chime in to say they would like it changed.

Our backend is .NET Core so like Python attributes are on a different line. We simply assumed this was the ES way as we only started using decorators when we picked up mobx (~3 years ago). Much of the mobx docs and examples I've seen are inline but again I may be wrong in making that inference that this is common with others who have a broader exposure to decorators.

@desbrowne

This comment has been minimized.

@svipas

This comment has been minimized.

@ikatyang

This comment has been minimized.

@desbrowne

This comment has been minimized.

@svipas

This comment has been minimized.

@ikatyang

This comment has been minimized.

@svipas

This comment has been minimized.

@ikatyang

This comment has been minimized.

@ikatyang ikatyang changed the title @decorators are still being placed on separate line @decorators on class are still being placed on separate line Nov 12, 2018
@ikatyang ikatyang added the status:needs discussion Issues needing discussion and a decision to be made before action can be taken label Nov 12, 2018
@thorn0
Copy link
Member

thorn0 commented Feb 15, 2021

MobX before version 6 encouraged the use of ES.next decorators to mark things as observable, computed and action. However, [...] In the interest of compatibility we have chosen to move away from them in MobX 6

-- MobX docs

Also, I found some docs for an older version, and it has class decorators on separate lines.

So things have changed, and what used to be common in the MobX community, isn't common anymore, which means that moving forward, even fewer users will want this formatting. I'm closing the issue.

@thorn0 thorn0 closed this as completed Feb 15, 2021
@thorn0 thorn0 added status:wontfix This will not be worked on and removed status:needs discussion Issues needing discussion and a decision to be made before action can be taken labels Feb 15, 2021
@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label May 17, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:javascript Issues affecting JS lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. status:wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

6 participants