Skip to content

Never inline decorators, unless they're lone parameter decorators#4830

Merged
suchipi merged 3 commits intoprettier:masterfrom
suchipi:dont_inline_decorators
Jul 20, 2018
Merged

Never inline decorators, unless they're lone parameter decorators#4830
suchipi merged 3 commits intoprettier:masterfrom
suchipi:dont_inline_decorators

Conversation

@suchipi
Copy link
Copy Markdown
Member

@suchipi suchipi commented Jul 10, 2018

Fixes #2613.

cc @vjeux who might have more context than me on this.

constructor(@ILifecycleService lifecycleService) {}
constructor(
@ILifecycleService
lifecycleService
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this syntax mean?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here’s what TS has to say:

The expression for the parameter decorator will be called as a function at runtime, with the following three arguments:

  1. Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
  2. The name of the member.
  3. The ordinal index of the parameter in the function’s parameter list.

Copy link
Copy Markdown
Member Author

@suchipi suchipi Jul 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Their example makes it a bit clearer:

class Greeter {
    greeting: string;

    constructor(message: string) {
        this.greeting = message;
    }

    @validate
    greet(@required name: string) {
        return "Hello " + name + ", " + this.greeting;
    }
}

We should keep these inline.

@observable amount: number = 1;
@observable
price: number = 0;
@observable
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It felt a bit odd to not have an empty line between these, but since we preserve user-entered empty lines, I think it's fine (see also the type orm snapshot)

? parent === firstNonConditionalParent
? group(doc)
: doc
? parent === firstNonConditionalParent ? group(doc) : doc
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, my editor formatted with an older version of prettier

: hasComment && !isOpeningFragment
? " "
: "",
: hasComment && !isOpeningFragment ? " " : "",
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

@vjeux
Copy link
Copy Markdown
Contributor

vjeux commented Jul 10, 2018

Originally I put all of them in their own line but there were a lot of people using mobx which complained. Apparently it’s the convention over there to inline them.

I have no opinions myself.

@suchipi suchipi added the status:wip Pull requests that are a work in progress and should not be merged label Jul 10, 2018
@egeozcan
Copy link
Copy Markdown

@vjeux mobx is the only library which inlines them as a convention. even in the spec itself, they are on their own line. I couldn't find any language which has something similar to decorators and inlined them by convention. See the last comments on #2613

@kamilmysliwiec
Copy link
Copy Markdown

Fully agree. Inline decorators look awful and it's quite a standard to keep them in the separated lines. TypeOrm entities, as well as NestJS/Angular stuff, looks really bad once they're inlined. Actually, I went here because I was pretty sure that this issue is finally sorted out.

@suchipi
Copy link
Copy Markdown
Member Author

suchipi commented Jul 17, 2018

I'm hoping to get this into the 1.14 release, but I won't have time to work on this until Thursday evening. I'll add it to the milestone for now, but other maintainers, remove it if you need to (or finish the work for me 😝).

@suchipi suchipi added this to the 1.14 milestone Jul 17, 2018
@suchipi suchipi force-pushed the dont_inline_decorators branch from 90c7b9e to c458d34 Compare July 20, 2018 03:03
@suchipi suchipi removed the status:wip Pull requests that are a work in progress and should not be merged label Jul 20, 2018
@suchipi suchipi changed the title Never inline decorators Never inline decorators, unless they're parameter decorators Jul 20, 2018
@suchipi
Copy link
Copy Markdown
Member Author

suchipi commented Jul 20, 2018

Ok, this is ready for review/merge

@suchipi suchipi changed the title Never inline decorators, unless they're parameter decorators Never inline decorators, unless they're lone parameter decorators Jul 20, 2018
if (
!(
parent[propertyName] &&
Object.prototype.toString.call(parent[propertyName]) === "[object Array]"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Array.isArray (we use it in several places)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice! I'll change it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.isArray isn’t supported on Node 4; do we polyfill?

Copy link
Copy Markdown
Collaborator

@duailibe duailibe Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@j-f1 it is supported. On node 4, it doesn't work with Proxys or subclasses

@suchipi suchipi merged commit 3bfaf66 into prettier:master Jul 20, 2018
@connorjclark
Copy link
Copy Markdown

I'd like to thank the project maintainers for listening to the community on this issue. Thank you for making a great tool!

@devuxer
Copy link
Copy Markdown

devuxer commented Aug 17, 2018

I just noticed this change when formatting a file in a mobx project.

I get the motivation for it, so I don't disagree with putting decorators on a separate line, but I think it would be more readable to put an extra line break between decorated properties.

Harder to read

@observable
public name = "";
@observable
public location = "";
@observable
public nationId = "";

Easier to read

@observable
public name = "";

@observable
public location = "";

@observable
public nationId = "";

Agree/disagree? I can make this a new issue if people agree.

@lydell
Copy link
Copy Markdown
Member

lydell commented Aug 17, 2018

So far, Prettier has never inserted blank lines into your code, as far as I know (only removed/collapsed blank lines). So this would be the first case where it inserts them. Might be good, might be confusing. Not sure. Anyway, check out the discussion in #4924 first.

@devuxer
Copy link
Copy Markdown

devuxer commented Aug 17, 2018

@lydell,

Very interesting. I guess the premise was incorrect that mobx was the only thing using inline decorators, so this is really quite a tough one to solve. I put a suggestion in #4924 as well.

@Rob1kelly15
Copy link
Copy Markdown

Is there a way to configure this to turn off? We work in Angular 6, and when it comes to our input and output decorators, we want them inline, but this prevents us from doing so, since we have it to auto format on save

@j-f1
Copy link
Copy Markdown
Member

j-f1 commented Aug 24, 2018

Please discuss this over in #4924.

@prettier prettier locked and limited conversation to collaborators Aug 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants