Search code examples
prettier

Prettier how to accept both single line and multiple line for function args


Hi I want my fn args should be on multiple lines:

// this should be on 1 line,
foo(user_id: string, bar: string, blahBlah: string) {   
  return this.financeService.getUserWallet(user_id);
}

// this fn args should be multi-lines
@Post('/userWallet')
async updateUserWallet(
  @Param('user_id') user_id: string,
  @Param('delta') delta: number,    
  @Param('foo') bar: number,
) {
  ...
}

But prettier force it to single line that make my code hard to read:

async updateUserWallet(@Param('user_id') user_id: string, @Param('delta') delta: number, @Param('foo') bar: number) {
 ...
}

my printWidth: 120

How can I keep using both case: 1 lines, and multiple line args?


Solution

  • Ah, I found out a hacky solution: Because all code after // will become a comment, so prettier will not move your code from line 2 to line 1 if line 1 has a comment at the end of line.

    // add a comment to the end of each args, so no one can merge your args into 1 line
    @Post('/userWallet')
    async updateUserWallet(
      @Param('user_id') user_id: string, //
      @Param('delta') delta: number, //
      @Param('foo') bar: number, //
    ) {
      ...
    }