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

SyntaxError when combining typescript-parser with sort-imports plugin and TS 4.7 syntax #168

Open
ThiloAschebrock opened this issue Aug 9, 2022 · 4 comments

Comments

@ThiloAschebrock
Copy link

Your Environment

  • Prettier version: 2.7.1
  • node version 14.8.3
  • package manager: yarn 1.22.19
  • IDE: VSCode

Describe the bug

prettier-plugin-sort-imports results in SyntaxError when combined with typescript parser and newer syntax for Typescript types, cf https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#extends-constraints-on-infer-type-variables.

To Reproduce

test.ts:

export type Test<T> = T extends infer P extends string ? P : never;

result in the following error when running prettier with prettier-plugin-sort-imports enabled:

[error] test.ts: SyntaxError: Unexpected token, expected "?" (1:40)
[error]   1 | export type Test<T> = T extends infer P extends string ? P : never;
[error]   2 |

Note that this error is not present when manually selecting babel-ts as the prettier parser or running prettier with --no-plugin-search to disable the plugin.

Expected behavior

No error for this syntax when using prettier with the typescript parser and prettier-plugin-sort-imports.

@ThiloAschebrock
Copy link
Author

The same issue also happens if you are using the new Typescript instantiation expressions, cf. https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#instantiation-expressions.

@IanVS
Copy link

IanVS commented Aug 17, 2022

This is because this project locks the babel versions used. It's updated in #161. Or, you can use https://github.com/ianvs/prettier-plugin-sort-imports which does not lock the babel version.

@edorivai
Copy link

I apologize if this has been discussed previously here, but instead of pinning dependencies on exact versions, wouldn't it be better to allow any version of dependencies that are on the same major?

So instead of

"@babel/parser": "7.18.9",

rather do

"@babel/parser": "^7.18.9",

@ThiloAschebrock
Copy link
Author

I apologize if this has been discussed previously here, but instead of pinning dependencies on exact versions, wouldn't it be better to allow any version of dependencies that are on the same major?

So instead of

"@babel/parser": "7.18.9",

rather do

"@babel/parser": "^7.18.9",

Yes, and this what the fork is doing: https://github.com/IanVS/prettier-plugin-sort-imports/blob/main/package.json#L46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants