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.37k stars 133 forks source link

Can't format typescript types file #286

Open cnhongwei opened 8 months ago

cnhongwei commented 8 months ago

Your Environment

Describe the bug

format a file lingui.d.ts

declare module "*.po" {
  const messages: {
    [key: string]: string;
  };
  export { messages };
}

To Reproduce yarn prettier --write lingui.d.ts

[error] lingui.d.ts: SyntaxError: Export 'messages' is not defined. (5:11)
[error]   1 | declare module "*.po" {
[error]   2 |   const messages: {
[error]   3 |     [key: string]: string;
[error]   4 |   };
[error]   5 |   export { messages };
[error]   6 | }
[error]   7 |
error Command failed with exit code 2.

Using IDEA for formatting, the same error is obtained.

SyntaxError: Export 'messages' is not defined. (5:14)
  1 | declare module "*.po" {
  2 |   const messages: {
  3 |     [key: string]: string;
  4 |   };
  5 |      export { messages };
  6 | }
  7 |
    at instantiate (xxx/node_modules/@babel/parser/lib/index.js:63:32)
    at constructor (xxx/node_modules/@babel/parser/lib/index.js:358:12)
    at TypeScriptParserMixin.raise (xxx/node_modules/@babel/parser/lib/index.js:3255:19)
    at TypeScriptParserMixin.parseProgram (xxx/node_modules/@babel/parser/lib/index.js:12463:14)
    at TypeScriptParserMixin.parseTopLevel (xxx/node_modules/@babel/parser/lib/index.js:12450:25)
    at TypeScriptParserMixin.parse (xxx/node_modules/@babel/parser/lib/index.js:14345:10)
    at TypeScriptParserMixin.parse (xxx/node_modules/@babel/parser/lib/index.js:10024:18)
    at parse (xxx/node_modules/@babel/parser/lib/index.js:14386:38)
    at preprocessor (xxx/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/preprocessors/preprocessor.js:15:34)
    at Object.defaultPreprocessor [as preprocess] (xxx/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/preprocessors/default-processor.js:9:44)

Expected behavior

Able to format correctly.

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)

{ "importOrder": [ "^react(\/[^\/]+)?$", "^react-dom(\/[^\/]+)?$", "^react-native(\/[^\/]+)?$", "^next(\/[^\/]+)?$", "", "^[./]" ], "importOrderSeparation": true, "importOrderSortSpecifiers": true, "importOrderGroupNamespaceSpecifiers": true, "xmlWhitespaceSensitivity": "ignore" }

HiroAgustin commented 8 months ago

Same as https://github.com/trivago/prettier-plugin-sort-imports/issues/213