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

Expression expected error on logical operator after checking if instanceof GenericClass<T> #13856

Closed
lucasrabiec opened this issue Nov 18, 2022 · 10 comments · Fixed by #13897
Closed
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:tests Issues about tests that are not correct, should be added, or similar

Comments

@lucasrabiec
Copy link
Contributor

lucasrabiec commented Nov 18, 2022

Prettier 2.7.1
Playground link

--parser typescript

Input:

export class Foo<T> {
}

function sample(error: unknown) {
  if (!(error instanceof Foo<'some-type'> || error instanceof Error) || !error.message) {
    return 'something';
  }
}

Output:

SyntaxError: Expression expected. (5:43)
  3 |
  4 | function sample(error: unknown) {
> 5 |   if (!(error instanceof Foo<'some-type'> || error instanceof Error) || !error.message) {
    |                                           ^
  6 |     return 'something';
  7 |   }
  8 | }

Expected behavior:

  3 |
  4 | function sample(error: unknown) {
  5 |   if (
  6 |     !(error instanceof Foo<'some-type'> || error instanceof Error) ||
  7 |     !error.message
  8 |   ) {
  6 |     return 'something'
  7 |   }
  8 | }

It works if I wrap Foo<'some-type'> with parentheses and then prettier formats it like expected behavior (also removes ( )) but I think it should work without wrapping. edit: problem occured again on reformat (obviously)

It works if I split that logical OR into separate statements (I know this is not logically the same as above, just for an example)

!(error instanceof Foo<'some-type'>) ||
!(error instanceof Error) ||
!error.message
@lucasrabiec lucasrabiec changed the title Broken formatting on logical operator after checking if instanceof GenericClass<T> Expression expected error on logical operator after checking if instanceof GenericClass<T> Nov 18, 2022
@fisker
Copy link
Sponsor Member

fisker commented Nov 18, 2022

Not sure when it's fixed, but works in main branch.

@lucasrabiec
Copy link
Contributor Author

@fisker, great! Do you know when it will be released?

@fisker
Copy link
Sponsor Member

fisker commented Nov 18, 2022

Very soon, #13792

@lucasrabiec
Copy link
Contributor Author

Ok, thank you for the info. I am closing it.

@fisker
Copy link
Sponsor Member

fisker commented Nov 18, 2022

Would like to add a test for it?

@lucasrabiec
Copy link
Contributor Author

Yes, I can give it a try. Is tests/format/typescript/generic the correct place for that test?

@fisker
Copy link
Sponsor Member

fisker commented Nov 18, 2022

Maybe tests/format/typescript/logical-expression (not-exist yet) is a better place.

@thorn0
Copy link
Member

thorn0 commented Nov 22, 2022

Expressions like Foo<'some-type'> are called instantiation expressions, so the place for the test is tests\format\typescript\instantiation-expression

@fisker fisker added the type:tests Issues about tests that are not correct, should be added, or similar label Nov 24, 2022
@fisker
Copy link
Sponsor Member

fisker commented Nov 24, 2022

Reopen to track test.

@lucasrabiec
Copy link
Contributor Author

@fisker I added test: #13897

@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Nov 28, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:tests Issues about tests that are not correct, should be added, or similar
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants