dprint-plugin-typescript
dprint-plugin-typescript copied to clipboard
Property decorators: Allow to maintain position or prefer / force to single line
Angular components look usually something like below. @Input() and @Output property decorators are placed on same line as the rest of class property.
@Component({
selector: 'app-hero-child',
})
export class HeroChildComponent {
@Input() hero: Hero;
@Output()
newItemEvent = new EventEmitter<string>();
@HostBinding('class.valid') get valid() { return this.control.valid; }
}
However, dprint will always add new line after @Input(), even when using decorators.preferSingleLine: true.
I'd like to dprint either maintain the position of decorator or have another way to keep it as a single line.
So all of these would maintain their positions
@Component({
selector: 'app-hero-child',
})
export class HeroChildComponent {
@Input() hero: Hero;
@Output()
newItemEvent = new EventEmitter<string>();
@HostBinding('class.valid') get valid() { return this.control.valid; }
}
@Injectable() class NeedsDependency {
constructor(@SkipSelf() @Optional() public dependency: Dependency) {}
}
@Injectable()
class NeedsDependency {
constructor(
@SkipSelf()
@Optional()
public dependency: Dependency
) {}
}
Fine tune by decorator type
Actually, what I would like to have is, that class decorators can be on their own line, but property and parameter decorators would be always inline. This would follow usual Angular practices. So above examples would always be
@Component({
selector: 'app-hero-child',
})
export class HeroChildComponent {
@Input() hero: Hero;
@Output() newItemEvent = new EventEmitter<string>();
@HostBinding('class.valid') get valid() {
return this.control.valid;
}
}
@Injectable()
class NeedsDependency {
constructor(@SkipSelf() @Optional() public dependency: Dependency) {}
}
I'm also running into this and it makes dprint unusable for Angular. This behavior goes against the Angular style guide.
@dsherret Is this a know bug?
Here is a playground link showing that the "decorators.preferSingleLine" setting is ignored.
@cedric-spinergie that specifically is not a bug because the "preferSingleLine" means for multiple decorators:
https://dprint.dev/playground/#code/KYDwDg9gTgLgBAYwDYEMDOa4FkCeBhVDOAbwCg44ABXAEWAWhRmgAoBKKmYNeAWxwBqKJAFdgcALxwARPwDKMKNIDcpAL5A/config/N4KABGBEA2CWB2BTA6rAJgFwBaQFxgEYAmABjIBpwo1EBjAewCcBDDJgZwDoAHRxAM0SMAyggDm0RABkEiPGAyMArohABfEEA/language/typescript
So is there any way to do this?
This is literally the only thing keeping me from using dprint in my project, it's otherwise exactly what I need from a formatter.
Please implement the option to maintain the decorator position as written