trivago / prettier-plugin-sort-imports

A prettier plugin to sort imports in typescript and javascript files by the provided RegEx order.
Apache License 2.0
3.39k stars 133 forks source link

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

Open ThiloAschebrock opened 2 years ago

ThiloAschebrock commented 2 years ago

Your Environment

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 commented 2 years ago

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 commented 2 years ago

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

edorivai commented 2 years ago

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 commented 2 years ago

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