dprint-plugin-typescript icon indicating copy to clipboard operation
dprint-plugin-typescript copied to clipboard

Property decorators: Allow to maintain position or prefer / force to single line

Open rubiesonthesky opened this issue 2 years ago • 5 comments

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) {}
}

rubiesonthesky avatar May 14 '23 09:05 rubiesonthesky

I'm also running into this and it makes dprint unusable for Angular. This behavior goes against the Angular style guide.

nwash57 avatar Jun 07 '23 15:06 nwash57

@dsherret Is this a know bug?

Here is a playground link showing that the "decorators.preferSingleLine" setting is ignored.

cedric-spinergie avatar Nov 07 '23 16:11 cedric-spinergie

@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

dsherret avatar Nov 07 '23 20:11 dsherret

So is there any way to do this?

zyf0330 avatar Nov 27 '23 07:11 zyf0330

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

alphatwit avatar Dec 11 '23 20:12 alphatwit