Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript: Prettier incorrectly breaks up constructor properties from modifiers when there is a decorator and comment before it #16207

Open
Woodz opened this issue Apr 5, 2024 · 1 comment
Labels
area:comments Issues with how Prettier prints comments lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) type:bug Issues identifying ugly output, or a defect in the program

Comments

@Woodz
Copy link

Woodz commented Apr 5, 2024

Prettier 3.2.5
Playground link

--parser typescript

Input:

function required(target: Object, propertyKey: string | symbol, parameterIndex: number) {
}

class Foo {
  constructor(
    @required
    // prettier-ignore
    protected readonly myParam: string,
  ) {}
}

Output:

function required(
  target: Object,
  propertyKey: string | symbol,
  parameterIndex: number,
) {}

class Foo {
  constructor(
    @required
    protected readonly // prettier-ignore
    myParam: string,
  ) {}
}

Expected output:

function required(
  target: Object,
  propertyKey: string | symbol,
  parameterIndex: number,
) {}

class Foo {
  constructor(
    @required
    // prettier-ignore
    protected readonly myParam: string,
  ) {}
}

Why?

Because protected readonly on a separate line is invalid Typescript syntax, plus the prettier-ignore comment applies to the next line, so by moving it down the scope has changed

@sosukesuzuki
Copy link
Member

Thanks for the report. This is a bug that occurs outside of // prettier-ignore comments.

Prettier 3.2.5
Playground link

--parser typescript

Input:

function required(target: Object, propertyKey: string | symbol, parameterIndex: number) {
}

class Foo {
  constructor(
    @required
    // comment
    protected readonly myParam: string,
  ) {}
}

Output:

function required(
  target: Object,
  propertyKey: string | symbol,
  parameterIndex: number,
) {}

class Foo {
  constructor(
    @required
    protected readonly // comment
    myParam: string,
  ) {}
}

@sosukesuzuki sosukesuzuki added type:bug Issues identifying ugly output, or a defect in the program area:comments Issues with how Prettier prints comments lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) labels Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:comments Issues with how Prettier prints comments lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

No branches or pull requests

2 participants