Never inline decorators, unless they're lone parameter decorators#4830
Never inline decorators, unless they're lone parameter decorators#4830suchipi merged 3 commits intoprettier:masterfrom
Conversation
| constructor(@ILifecycleService lifecycleService) {} | ||
| constructor( | ||
| @ILifecycleService | ||
| lifecycleService |
There was a problem hiding this comment.
What does this syntax mean?
There was a problem hiding this comment.
The expression for the parameter decorator will be called as a function at runtime, with the following three arguments:
- Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
- The name of the member.
- The ordinal index of the parameter in the function’s parameter list.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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)
src/language-js/printer-estree.js
Outdated
| ? parent === firstNonConditionalParent | ||
| ? group(doc) | ||
| : doc | ||
| ? parent === firstNonConditionalParent ? group(doc) : doc |
There was a problem hiding this comment.
My bad, my editor formatted with an older version of prettier
src/language-js/printer-estree.js
Outdated
| : hasComment && !isOpeningFragment | ||
| ? " " | ||
| : "", | ||
| : hasComment && !isOpeningFragment ? " " : "", |
|
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. |
|
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. |
|
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 😝). |
90c7b9e to
c458d34
Compare
|
Ok, this is ready for review/merge |
src/common/util.js
Outdated
| if ( | ||
| !( | ||
| parent[propertyName] && | ||
| Object.prototype.toString.call(parent[propertyName]) === "[object Array]" |
There was a problem hiding this comment.
You can use Array.isArray (we use it in several places)
There was a problem hiding this comment.
Oh nice! I'll change it.
There was a problem hiding this comment.
Array.isArray isn’t supported on Node 4; do we polyfill?
There was a problem hiding this comment.
@j-f1 it is supported. On node 4, it doesn't work with Proxys or subclasses
|
I'd like to thank the project maintainers for listening to the community on this issue. Thank you for making a great tool! |
|
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. |
|
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. |
|
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 |
|
Please discuss this over in #4924. |
Fixes #2613.
cc @vjeux who might have more context than me on this.